Skip to content

Commit

Permalink
MPR Workaround firefox worker init fail by limiting firefox workers to 2
Browse files Browse the repository at this point in the history
Workaround firefox worker init fail by limiting firefox workers to 2
  • Loading branch information
YayunHuang authored Aug 28, 2024
2 parents 68631fb + ccfa270 commit 49056d9
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions public/scripts/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ async function runWorker(worker) {
);
}

const MAX_FIREFOX_WEB_WORKERS = 2;
const DEFAULT_MAX_WEB_WORKERS = 4; // most CPUs at least have 4 cores nowadays
function isUserAgentFirefox() {
return navigator.userAgent.match(/firefox|fxios/i);
}

function getMaxWorkers() {
if (isUserAgentFirefox()) {
return MAX_FIREFOX_WEB_WORKERS;
}
if (navigator.hardwareConcurrency == null) {
// browser does not support navigator.hardwareConcurrency, fallback to default
return DEFAULT_MAX_WEB_WORKERS;
}
return navigator.hardwareConcurrency;
}

function runPreviewWorker(worker, imageUpload) {
const imageUploadFile = imageUpload.file;
worker.worker.postMessage({
Expand Down Expand Up @@ -211,7 +228,7 @@ class RootViewModel {
_isGeneratingMockup = false;
worker = new Worker("/scripts/web_worker.js");
workerPool = [];
maxWorkers = 4;
maxWorkers = 0;
selectedColorId = null;
selectedPreviewImageULID = null;

Expand All @@ -228,7 +245,8 @@ class RootViewModel {
this.selectedColorId = selectedColorId;
this.maxMockupWaitSec = maxMockupWaitSec;
this.fileList = fileListViewModel;
this.maxWorkers = navigator.hardwareConcurrency || 4;

this.maxWorkers = getMaxWorkers();

// Reserve one worker to generate the final mockup, will update later
for (let i = 0; i < this.maxWorkers - 1; i += 1) {
Expand Down

0 comments on commit 49056d9

Please sign in to comment.