Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,9 @@ const PDFViewerApplication = {
this.setInitialView(hash);
})
.catch(() => {
if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while initializing.
}
// Ensure that the document is always completely initialized,
// even if there are any errors thrown above.
this.setInitialView();
Expand All @@ -1857,6 +1860,9 @@ const PDFViewerApplication = {
this._initializeAutoPrint(pdfDocument, openActionPromise);
},
reason => {
if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while the pages loaded.
}
this._documentError("pdfjs-loading-error", { message: reason.message });
}
);
Expand Down
6 changes: 5 additions & 1 deletion web/pdf_scripting_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ class PDFScriptingManager {
pdfViewer.currentPageNumber = value + 1;
break;
case "print":
await pdfViewer.pagesPromise;
try {
await pdfViewer.pagesPromise;
} catch {
break; // The document was closed, or failed to load.
Comment thread
timvandermeij marked this conversation as resolved.
Resolved
}
this.#eventBus.dispatch("print", { source: this });
break;
case "println":
Expand Down
29 changes: 26 additions & 3 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
/** @typedef {import("./l10n.js").L10n} L10n */

import {
AbortException,
AnnotationEditorType,
AnnotationEditorUIManager,
AnnotationMode,
Expand Down Expand Up @@ -915,6 +916,10 @@ class PDFViewer {
if (this.pdfDocument) {
this.eventBus.dispatch("pagesdestroy", { source: this });

// Ensure that `pagesPromise` doesn't remain pending forever, if the
// document was closed before all pages were loaded.
this._pagesCapability.reject(new AbortException("Document was closed."));

this._cancelRendering();
this._resetView();

Expand Down Expand Up @@ -1196,6 +1201,10 @@ class PDFViewer {
}
},
reason => {
if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while loading the page.
Comment thread
nicolo-ribaudo marked this conversation as resolved.
Resolved
}

console.error(
`Unable to get page ${pageNum} to initialize viewer`,
reason
Expand All @@ -1208,6 +1217,10 @@ class PDFViewer {

if (pageNum % PagesCountLimit.PAUSE_EAGER_PAGE_INIT === 0) {
await promise;

if (pdfDocument !== this.pdfDocument) {
break; // The document was closed, stop fetching more pages.
}
}
}
});
Expand All @@ -1228,6 +1241,10 @@ class PDFViewer {
}
})
.catch(reason => {
if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while the first page loaded.
}

console.error("Unable to initialize viewer", reason);

this._pagesCapability.reject(reason);
Expand Down Expand Up @@ -2139,13 +2156,17 @@ class PDFViewer {
if (pageView.pdfPage) {
return pageView.pdfPage;
}
const { pdfDocument } = this;
try {
const pdfPage = await this.pdfDocument.getPage(pageView.id);
const pdfPage = await pdfDocument.getPage(pageView.id);
if (!pageView.pdfPage) {
pageView.setPdfPage(pdfPage);
}
return pdfPage;
} catch (reason) {
if (pdfDocument !== this.pdfDocument) {
return null; // The document was closed while the page was loading.
}
console.error("Unable to get page for page view", reason);
return null; // Page error -- there is nothing that can be done.
}
Expand Down Expand Up @@ -2195,8 +2216,10 @@ class PDFViewer {
);

if (pageView) {
this.#ensurePdfPageLoaded(pageView).then(() => {
this.renderingQueue.renderView(pageView);
this.#ensurePdfPageLoaded(pageView).then(pdfPage => {
if (pdfPage !== null) {
this.renderingQueue.renderView(pageView);
}
});
return true;
}
Expand Down
Loading