Skip to content

Commit

Permalink
refactor: improve timer worker initialization (less branching)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeg committed Oct 14, 2024
1 parent ef67357 commit 9c4d254
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions public/ocr/ocr_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2128,16 +2128,23 @@ const workerTimer = {
return new Promise(resolve => {
const blob = new Blob([worker_script], { type: 'text/javascript' });
this.worker = new Worker(window.URL.createObjectURL(blob));
this.worker.addEventListener('message', e => {

const handleWorkerMessage = e => {
const [cmd, ...args] = e.data;
if (cmd === 'interval') {
const [callid] = args;
console.log({ cmd, callid });
this.callbacks[callid]?.();
} else if (cmd === 'init') {
resolve();
}
});
};

const handleWorkerInit = () => {
this.worker.removeEventListener('message', handleWorkerInit);
this.worker.addEventListener('message', handleWorkerMessage);
resolve();
};

// by convention the first message is guaranteed to be the init message, so we don't need to check the details
this.worker.addEventListener('message', handleWorkerInit);
});
},
};
Expand Down

0 comments on commit 9c4d254

Please sign in to comment.