Skip to content

Commit 8d30712

Browse files
committed
Remove most debugging messages. Change debugFetch param into fetchMode=chunkByChunk to test it
1 parent 6e27a7f commit 8d30712

File tree

5 files changed

+53
-53
lines changed

5 files changed

+53
-53
lines changed

code/bitjs/archive/common.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export async function getConnectedPort(implFilename) {
3636
const messageChannel = new MessageChannel();
3737
const hostPort = messageChannel.port1;
3838
const implPort = messageChannel.port2;
39-
console.log(`debugFetch: Connected host to implementation with ports`);
4039

4140
if (typeof Worker === 'undefined') {
4241
const implModule = await import(`${implFilename}`);
@@ -51,7 +50,6 @@ export async function getConnectedPort(implFilename) {
5150
const workerScriptPath = new URL(`./webworker-wrapper.js`, import.meta.url).href;
5251
const worker = new Worker(workerScriptPath, { type: 'module' });
5352
worker.onmessage = () => {
54-
console.log(`debugFetch: Got the connected event from the worker`);
5553
resolve({
5654
hostPort,
5755
disconnectFn: () => worker.postMessage({ disconnect: true }),

code/bitjs/archive/unzip.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,6 @@ export function connect(port) {
789789

790790
hostPort = port;
791791
port.onmessage = onmessage;
792-
// TODO: kthoom change for debugging.
793-
console.log(`debugFetch: Connected host to unzip implementation`);
794792
}
795793

796794
export function disconnect() {

code/bitjs/archive/webworker-wrapper.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ let implPort;
1818
let module;
1919

2020
onmessage = async (evt) => {
21-
console.log(`debugFetch: Got a message inside webworker-wrapper with implSrc: ${evt.data.implSrc}`);
2221
if (evt.data.implSrc) {
2322
module = await import(evt.data.implSrc);
2423
module.connect(evt.ports[0]);

code/book.js

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

code/epub/epub-book-binder.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class EPUBBookBinder extends BookBinder {
5050
constructor(filenameOrUri, ab, totalExpectedSize) {
5151
super(filenameOrUri, ab, totalExpectedSize);
5252

53-
if (Params['debugFetch'] === 'true') {
53+
if (Params['fetchMode']) {
5454
console.log(`debugFetch: Constructing a EPUBBookBinder for ${filenameOrUri}`);
5555
}
5656

@@ -85,24 +85,21 @@ export class EPUBBookBinder extends BookBinder {
8585

8686
/** @override */
8787
beforeStart_() {
88-
if (Params['debugFetch'] === 'true') {
89-
console.log(`EPubBookBinder.beforeStart_()`);
90-
}
9188
let firstFile = true;
9289
let numExtractions = 0;
9390
this.unarchiver.onExtract(evt => {
9491
numExtractions++;
9592
/** @type {import('../bitjs/archive/decompress.js').UnarchivedFile} */
9693
const theFile = evt.unarchivedFile;
97-
if (Params['debugFetch'] === 'true' && this.fileMap_.has(theFile.filename)) {
94+
if (this.fileMap_.has(theFile.filename)) {
9895
// TODO: How does it get multiple extract events for the same file?
99-
console.error(`debugFetch: Received an EXTRACT event for ${theFile.filename}, but already have that file!`);
96+
console.error(`TODO: Received an EXTRACT event for ${theFile.filename}, but already have that file!`);
10097
return;
10198
}
10299

103100
// This is a new file. Add it to the map.
104101
this.fileMap_.set(theFile.filename, theFile.fileData);
105-
if (Params['debugFetch'] === 'true') {
102+
if (Params['fetchMode']) {
106103
console.log(`debugFetch: Extracted file ${theFile.filename} of size ${theFile.fileData.byteLength}`);
107104
}
108105

@@ -113,7 +110,7 @@ export class EPUBBookBinder extends BookBinder {
113110
}
114111
});
115112
this.unarchiver.addEventListener(UnarchiveEventType.FINISH, evt => {
116-
if (Params['debugFetch'] === 'true') {
113+
if (Params['fetchMode']) {
117114
console.log(`debugFetch: Received UnarchiveEventType.FINISH event with ${numExtractions} extractions`);
118115
}
119116

0 commit comments

Comments
 (0)