]> zoso.dev Git - libnemo.git/commitdiff
Pool is misbehaving, so go backwards a bit.
authorChris Duncan <chris@zoso.dev>
Fri, 6 Dec 2024 21:43:31 +0000 (13:43 -0800)
committerChris Duncan <chris@zoso.dev>
Fri, 6 Dec 2024 21:43:31 +0000 (13:43 -0800)
src/lib/pool.ts

index 7ce89744c5288b5fd4b95ec2e1724f3282c455ca..b44b2ed37aa29f415e0ae2b4c3ae51ea8be64d63 100644 (file)
@@ -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)
        }
 }