From 05b0798824aa9686b8a878ee9ccf691b04d57108 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 10 Apr 2021 12:46:45 +0200 Subject: [PATCH] Check that the correct `pdfDocument` is still active, before rendering the outline/attachments/layers *This patch fixes some technical debt in the viewer.* Given that most API methods are (purposely) asynchronous, there's always a risk that the viewer could have been `close`d before the requested data arrives. Lately we've started to check this case before using the data, to prevent errors and/or inconsistent state, however the outline/attachments/layers fetching and rendering is old enough that it pre-dates those checks. --- web/app.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/web/app.js b/web/app.js index 27dc2935f..7bc296bd4 100644 --- a/web/app.js +++ b/web/app.js @@ -1383,14 +1383,23 @@ const PDFViewerApplication = { onePageRendered.then(() => { pdfDocument.getOutline().then(outline => { + if (pdfDocument !== this.pdfDocument) { + return; // The document was closed while the outline resolved. + } this.pdfOutlineViewer.render({ outline, pdfDocument }); }); pdfDocument.getAttachments().then(attachments => { + if (pdfDocument !== this.pdfDocument) { + return; // The document was closed while the attachments resolved. + } this.pdfAttachmentViewer.render({ attachments }); }); // Ensure that the layers accurately reflects the current state in the // viewer itself, rather than the default state provided by the API. pdfViewer.optionalContentConfigPromise.then(optionalContentConfig => { + if (pdfDocument !== this.pdfDocument) { + return; // The document was closed while the layers resolved. + } this.pdfLayerViewer.render({ optionalContentConfig, pdfDocument }); }); if (