diff --git a/codecs/oxipng/spawn.ts b/codecs/oxipng/spawn.ts index f6c77093..59293615 100644 --- a/codecs/oxipng/spawn.ts +++ b/codecs/oxipng/spawn.ts @@ -19,7 +19,10 @@ async function startMainThread() { // First, let browser fetch and spawn Workers for our pool in the background. // This is fairly expensive, so we want to start it as early as possible. - const workers = Array.from({ length: num }, () => new Worker('./worker', { type: 'module' })); + const workers = Array.from({ length: num }, () => new Promise((resolve) => { + const w = new Worker('./worker', { type: 'module' }); + w.addEventListener('message', () => resolve(w), { once: true }); + })); // Meanwhile, asynchronously compile, instantiate and initialise Wasm on our main thread. await initOxiPNG(fetch(wasmUrl), undefined as any); @@ -38,7 +41,12 @@ async function startMainThread() { // If we didn't do this ping-pong game, the `start_main_thread` below would block the current // thread on an atomic before even *sending* the `postMessage` containing memory, // so Workers would never be able to unblock us back. - await Promise.all(workers.map(worker => initWorker(worker, workerInit))); + + // TODO: remove this line. + // It waits for all workers to be created just to repro a possible V8 bug. + const resolvedWorkers = await Promise.all(workers); + + await Promise.all(resolvedWorkers.map(worker => initWorker(worker, workerInit))); // Finally, instantiate rayon pool - this will use shared Wasm memory to send tasks to the // Workers and then block until they're all ready. diff --git a/codecs/oxipng/worker.ts b/codecs/oxipng/worker.ts index 69f60501..5a813fb7 100644 --- a/codecs/oxipng/worker.ts +++ b/codecs/oxipng/worker.ts @@ -4,6 +4,10 @@ import initOxiPNG, { start_worker_thread } from './pkg-parallel'; export type WorkerInit = [WebAssembly.Module, WebAssembly.Memory]; +// TODO: remove this line. +// It allows main thread to wait for a Worker to be created just to repro a possible V8 bug. +postMessage(null); + addEventListener( 'message', async (event) => { diff --git a/src/components/compress/index.tsx b/src/components/compress/index.tsx index f48d0173..1657f8e1 100644 --- a/src/components/compress/index.tsx +++ b/src/components/compress/index.tsx @@ -232,7 +232,7 @@ export default class Compress extends Component { { latestSettings: { preprocessorState: defaultPreprocessorState, - encoderState: { type: mozJPEG.type, options: mozJPEG.defaultOptions }, + encoderState: { type: oxiPNG.type, options: oxiPNG.defaultOptions }, }, loadingCounter: 0, loadedCounter: 0,