mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-13 09:17:20 +00:00
Now initialise all workers with module+memory separately, and then instead of using postMessage to send thread pointers, push them into a crossbeam-deque on the Rust side. Rayon already depends on crossbeam-dequeue, so we're not even adding another dependency, and this model allows us to push "tasks" (thread pointers) on the main thread and pop them on worker threads in arbitrary order without sacrificing correctness.
24 lines
697 B
JavaScript
24 lines
697 B
JavaScript
import initOxiPNG, { worker_initializer, start_main_thread, optimise } from './pkg/oxipng.js';
|
|
import wasmUrl from "./pkg/oxipng_bg.wasm";
|
|
|
|
async function startMainThread() {
|
|
let num = navigator.hardwareConcurrency;
|
|
await initOxiPNG(fetch(wasmUrl));
|
|
let workerInit = worker_initializer();
|
|
let workers = [];
|
|
for (let i = 0; i < num; i++) {
|
|
workers.push(new Promise(resolve => {
|
|
let worker = new Worker("./worker.js", { type: "module" });
|
|
worker.postMessage(workerInit);
|
|
worker.addEventListener('message', resolve, { once: true });
|
|
}));
|
|
}
|
|
await Promise.all(workers);
|
|
start_main_thread(num);
|
|
return {
|
|
optimise
|
|
};
|
|
}
|
|
|
|
export default startMainThread();
|