mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-11 16:26:20 +00:00
Move image decoding into the worker pool
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user