Skip to content

Commit 6e27a7f

Browse files
committed
Fallback to the XHR-like way of using fetch() and just grab all bytes as a single ArrayBuffer. Trying to see if this has the same weird problems as the chunk-by-chunk fetch.
1 parent 42e516a commit 6e27a7f

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

code/book.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ export class Book extends EventTarget {
329329
throw 'Request for book was not set in loadFromFetch()';
330330
}
331331

332+
let bytesTotal = 0;
332333
this.#needsLoading = false;
333334
this.dispatchEvent(new BookLoadingStartedEvent(this));
334335

@@ -343,42 +344,41 @@ export class Book extends EventTarget {
343344

344345
// =============================================================================================
345346
// Option 1: Readable code, fetching chunk by chunk using await.
346-
const reader = response.body.getReader();
347-
348-
/**
349-
* Reads one chunk at a time.
350-
* @returns {Promise<ArrayBuffer | null>}
351-
*/
352-
const getOneChunk = async () => {
353-
const { done, value } = await reader.read();
354-
if (!done) return value.buffer;
355-
return null;
356-
};
357-
358-
const firstChunk = await getOneChunk();
359-
if (!firstChunk) {
360-
throw `Could not get one chunk from fetch()`;
361-
}
362-
let bytesTotal = firstChunk.byteLength;
363-
364-
// Asynchronously wait for the BookBinder and its implementation to be connected.
365-
await this.#startBookBinding(this.#name, firstChunk, this.#expectedSize);
366-
367-
// Read out all subsequent chunks.
368-
/** @type {ArrayBuffer | null} */
369-
let nextChunk;
370-
while (nextChunk = await getOneChunk()) {
371-
bytesTotal += nextChunk.byteLength;
372-
this.appendBytes(nextChunk);
373-
this.#bookBinder.appendBytes(nextChunk);
374-
}
375-
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+
// }
376376

377377
// =============================================================================================
378378
// 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);
379+
const ab = await response.arrayBuffer();
380+
bytesTotal = ab.byteLength;
381+
await this.#startBookBinding(this.#name, ab, this.#expectedSize);
382382

383383
// Send out BookLoadingComplete event and return this book.
384384
this.#finishedLoading = true;

0 commit comments

Comments
 (0)