Files
speckle-server/packages/objectloader2/src/queues/keyedQueue.ts
T
Adam Hathcock c5967a9616 (OL2) move files around to make more sense (#4950)
* Rename to saveBatch

* forgot a file

* first pass of cacheReader

* OL2 tests have infinite timeout

* OL2 refactor works

* fix for tests

* moved/removed types to make a more logical structure

* fixed imports

* rework loop to be in async generator for the expected count

* get rid of pumps and fix test

* lint fix

* redo mermaid diagrams

* add readme section on deferment

* always return root first

* fix linting

* revert the counting

* merge fixes

* remove unused var
2025-06-26 13:28:50 +01:00

58 lines
1.1 KiB
TypeScript

export default class KeyedQueue<K, V> {
private _map: Map<K, V>
private _order: K[]
constructor() {
this._map = new Map<K, V>()
this._order = []
}
enqueue(key: K, value: V): boolean {
if (this._map.has(key)) {
return false // Key already exists
}
this._map.set(key, value)
this._order.push(key)
return true
}
enqueueAll(keys: K[], values: V[]): number {
let count = 0
for (let i = 0; i < keys.length; i++) {
if (!this._map.has(keys[i])) {
this._map.set(keys[i], values[i])
this._order.push(keys[i])
count++
}
}
return count
}
get(key: K): V | undefined {
return this._map.get(key)
}
has(key: K): boolean {
return this._map.has(key)
}
get size(): number {
return this._order.length
}
spliceValues(start: number, deleteCount: number): V[] {
const splicedKeys = this._order.splice(start, deleteCount)
const result: V[] = []
for (const key of splicedKeys) {
const value = this._map.get(key)
if (value !== undefined) {
result.push(value)
this._map.delete(key)
}
}
return result
}
}