Skip to content

Commit

Permalink
Made promises have unique identifier per #875 (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Feb 13, 2024
1 parent f35ac0f commit c0479a1
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/createWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,21 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi

workerCounter += 1;

const setResolve = (action, res) => {
resolves[action] = res;
const setResolve = (promiseId, res) => {
resolves[promiseId] = res;
};

const setReject = (action, rej) => {
rejects[action] = rej;
const setReject = (promiseId, rej) => {
rejects[promiseId] = rej;
};

const startJob = ({ id: jobId, action, payload }) => (
new Promise((resolve, reject) => {
log(`[${id}]: Start ${jobId}, action=${action}`);
setResolve(action, resolve);
setReject(action, reject);
// Using both `action` and `jobId` in case user provides non-unique `jobId`.
const promiseId = `${action}-${jobId}`;
setResolve(promiseId, resolve);
setReject(promiseId, reject);
send(worker, {
workerId: id,
jobId,
Expand Down Expand Up @@ -226,6 +228,7 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
onMessage(worker, ({
workerId, jobId, status, action, data,
}) => {
const promiseId = `${action}-${jobId}`;
if (status === 'resolve') {
log(`[${workerId}]: Complete ${jobId}`);
let d = data;
Expand All @@ -234,9 +237,9 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
} else if (action === 'getPDF') {
d = Array.from({ ...data, length: Object.keys(data).length });
}
resolves[action]({ jobId, data: d });
resolves[promiseId]({ jobId, data: d });
} else if (status === 'reject') {
rejects[action](data);
rejects[promiseId](data);
if (action === 'load') workerResReject(data);
if (errorHandler) {
errorHandler(data);
Expand Down

0 comments on commit c0479a1

Please sign in to comment.