From f9530e56dabf63c5daa4c8ef5f787271bb853580 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 19 Dec 2020 13:21:40 +0100 Subject: [PATCH] Run `AnnotationStorage.resetModified` when destroying the `PDFDocumentLoadingTask`/`PDFDocumentProxy` This will, in a very simple way using the existing events, thus allow the viewer to remove the "beforeunload" `window` event listener when the document is closed. Generally speaking we want to avoid having *global* event listeners for the PDF document instance, which is why the `EventBus` exists, and instead reserve global events for the viewer itself. However, the `AnnotationStorage` "beforeunload" event unfortunately needs to be document-specific and we should thus ensure that it's correctly removed when the document is destroyed. --- src/display/api.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/display/api.js b/src/display/api.js index ba5dd87cf..48de0601c 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -2127,6 +2127,10 @@ class WorkerTransport { this.setupMessageHandler(); } + get loadingTaskSettled() { + return this.loadingTask._capability.settled; + } + destroy() { if (this.destroyCapability) { return this.destroyCapability.promise; @@ -2154,6 +2158,18 @@ class WorkerTransport { // We also need to wait for the worker to finish its long running tasks. const terminated = this.messageHandler.sendWithPromise("Terminate", null); waitOn.push(terminated); + // Allow `AnnotationStorage`-related clean-up when destroying the document. + if (this.loadingTaskSettled) { + const annotationStorageResetModified = this.loadingTask.promise + .then(pdfDocument => { + // Avoid initializing the `annotationStorage` if it doesn't exist. + if (pdfDocument.hasOwnProperty("annotationStorage")) { + pdfDocument.annotationStorage.resetModified(); + } + }) + .catch(() => {}); + waitOn.push(annotationStorageResetModified); + } Promise.all(waitOn).then(() => { this.commonObjs.clear(); this.fontLoader.clear();