Suppress more errors due to closed documents

Closing/switching the document in the viewer will cause multiple
tasks to fail. Other code paths already have
`if (pdfDocument !== this.pdfDocument) return;` checks to know if the
document has been changed after an async step, but it was missing in
some places leading to errors being logged in the console.

This commit adds the check in those places.
This commit is contained in:
Nicolò Ribaudo
2026-11-24 15:57:14 +01:00
parent 32fb05b3b7
commit b982ac42e4
+21 -3
View File
@@ -1195,6 +1195,10 @@ class PDFViewer {
}
},
reason => {
if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while loading the page.
}
console.error(
`Unable to get page ${pageNum} to initialize viewer`,
reason
@@ -1207,6 +1211,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.
}
}
}
});
@@ -1230,6 +1238,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);
@@ -2137,13 +2149,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.
}
@@ -2193,8 +2209,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;
}