@@ -25,8 +25,7 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
2525 ...defaultOptions ,
2626 ..._options ,
2727 } ) ;
28- const resolves = { } ;
29- const rejects = { } ;
28+ const promises = { } ;
3029
3130 // Current langs, oem, and config file.
3231 // Used if the user ever re-initializes the worker using `worker.reinitialize`.
@@ -48,21 +47,12 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
4847
4948 workerCounter += 1 ;
5049
51- const setResolve = ( promiseId , res ) => {
52- resolves [ promiseId ] = res ;
53- } ;
54-
55- const setReject = ( promiseId , rej ) => {
56- rejects [ promiseId ] = rej ;
57- } ;
58-
5950 const startJob = ( { id : jobId , action, payload } ) => (
6051 new Promise ( ( resolve , reject ) => {
6152 log ( `[${ id } ]: Start ${ jobId } , action=${ action } ` ) ;
6253 // Using both `action` and `jobId` in case user provides non-unique `jobId`.
6354 const promiseId = `${ action } -${ jobId } ` ;
64- setResolve ( promiseId , resolve ) ;
65- setReject ( promiseId , reject ) ;
55+ promises [ promiseId ] = { resolve, reject } ;
6656 send ( worker , {
6757 workerId : id ,
6858 jobId,
@@ -237,9 +227,11 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
237227 } else if ( action === 'getPDF' ) {
238228 d = Array . from ( { ...data , length : Object . keys ( data ) . length } ) ;
239229 }
240- resolves [ promiseId ] ( { jobId, data : d } ) ;
230+ promises [ promiseId ] . resolve ( { jobId, data : d } ) ;
231+ delete promises [ promiseId ] ;
241232 } else if ( status === 'reject' ) {
242- rejects [ promiseId ] ( data ) ;
233+ promises [ promiseId ] . reject ( data ) ;
234+ delete promises [ promiseId ] ;
243235 if ( action === 'load' ) workerResReject ( data ) ;
244236 if ( errorHandler ) {
245237 errorHandler ( data ) ;
@@ -254,8 +246,6 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
254246 const resolveObj = {
255247 id,
256248 worker,
257- setResolve,
258- setReject,
259249 load,
260250 writeText,
261251 readText,
0 commit comments