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
+3
View File
@@ -7,6 +7,9 @@
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=12.0.0"
},
"type": "module",
"files": [
"build"
+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 = []
}
}
+18
View File
@@ -351,6 +351,24 @@ export const displayDomain = (url: string) => displayUrl(first(url.split(/[\/\?]
*/
export const sleep = (t: number) => new Promise(resolve => setTimeout(resolve, t))
/**
* Creates a microtask that yields to other tasks in the event loop
* @returns Promise that resolves after yielding
*/
export const yieldThread = () => {
if (
typeof window !== "undefined" &&
"scheduler" in window &&
"yield" in (window as any).scheduler
) {
return (window as any).scheduler.yield()
}
return new Promise<void>(resolve => {
setTimeout(resolve, 0)
})
}
/**
* Concatenates multiple arrays, filtering out null/undefined
* @param xs - Arrays to concatenate
+1
View File
@@ -4,6 +4,7 @@ export * from "./Emitter.js"
export * from "./LRUCache.js"
export * from "./Tools.js"
export * from "./Worker.js"
export * from "./TaskQueue.js"
export {default as normalizeUrl} from "./normalize-url/index.js"
declare module "@welshman/lib" {