From: Chris Duncan Date: Fri, 6 Dec 2024 21:43:31 +0000 (-0800) Subject: Pool is misbehaving, so go backwards a bit. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=de95a562a8b775b8f4db401a75eef45aa1a7863d;p=libnemo.git Pool is misbehaving, so go backwards a bit. --- diff --git a/src/lib/pool.ts b/src/lib/pool.ts index 7ce8974..b44b2ed 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -11,8 +11,8 @@ type Thread = { */ export class Pool { #approach: 'converge' | 'divide' = 'divide' - // #cores: number = Math.max(1, navigator.hardwareConcurrency - 1) - #cores: number = 1 + #cores: number = Math.max(1, navigator.hardwareConcurrency - 1) + // #cores: number = 1 #queue: object[] = [] #resolve: Function = (value: unknown): void => { } #results: object[] = [] @@ -29,16 +29,11 @@ export class Pool { } constructor (fn: string) { - if (typeof window === 'undefined' && typeof self === 'undefined') { - this.#url = fn - } else { - this.#url = URL.createObjectURL(new Blob([fn], { type: 'text/javascript' })) - } + this.#url = URL.createObjectURL(new Blob([fn], { type: 'text/javascript' })) for (let i = this.#cores; i > 0; i--) { const thread = { isBusy: false, - //@ts-expect-error - worker: new Worker(this.#url, { type: 'module', eval: true }) + worker: new Worker(this.#url, { type: 'module' }) } thread.worker.addEventListener('message', message => { const data = new TextDecoder().decode(message.data ?? message) @@ -51,7 +46,8 @@ export class Pool { } #assign (thread: Thread, next: any[]): void { - if (next.length > 0) { + console.dir(thread.worker) + if (next?.length > 0) { thread.isBusy = true const buf = new TextEncoder().encode(JSON.stringify(next)).buffer thread.worker.postMessage(buf, [buf]) @@ -101,10 +97,6 @@ export class Pool { } dismiss (): void { - if (typeof Window === 'undefined') { - this.#threads.forEach(thread => thread.worker.terminate()) - } else { - URL.revokeObjectURL(this.#url) - } + URL.revokeObjectURL(this.#url) } }