@@ -222,13 +222,13 @@ class WorkerMessageHandler {
222222 const pdfStream = new PDFWorkerStream ( { msgHandler : handler } ) ,
223223 fullReader = pdfStream . getFullReader ( ) ;
224224
225- const pdfManagerCapability = Promise . withResolvers ( ) ;
225+ const { promise , resolve , reject } = Promise . withResolvers ( ) ;
226226 let newPdfManager ,
227- cachedChunks = [ ] ,
228- loaded = 0 ;
227+ cachedChunks = [ ] ;
228+ cancelXHRs = reason => pdfStream . cancelAllRequests ( reason ) ;
229229
230230 fullReader . headersReady
231- . then ( function ( ) {
231+ . then ( ( ) => {
232232 if ( ! fullReader . isRangeSupported ) {
233233 return ;
234234 }
@@ -239,77 +239,73 @@ class WorkerMessageHandler {
239239
240240 newPdfManager = new NetworkPdfManager ( pdfManagerArgs ) ;
241241 // There may be a chance that `newPdfManager` is not initialized for
242- // the first few runs of `readchunk` block of code. Be sure to send
242+ // the first few iterations of the `readData` code. Be sure to send
243243 // all cached chunks, if any, to chunked_stream via pdf_manager.
244244 for ( const chunk of cachedChunks ) {
245245 newPdfManager . sendProgressiveData ( chunk ) ;
246246 }
247+ cachedChunks = null ;
247248
248- cachedChunks = [ ] ;
249- pdfManagerCapability . resolve ( newPdfManager ) ;
249+ resolve ( newPdfManager ) ;
250250 cancelXHRs = null ;
251251 } )
252- . catch ( function ( reason ) {
253- pdfManagerCapability . reject ( reason ) ;
252+ . catch ( reason => {
253+ reject ( reason ) ;
254254 cancelXHRs = null ;
255255 } ) ;
256256
257- new Promise ( function ( resolve , reject ) {
258- const readChunk = function ( { value, done } ) {
259- try {
260- ensureNotTerminated ( ) ;
261- if ( done ) {
262- if ( ! newPdfManager ) {
263- const pdfFile = arrayBuffersToBytes ( cachedChunks ) ;
264- cachedChunks = [ ] ;
257+ async function readData ( ) {
258+ let loaded = 0 ;
265259
266- if ( length && pdfFile . length !== length ) {
267- warn ( "reported HTTP length is different from actual" ) ;
268- }
269- pdfManagerArgs . source = pdfFile ;
260+ while ( true ) {
261+ const { value, done } = await fullReader . read ( ) ;
262+ ensureNotTerminated ( ) ;
270263
271- newPdfManager = new LocalPdfManager ( pdfManagerArgs ) ;
272- pdfManagerCapability . resolve ( newPdfManager ) ;
273- }
274- cancelXHRs = null ;
275- return ;
276- }
277- if ( typeof PDFJSDev === "undefined" || PDFJSDev . test ( "TESTING" ) ) {
278- assert (
279- value instanceof ArrayBuffer ,
280- "readChunk (getPdfManager) - expected an ArrayBuffer."
281- ) ;
282- }
283- loaded += value . byteLength ;
264+ if ( done ) {
265+ break ;
266+ }
267+ if ( typeof PDFJSDev === "undefined" || PDFJSDev . test ( "TESTING" ) ) {
268+ assert (
269+ value instanceof ArrayBuffer ,
270+ "readData (getPdfManager) - expected an ArrayBuffer."
271+ ) ;
272+ }
273+ loaded += value . byteLength ;
284274
285- if ( ! fullReader . isStreamingSupported ) {
286- handler . send ( "DocProgress" , {
287- loaded,
288- total : Math . max ( loaded , fullReader . contentLength || 0 ) ,
289- } ) ;
290- }
275+ if ( ! fullReader . isStreamingSupported ) {
276+ handler . send ( "DocProgress" , {
277+ loaded,
278+ total : Math . max ( loaded , fullReader . contentLength || 0 ) ,
279+ } ) ;
280+ }
291281
292- if ( newPdfManager ) {
293- newPdfManager . sendProgressiveData ( value ) ;
294- } else {
295- cachedChunks . push ( value ) ;
296- }
297- fullReader . read ( ) . then ( readChunk , reject ) ;
298- } catch ( e ) {
299- reject ( e ) ;
282+ if ( newPdfManager ) {
283+ newPdfManager . sendProgressiveData ( value ) ;
284+ } else {
285+ cachedChunks . push ( value ) ;
286+ }
287+ }
288+
289+ if ( ! newPdfManager ) {
290+ const pdfFile = arrayBuffersToBytes ( cachedChunks ) ;
291+ cachedChunks = null ;
292+
293+ if ( length && pdfFile . length !== length ) {
294+ warn ( "reported HTTP length is different from actual" ) ;
300295 }
301- } ;
302- fullReader . read ( ) . then ( readChunk , reject ) ;
303- } ) . catch ( function ( e ) {
304- pdfManagerCapability . reject ( e ) ;
296+ pdfManagerArgs . source = pdfFile ;
297+
298+ newPdfManager = new LocalPdfManager ( pdfManagerArgs ) ;
299+ resolve ( newPdfManager ) ;
300+ }
301+ cancelXHRs = null ;
302+ }
303+ readData ( ) . catch ( reason => {
304+ reject ( reason ) ;
305305 cancelXHRs = null ;
306306 } ) ;
307307
308- cancelXHRs = reason => {
309- pdfStream . cancelAllRequests ( reason ) ;
310- } ;
311-
312- return pdfManagerCapability . promise ;
308+ return promise ;
313309 }
314310
315311 function setupDoc ( data ) {
@@ -960,7 +956,7 @@ class WorkerMessageHandler {
960956 return pdfManager . cleanup ( /* manuallyTriggered = */ true ) ;
961957 } ) ;
962958
963- handler . on ( "Terminate" , function ( data ) {
959+ handler . on ( "Terminate" , async function ( data ) {
964960 terminated = true ;
965961
966962 const waitOn = [ ] ;
@@ -981,12 +977,11 @@ class WorkerMessageHandler {
981977 task . terminate ( ) ;
982978 }
983979
984- return Promise . all ( waitOn ) . then ( function ( ) {
985- // Notice that even if we destroying handler, resolved response promise
986- // must be sent back.
987- handler . destroy ( ) ;
988- handler = null ;
989- } ) ;
980+ await Promise . all ( waitOn ) ;
981+ // Notice that even if we destroying handler, resolved response promise
982+ // must be sent back.
983+ handler . destroy ( ) ;
984+ handler = null ;
990985 } ) ;
991986
992987 handler . on ( "Ready" , function ( data ) {
0 commit comments