Move image decoding into the worker pool

This commit is contained in:
Jason Miller
2020-10-05 22:50:06 -04:00
parent 4487da9e9e
commit 4946268ae2

View File

@@ -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);
}