Various optimizations
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user