Skip to content

Commit

Permalink
Add some logging to debug race condition with fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
codedread committed Jul 3, 2024
1 parent ab98fc1 commit 632d197
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions code/bitjs/archive/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function getConnectedPort(implFilename) {
const messageChannel = new MessageChannel();
const hostPort = messageChannel.port1;
const implPort = messageChannel.port2;
console.log(`debugFetch: Connected host to implementation with ports`);

if (typeof Worker === 'undefined') {
const implModule = await import(`${implFilename}`);
Expand All @@ -49,6 +50,9 @@ export async function getConnectedPort(implFilename) {
return new Promise((resolve, reject) => {
const workerScriptPath = new URL(`./webworker-wrapper.js`, import.meta.url).href;
const worker = new Worker(workerScriptPath, { type: 'module' });
worker.addEventListener('connected', () => {
console.log(`debugFetch: Got the connected event from the worker`);
});
worker.postMessage({ implSrc: implFilename }, [implPort]);
resolve({
hostPort,
Expand Down
15 changes: 15 additions & 0 deletions code/bitjs/archive/unzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ export function connect(port) {

hostPort = port;
port.onmessage = onmessage;
console.log(`debugFetch: Connected host to unzip implementation`);
}

export function disconnect() {
Expand All @@ -811,3 +812,17 @@ export function disconnect() {
totalUncompressedBytesInArchive = 0;
totalFilesInArchive = 0;
}

/*
book-binder.js:172 unarchiving done in 0.292s
comic-book-binder.js:105 number of pages = 0
book-viewer.js:263 updateLayout() before current page is loaded
unzip.js:773 Found an error while unzipping
onmessage @ unzip.js:773Understand this error
unzip.js:774 TypeError: Cannot read properties of null (reading 'postMessage')
at archiveUnzip (https://codedread.github.io/kthoom/code/bitjs/archive/unzip.js:634:18)
at async MessagePort.onmessage (https://codedread.github.io/kthoom/code/bitjs/archive/unzip.js:767:7)
unzip.js:634 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'postMessage')
at archiveUnzip (unzip.js:634:18)
at async MessagePort.onmessage (unzip.js:767:7)Understand this error
*/
2 changes: 2 additions & 0 deletions code/bitjs/archive/webworker-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ let implPort;
let module;

onmessage = async (evt) => {
console.log(`debugFetch: Got a message inside webworker-wrapper with implSrc: ${evt.data.implSrc}`);
if (evt.data.implSrc) {
module = await import(evt.data.implSrc);
module.connect(evt.ports[0]);
postMessage({type: 'connected'});
} else if (evt.data.disconnect) {
module.disconnect();
}
Expand Down
12 changes: 12 additions & 0 deletions code/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,12 @@ export class Book extends EventTarget {
const reader = response.body.getReader();
const readAndProcessNextChunk = () => {
reader.read().then(({ done, value }) => {
if (Params['debugFetch']) {
let str = `debugFetch: readAndProcessNextChunk(), `;
if (done) { str += 'done'; }
else { str += `buffer.byteLength=${value.buffer.byteLength}`; }
console.log(str);
}
if (!done) {
// value is a chunk of the file as a Uint8Array.
if (!this.#bookBinder) {
Expand Down Expand Up @@ -489,6 +495,9 @@ export class Book extends EventTarget {
#startBookBinding(fileNameOrUri, ab, totalExpectedSize) {
this.#arrayBuffer = ab;
return createBookBinderAsync(fileNameOrUri, ab, totalExpectedSize).then(bookBinder => {
if (Params['debugFetch']) {
console.log(`debugFetch: Book Binder created`);
}
this.#bookBinder = bookBinder;
this.#bookMetadata = createEmptyMetadata(bookBinder.getBookType());

Expand All @@ -506,6 +515,9 @@ export class Book extends EventTarget {
});

this.#bookBinder.addEventListener(BookEventType.PAGE_EXTRACTED, evt => {
if (Params['debugFetch']) {
console.log(`debugFetch: Page #${this.#pages.length+1} extracted`);
}
this.#pages.push(evt.page);
this.dispatchEvent(new BookPageExtractedEvent(this, evt.page, evt.pageNum));
});
Expand Down

0 comments on commit 632d197

Please sign in to comment.