@@ -25,8 +25,7 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
25
25
...defaultOptions ,
26
26
..._options ,
27
27
} ) ;
28
- const resolves = { } ;
29
- const rejects = { } ;
28
+ const promises = { } ;
30
29
31
30
// Current langs, oem, and config file.
32
31
// 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
48
47
49
48
workerCounter += 1 ;
50
49
51
- const setResolve = ( promiseId , res ) => {
52
- resolves [ promiseId ] = res ;
53
- } ;
54
-
55
- const setReject = ( promiseId , rej ) => {
56
- rejects [ promiseId ] = rej ;
57
- } ;
58
-
59
50
const startJob = ( { id : jobId , action, payload } ) => (
60
51
new Promise ( ( resolve , reject ) => {
61
52
log ( `[${ id } ]: Start ${ jobId } , action=${ action } ` ) ;
62
53
// Using both `action` and `jobId` in case user provides non-unique `jobId`.
63
54
const promiseId = `${ action } -${ jobId } ` ;
64
- setResolve ( promiseId , resolve ) ;
65
- setReject ( promiseId , reject ) ;
55
+ promises [ promiseId ] = { resolve, reject } ;
66
56
send ( worker , {
67
57
workerId : id ,
68
58
jobId,
@@ -237,9 +227,11 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
237
227
} else if ( action === 'getPDF' ) {
238
228
d = Array . from ( { ...data , length : Object . keys ( data ) . length } ) ;
239
229
}
240
- resolves [ promiseId ] ( { jobId, data : d } ) ;
230
+ promises [ promiseId ] . resolve ( { jobId, data : d } ) ;
231
+ delete promises [ promiseId ] ;
241
232
} else if ( status === 'reject' ) {
242
- rejects [ promiseId ] ( data ) ;
233
+ promises [ promiseId ] . reject ( data ) ;
234
+ delete promises [ promiseId ] ;
243
235
if ( action === 'load' ) workerResReject ( data ) ;
244
236
if ( errorHandler ) {
245
237
errorHandler ( data ) ;
@@ -254,8 +246,6 @@ module.exports = async (langs = 'eng', oem = OEM.LSTM_ONLY, _options = {}, confi
254
246
const resolveObj = {
255
247
id,
256
248
worker,
257
- setResolve,
258
- setReject,
259
249
load,
260
250
writeText,
261
251
readText,
0 commit comments