From: Chris Duncan Date: Sat, 9 Nov 2024 09:27:34 +0000 (-0800) Subject: w X-Git-Tag: v0.0.20~35 X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=b2375ef2215c3e6c239b54d41f2b3d4b6cadc490;p=libnemo.git w --- diff --git a/src/lib/ckd.ts b/src/lib/ckd.ts index 67e1a28..29864fc 100644 --- a/src/lib/ckd.ts +++ b/src/lib/ckd.ts @@ -14,6 +14,9 @@ import type { Ledger } from './ledger.js' * @returns {Promise} */ 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) } /** diff --git a/src/lib/pool.ts b/src/lib/pool.ts index e5afa1b..3829e2c 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -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 { - 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 }) }) } } diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 163a3aa..0d4aed0 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -13,7 +13,7 @@ import { Safe } from './safe.js' import Tools from './tools.js' import type { Ledger } from './ledger.js' -const ckdPool = new Pool('ckd.js') +const ckdPool = new Pool('./ckd.js') /** * Represents a wallet containing numerous Nano accounts derived from a single