Skip to content

Commit

Permalink
refactor: use async await pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
DerStimmler committed Jul 24, 2024
1 parent cbf39a8 commit 6c4cb3b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 64 deletions.
76 changes: 38 additions & 38 deletions src/asm-ww.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,44 @@ function convertFile(file: File, format: string, mimeType: string) {

performance.mark("start");

file.arrayBuffer().then((buffer) => {
const worker = new Worker(workerFile);

worker.onmessage = function (e) {
const msg = e.data;
switch (msg.type) {
case "ready":
worker.postMessage({
type: "run",
arguments: ["-i", inputFileName, outputFileName],
MEMFS: [{ name: inputFileName, data: buffer }],
});
break;
case "stdout":
stdout += msg.data + "\n";
break;
case "stderr":
stderr += msg.data + "\n";
break;
case "done":
console.info(stdout);
console.log(stderr);

const out = msg.data.MEMFS[0];

performance.mark("end");

downloadBlob([out.data], mimeType, outputFileName);

worker.terminate();

const measure = performance.measure("test", "start", "end");

updateState(`Duration: ${measure.duration.toFixed(2)} ms`);
break;
}
};
});
const buffer = file.arrayBuffer();

const worker = new Worker(workerFile);

worker.onmessage = (e) => {
const msg = e.data;
switch (msg.type) {
case "ready":
worker.postMessage({
type: "run",
arguments: ["-i", inputFileName, outputFileName],
MEMFS: [{ name: inputFileName, data: buffer }],
});
break;
case "stdout":
stdout += msg.data + "\n";
break;
case "stderr":
stderr += msg.data + "\n";
break;
case "done":
console.info(stdout);
console.log(stderr);

const out = msg.data.MEMFS[0];

performance.mark("end");

downloadBlob([out.data], mimeType, outputFileName);

worker.terminate();

const measure = performance.measure("test", "start", "end");

updateState(`Duration: ${measure.duration.toFixed(2)} ms`);
break;
}
};
}

function updateState(state: string) {
Expand Down
52 changes: 26 additions & 26 deletions src/asm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,33 @@ function convertFile(file: File, format: string, mimeType: string) {

performance.mark("start");

file.arrayBuffer().then((buffer) => {
const result = ffmpeg({
MEMFS: [{ name: inputFileName, data: buffer }],
arguments: ["-i", inputFileName, outputFileName],
print: function (data: string) {
stdout += data + "\n";
},
printErr: function (data: string) {
stderr += data + "\n";
},
onExit: function (code: number) {
console.info(stdout);
console.log(stderr);
console.log("Process exited with code " + code);
},
});

const out = result.MEMFS[0];

performance.mark("end");

downloadBlob([out.data], mimeType, outputFileName);

const measure = performance.measure("test", "start", "end");

updateState(`Duration: ${measure.duration.toFixed(2)} ms`);
const fileBuffer = file.arrayBuffer();

const result = ffmpeg({
MEMFS: [{ name: inputFileName, data: fileBuffer }],
arguments: ["-i", inputFileName, outputFileName],
print: function (data: string) {
stdout += data + "\n";
},
printErr: function (data: string) {
stderr += data + "\n";
},
onExit: function (code: number) {
console.info(stdout);
console.log(stderr);
console.log("Process exited with code " + code);
},
});

const out = result.MEMFS[0];

performance.mark("end");

downloadBlob([out.data], mimeType, outputFileName);

const measure = performance.measure("test", "start", "end");

updateState(`Duration: ${measure.duration.toFixed(2)} ms`);
}

function updateState(state: string) {
Expand Down

0 comments on commit 6c4cb3b

Please sign in to comment.