From 4946268ae2642cb89b0fcf7ff76b82e45665f39e Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Mon, 5 Oct 2020 22:50:06 -0400 Subject: [PATCH] Move image decoding into the worker pool --- cli/src/index.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cli/src/index.js b/cli/src/index.js index f971900b..9b071dc6 100644 --- a/cli/src/index.js +++ b/cli/src/index.js @@ -104,11 +104,33 @@ async function encodeFile({ }; } +// both decoding and encoding go through the worker pool +function handleJob(params) { + const { operation } = params; + if (operation === 'encode') { + return encodeFile(params); + } + if (operation === 'decode') { + return decodeFile(params.file); + } +} async function processFiles(files) { const workerPool = new WorkerPool(cpus().length, __filename); // Create output directory await fsp.mkdir(program.outputDir, { recursive: true }); + let decoded = 0; + const decodedFiles = await Promise.all(files.map(async file => { + const result = await workerPool.dispatchJob({ operation: 'decode', file }); + results.set(file, { + file: result.file, + size: result.size, + outputs: [] + }); + progress.setProgress(++decoded, files.length); + return result; + })); + const decodedFiles = await Promise.all(files.map(file => decodeFile(file))); let jobsStarted = 0; @@ -135,6 +157,7 @@ async function processFiles(files) { jobsStarted++; workerPool .dispatchJob({ + operation: 'encode', file, size, bitmap, @@ -195,5 +218,5 @@ if (isMainThread) { program.parse(process.argv); } else { - WorkerPool.useThisThreadAsWorker(encodeFile); + WorkerPool.useThisThreadAsWorker(handleJob); }