diff --git a/rollup.config.js b/rollup.config.js index 968ec5d..a7f0e9b 100755 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,4 +1,4 @@ -#!node_modules/.bin/rollup --config +#!rollup --config import babelPresetEnv from "@babel/preset-env" import babelPresetTypescript from "@babel/preset-typescript" import { babel } from "@rollup/plugin-babel" diff --git a/scripts/package.sh b/scripts/package.sh index 9cb3138..335d7a6 100755 --- a/scripts/package.sh +++ b/scripts/package.sh @@ -4,5 +4,5 @@ rm -rf dist ./rollup.config.js scripts/emit-declarations.sh || true scripts/emit-package-json.js -rm dist/internal.d.ts dist/process.d.ts dist/worker.d.ts +rm dist/internal.d.ts dist/process.d.ts cp readme.md license dist diff --git a/src/importInWorker.ts b/src/importInWorker.ts index 695a48a..27791df 100644 --- a/src/importInWorker.ts +++ b/src/importInWorker.ts @@ -1,9 +1,9 @@ import type { AnyFunction, Async, Cloneable, Entries, Rejecter, Resolver } from "@samual/lib" import { cpus } from "os" -import { Worker, isMainThread, threadId } from "worker_threads" +import { Worker, isMainThread, parentPort, threadId } from "worker_threads" import { MessageTag, type Message } from "./internal" -// console.debug(`hello from thread ${threadId}`) +parentPort?.postMessage(undefined) const broadcastChannel = new BroadcastChannel(`oz5iuq2d9vnjoifjitqwwbj2`) @@ -81,12 +81,11 @@ broadcastChannel.addEventListener("message", async event => { let idCounter = 0 -if (isMainThread) { - const url = new URL(import.meta.url) +const url = new URL(import.meta.url) +let anyOnlinePromise: Promise - for (let index = cpus().length; index--;) - new Worker(url) -} +if (isMainThread) + anyOnlinePromise = Promise.any(cpus().map(() => new Promise(resolve => new Worker(url).on("message", resolve)))) /** @example * const heavyFunction = importInWorker( @@ -99,8 +98,12 @@ export const importInWorker = < >(url: URL, name: TName) => ((...args: any) => { const taskId = idCounter++ - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { tasks.set(taskId, { resolve, reject, data: { path: url.href, name, args } }) + + if (anyOnlinePromise) + await anyOnlinePromise + broadcastChannel.postMessage({ tag: MessageTag.Task, fromThreadId: threadId, taskId } satisfies Message) }) }) as Async diff --git a/src/test/fibonacci.ts b/src/test/fibonacci.ts new file mode 100644 index 0000000..01b7210 --- /dev/null +++ b/src/test/fibonacci.ts @@ -0,0 +1,26 @@ +import { importInWorker } from "../importInWorker" +import { isMainThread } from "worker_threads" + +const asyncFibonacci = importInWorker( + new URL("./fibonacci.js", import.meta.url), + "fibonacci" +) + +export async function fibonacci(n: number): Promise { + // console.log(`${threadId} fibonacci(${n})`) + + if (n < 2) + return n + + const [ a, b ] = await Promise.all([ asyncFibonacci(n - 1), asyncFibonacci(n - 2) ]) + + return a + b +} + +if (isMainThread) { + console.time() + const answer = await fibonacci(26) + console.timeEnd() + console.log(answer) + process.exit() +}