Add pool, flesh out auth

This commit is contained in:
Jon Staab
2025-03-20 16:50:39 -07:00
parent 7d0d303dae
commit f00ffc5c9c
5 changed files with 351 additions and 40 deletions
+19 -8
View File
@@ -7,19 +7,21 @@ export type TaskQueueOptions<Item> = {
export class TaskQueue<Item> {
items: Item[] = []
isPaused = false
isProcessing = false
constructor(readonly options: TaskQueueOptions<Item>) {}
push(item: Item) {
this.items.push(item)
if (!this.isProcessing) {
this.processBatch()
}
this.process()
}
async processBatch() {
async process() {
if (this.isProcessing || this.isPaused) {
return
}
this.isProcessing = true
for (const item of this.items.splice(0, this.options.batchSize)) {
@@ -30,15 +32,24 @@ export class TaskQueue<Item> {
}
}
this.isProcessing = false
if (this.items.length > 0) {
await yieldThread()
this.processBatch()
} else {
this.isProcessing = false
this.process()
}
}
stop() {
this.isPaused = true
}
start() {
this.isPaused = false
this.process()
}
clear() {
this.items = []
}