Add task queue, work on socket

This commit is contained in:
Jon Staab
2025-03-20 14:22:40 -07:00
parent 7706034b99
commit 7d0d303dae
10 changed files with 345 additions and 35 deletions
+45
View File
@@ -0,0 +1,45 @@
import {yieldThread} from "./Tools.js"
export type TaskQueueOptions<Item> = {
batchSize: number
processItem: (item: Item) => unknown
}
export class TaskQueue<Item> {
items: Item[] = []
isProcessing = false
constructor(readonly options: TaskQueueOptions<Item>) {}
push(item: Item) {
this.items.push(item)
if (!this.isProcessing) {
this.processBatch()
}
}
async processBatch() {
this.isProcessing = true
for (const item of this.items.splice(0, this.options.batchSize)) {
try {
await this.options.processItem(item)
} catch (e) {
console.error(e)
}
}
if (this.items.length > 0) {
await yieldThread()
this.processBatch()
} else {
this.isProcessing = false
}
}
clear() {
this.items = []
}
}