Various optimizations

This commit is contained in:
Jon Staab
2026-02-05 17:48:29 -08:00
parent d2756ead28
commit 99c1d09b62
9 changed files with 77 additions and 52 deletions
+17 -16
View File
@@ -1,7 +1,8 @@
import {remove, yieldThread} from "./Tools.js"
import {remove} from "./Tools.js"
export type TaskQueueOptions<Item> = {
batchSize: number
batchDelay: number
processItem: (item: Item) => unknown
}
@@ -30,32 +31,32 @@ export class TaskQueue<Item> {
}
}
async process() {
process() {
if (this.isProcessing || this.isPaused || this.items.length === 0) {
return
}
this.isProcessing = true
await yieldThread()
setTimeout(async () => {
for (const item of this.items.splice(0, this.options.batchSize)) {
try {
for (const subscriber of this._subs) {
subscriber(item)
}
for (const item of this.items.splice(0, this.options.batchSize)) {
try {
for (const subscriber of this._subs) {
subscriber(item)
await this.options.processItem(item)
} catch (e) {
console.error(e)
}
await this.options.processItem(item)
} catch (e) {
console.error(e)
}
}
this.isProcessing = false
this.isProcessing = false
if (this.items.length > 0) {
this.process()
}
if (this.items.length > 0) {
this.process()
}
}, this.options.batchDelay)
}
stop() {
+1 -3
View File
@@ -1257,11 +1257,9 @@ export const batch = <T>(t: number, f: (xs: T[]) => void) => {
}
return (x: T) => {
const shouldFlush = timeoutId === undefined
xs.push(x)
if (shouldFlush) {
if (!timeoutId) {
f(xs.splice(0))
timeoutId = setTimeout(later, t)
}