Skip to content

Commit

Permalink
Save images to localforage & redirect to download page when all mocku…
Browse files Browse the repository at this point in the history
…ps generated
  • Loading branch information
YayunHuang committed Aug 28, 2024
1 parent ef0936b commit d723a25
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions public/scripts/models/image-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ImageUpload {
message = null;
ulid = null;
previewUrl = null;
generatedMockups = [];

loadDimensionPromise = null;

Expand Down
51 changes: 49 additions & 2 deletions public/scripts/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function getMaxWorkers() {
// browser does not support navigator.hardwareConcurrency, fallback to default
return DEFAULT_MAX_WEB_WORKERS;
}
return navigator.hardwareConcurrency;
return 4;
}

function runWorker(worker, imageUpload, orientationIndex, mode) {
Expand Down Expand Up @@ -100,7 +100,13 @@ function runWorker(worker, imageUpload, orientationIndex, mode) {
ImageUploadState.ReadSuccess,
);
} else if (mode === "mockup") {
console.log("generate mockup", imageUpload.file.name, orientationIndex);
const ulid = e.data["ulid"];
const results = e.data["results"];

window.viewModel.fileList.addGeneratedMockupToImageUploadByULID(
ulid,
results,
);
}

window.viewModel.idleWorker(worker);
Expand Down Expand Up @@ -187,6 +193,15 @@ class FileListViewModel {
return imageUpload;
});
}

addGeneratedMockupToImageUploadByULID(ulid, generatedMockup) {
this._imageUploads = this._imageUploads.map((imageUpload) => {
if (imageUpload.ulid == ulid) {
imageUpload.generatedMockups.push(generatedMockup);
}
return imageUpload;
});
}
}

class RootViewModel {
Expand Down Expand Up @@ -788,6 +803,38 @@ function main() {
},
);

// observe fileListViewModel: imageUploads[].generatedMockups[].length
mobx.reaction(
() =>
viewModel.fileList.imageUploads.map(
(imageUpload) => imageUpload.generatedMockups.length,
),
async () => {
if (viewModel.isGeneratingMockup) {
const imageUploads = viewModel.fileList.imageUploads;
let generatedMockups = [];
for (let i = 0; i < imageUploads.length; i += 1) {
generatedMockups = [
...generatedMockups,
...imageUploads[i].generatedMockups,
];
if (
imageUploads[i].generatedMockups.length < viewModel.orientationsNum
) {
return;
}
}

window.localforage
.setItem("pictureArray", generatedMockups)
.then(() => {
window.location.href =
"/download/?deviceId=" + window.workerDeviceId;
});
}
},
);

// observe viewModel: selectedPreviewImageULID
mobx.reaction(
() => viewModel.selectedPreviewImageULID,
Expand Down

0 comments on commit d723a25

Please sign in to comment.