@@ -342,48 +342,56 @@ export class Book extends EventTarget {
342342 throw e ;
343343 }
344344
345- // =============================================================================================
346- // Option 1: Readable code, fetching chunk by chunk using await.
347- // const reader = response.body.getReader();
348-
349- // /**
350- // * Reads one chunk at a time.
351- // * @returns {Promise<ArrayBuffer | null> }
352- // */
353- // const getOneChunk = async () => {
354- // const { done, value } = await reader.read();
355- // if (!done) return value.buffer;
356- // return null;
357- // };
358-
359- // const firstChunk = await getOneChunk();
360- // if (!firstChunk) {
361- // throw `Could not get one chunk from fetch()`;
362- // }
363- // bytesTotal = firstChunk.byteLength;
364-
365- // // Asynchronously wait for the BookBinder and its implementation to be connected.
366- // await this.#startBookBinding(this.#name, firstChunk, this.#expectedSize);
367-
368- // // Read out all subsequent chunks.
369- // /** @type {ArrayBuffer | null } */
370- // let nextChunk;
371- // while (nextChunk = await getOneChunk()) {
372- // bytesTotal += nextChunk.byteLength;
373- // this.appendBytes(nextChunk);
374- // this.#bookBinder.appendBytes(nextChunk);
375- // }
376-
377- // =============================================================================================
378- // Option 2: The XHR way (grab all bytes and only then start book binding).
379- const ab = await response . arrayBuffer ( ) ;
380- bytesTotal = ab . byteLength ;
381- await this . #startBookBinding( this . #name, ab , this . #expectedSize) ;
345+ if ( Params [ 'fetchMode' ] === 'chunkByChunk' ) {
346+ // =============================================================================================
347+ // Option 1: Readable code, fetching chunk by chunk using await.
348+ const reader = response . body . getReader ( ) ;
349+ let numChunks = 0 ;
350+
351+ /**
352+ * Reads one chunk at a time.
353+ * @returns {Promise<ArrayBuffer | null> }
354+ */
355+ const getOneChunk = async ( ) => {
356+ const { done, value } = await reader . read ( ) ;
357+ if ( ! done ) {
358+ numChunks ++ ;
359+ console . log ( `debugFetch: Received chunk #${ numChunks } of ${ value . byteLength } bytes` ) ;
360+ return value . buffer ;
361+ }
362+ return null ;
363+ } ;
364+
365+ const firstChunk = await getOneChunk ( ) ;
366+ if ( ! firstChunk ) {
367+ throw `Could not get one chunk from fetch()` ;
368+ }
369+ bytesTotal = firstChunk . byteLength ;
370+
371+ // Asynchronously wait for the BookBinder and its implementation to be connected.
372+ await this . #startBookBinding( this . #name, firstChunk , this . #expectedSize) ;
373+ console . log ( `debugFetch: Instantiated the BookBinder` ) ;
374+
375+ // Read out all subsequent chunks.
376+ /** @type {ArrayBuffer | null } */
377+ let nextChunk ;
378+ while ( nextChunk = await getOneChunk ( ) ) {
379+ bytesTotal += nextChunk . byteLength ;
380+ this . appendBytes ( nextChunk ) ;
381+ this . #bookBinder. appendBytes ( nextChunk ) ;
382+ }
383+ } else {
384+ // =============================================================================================
385+ // Option 2: The XHR way (grab all bytes and only then start book binding).
386+ const ab = await response . arrayBuffer ( ) ;
387+ bytesTotal = ab . byteLength ;
388+ await this . #startBookBinding( this . #name, ab , this . #expectedSize) ;
389+ }
382390
383391 // Send out BookLoadingComplete event and return this book.
384392 this . #finishedLoading = true ;
385393 this . dispatchEvent ( new BookLoadingCompleteEvent ( this ) ) ;
386- if ( Params [ 'debugFetch' ] === 'true' ) {
394+ if ( Params [ 'fetchMode' ] ) {
387395 console . log ( `debugFetch: ArrayBuffers were total length ${ bytesTotal } ` ) ;
388396 }
389397
@@ -553,7 +561,7 @@ export class Book extends EventTarget {
553561 } ) ;
554562
555563 bookBinder . addEventListener ( BookEventType . PAGE_EXTRACTED , evt => {
556- if ( Params [ 'debugFetch' ] === 'true' ) {
564+ if ( Params [ 'fetchMode' ] ) {
557565 console . log ( `debugFetch: Page #${ this . #pages. length + 1 } extracted` ) ;
558566 }
559567 this . #pages. push ( evt . page ) ;
@@ -567,7 +575,7 @@ export class Book extends EventTarget {
567575 this . dispatchEvent ( new BookProgressEvent ( this , evt . totalPages , evt . message ) ) ;
568576 } ) ;
569577
570- if ( Params [ 'debugFetch' ] === 'true' ) {
578+ if ( Params [ 'fetchMode' ] ) {
571579 console . log ( `debugFetch: Calling BookBinder.start()` ) ;
572580 }
573581 // Wait for its decompressing implementation to be loaded and ports connected.
0 commit comments