From 15c62cbc209f74a5026aa5f500bef7fedad21a8f Mon Sep 17 00:00:00 2001 From: Balearica Date: Mon, 12 Feb 2024 21:33:47 -0800 Subject: [PATCH] Made promises have unique identifier per #875 --- src/createWorker.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/createWorker.js b/src/createWorker.js index fbdf5de2c..a4ff45fcd 100644 --- a/src/createWorker.js +++ b/src/createWorker.js @@ -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, @@ -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; @@ -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);