]> zoso.dev Git - libnemo.git/commitdiff
w
authorChris Duncan <chris@zoso.dev>
Sat, 9 Nov 2024 09:27:34 +0000 (01:27 -0800)
committerChris Duncan <chris@zoso.dev>
Sat, 9 Nov 2024 09:27:34 +0000 (01:27 -0800)
src/lib/ckd.ts
src/lib/pool.ts
src/lib/wallet.ts

index 67e1a2835973ce213f4f619801bf63cf0d4dfccc..29864fc406f763fbb0c7a3a472d9c089bc8155a7 100644 (file)
@@ -14,6 +14,9 @@ import type { Ledger } from './ledger.js'
 * @returns {Promise<Account>}
 */
 globalThis.onmessage = async (event) => {
+       console.log('ckd')
+       console.dir(event)
+       console.dir(event.data)
        let result = null
        const { type, seed, index } = event.data
        switch (type) {
@@ -30,7 +33,7 @@ globalThis.onmessage = async (event) => {
                        break
                }
        }
-       postMessage(result)
+       globalThis.postMessage(result)
 }
 
 /**
index e5afa1b9fc11671e93a6c22b38c6998c9949f3bf..3829e2cadd8bb9d1ce1033ef21b580e7747b5c1c 100644 (file)
@@ -3,11 +3,22 @@
 
 const Worker = globalThis.Worker ?? (await import('node:worker_threads')).Worker
 
-type Thread = {
-       worker: Worker,
-       tasks: any[],
-       isAvailable: boolean
+class Thread {
+       worker: Worker
+       tasks: any[]
+       get isAvailable () { return this.tasks.length === 0 }
+
+       constructor (url: string | URL) {
+               this.worker = new Worker(new URL(url, import.meta.url))
+               this.worker.addEventListener('message', (event) => {
+                       if (this.tasks.length > 0) {
+                               this.worker.postMessage(this.tasks.shift())
+                       }
+               })
+               this.tasks = new Array()
+       }
 }
+
 /**
 * Assigns a Web Worker to process data. Creates a new one if none are available.
 *
@@ -23,30 +34,21 @@ export class Pool {
        constructor (url: string | URL) {
                this.#url = new URL(url, import.meta.url)
                this.#threads = [...Array(navigator.hardwareConcurrency)]
-               this.#threads = this.#threads.map(() => {
-                       return {
-                               worker: new Worker(this.#url),
-                               tasks: new Array(),
-                               get isAvailable () { return this.tasks.length === 0 }
-                       }
-               })
+               this.#threads = this.#threads.map(() => { return new Thread(this.#url) })
        }
 
        async work (data: object): Promise<any> {
-               return new Promise((resolve) => {
-                       const thread = this.#threads.reduce((curr, next) => {
-                               return (next.tasks.length < curr.tasks.length)
-                                       ? next
-                                       : curr
-                       })
-                       thread.tasks.push(data)
-                       thread.worker.postMessage(thread.tasks.shift())
-                       thread.worker.onmessage = (event: any) => {
-                               if (thread.tasks.length > 0) {
-                                       thread.worker.postMessage(thread.tasks.shift())
-                               }
+               const thread = this.#threads.reduce((curr, next) => {
+                       return (next.tasks.length < curr.tasks.length)
+                               ? next
+                               : curr
+               })
+               thread.tasks.push(data)
+               thread.worker.postMessage(thread.tasks.shift())
+               return new Promise(resolve => {
+                       thread.worker.addEventListener('message', (event: any) => {
                                resolve(event.data)
-                       }
+                       }, { once: true })
                })
        }
 }
index 163a3aa26b30b9ee09df5ccdb29739dffd7ae600..0d4aed030f65b50039c54c4610359e6208186663 100644 (file)
@@ -13,7 +13,7 @@ import { Safe } from './safe.js'
 import Tools from './tools.js'\r
 import type { Ledger } from './ledger.js'\r
 \r
-const ckdPool = new Pool('ckd.js')\r
+const ckdPool = new Pool('./ckd.js')\r
 \r
 /**\r
 * Represents a wallet containing numerous Nano accounts derived from a single\r