From bfdb39a1e67447442d861feaf69e404084d24226 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 6 Dec 2020 12:42:56 +0100 Subject: [PATCH] Stop re-fetching the `metadata` unconditionally in `PDFViewerApplication._initializeJavaScript` We can easily avoid unnecessary API-calls here, since most of the time the `metadata` will already be available here. In the *rare* case that it's not available, we can simply wait for the existing `getMetadata`-call to resolve. --- web/app.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/web/app.js b/web/app.js index baacb1b0f..67a6ff944 100644 --- a/web/app.js +++ b/web/app.js @@ -1417,11 +1417,21 @@ const PDFViewerApplication = { } const calculationOrder = await pdfDocument.getCalculationOrderIds(); const scripting = this.externalServices.scripting; - const { - info, - metadata, - contentDispositionFilename, - } = await pdfDocument.getMetadata(); + + if (!this.documentInfo) { + // It should be *extremely* rare for metadata to not have been resolved + // when this code runs, but ensure that we handle that case here. + await new Promise(resolve => { + const metadataLoaded = () => { + this.eventBus._off("metadataloaded", metadataLoaded); + resolve(); + }; + this.eventBus._on("metadataloaded", metadataLoaded); + }); + if (pdfDocument !== this.pdfDocument) { + return; // The document was closed while the metadata resolved. + } + } window.addEventListener("updateFromSandbox", event => { const detail = event.detail; @@ -1480,7 +1490,7 @@ const PDFViewerApplication = { const dispatchEventName = generateRandomStringForSandbox(objects); const { length } = await pdfDocument.getDownloadInfo(); const filename = - contentDispositionFilename || getPDFFileNameFromURL(this.url); + this.contentDispositionFilename || getPDFFileNameFromURL(this.url); scripting.createSandbox({ objects, dispatchEventName, @@ -1490,11 +1500,11 @@ const PDFViewerApplication = { language: navigator.language, }, docInfo: { - ...info, + ...this.documentInfo, baseURL: this.baseUrl, filesize: length, filename, - metadata, + metadata: this.metadata, numPages: pdfDocument.numPages, URL: this.url, },