diff --git a/src/display/api.js b/src/display/api.js index 1a291ae8e..055a9e985 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1140,12 +1140,9 @@ class PDFPageProxy { * {Object} with JS actions. */ getJSActions() { - if (!this._jsActionsPromise) { - this._jsActionsPromise = this._transport.getPageJSActions( - this._pageIndex - ); - } - return this._jsActionsPromise; + return (this._jsActionsPromise ||= this._transport.getPageJSActions( + this._pageIndex + )); } /** diff --git a/web/app.js b/web/app.js index e36e5f1a8..45e156734 100644 --- a/web/app.js +++ b/web/app.js @@ -1564,24 +1564,15 @@ const PDFViewerApplication = { internalEvents.set("updatefromsandbox", updateFromSandbox); const visitedPages = new Map(); - const pageOpen = ({ pageNumber }) => { + const pageOpen = ({ pageNumber, actionsPromise }) => { visitedPages.set( pageNumber, (async () => { // Avoid sending, and thus serializing, the `actions` data - // when the same page is open several times. + // when the same page is opened several times. let actions = null; if (!visitedPages.has(pageNumber)) { - // visitedPages doesn't contain pageNumber: first visit. - - const pageView = this.pdfViewer.getPageView( - /* index = */ pageNumber - 1 - ); - if (pageView?.pdfPage) { - actions = await pageView.pdfPage.getJSActions(); - } else { - actions = await pdfDocument.getPage(pageNumber).getJSActions(); - } + actions = await actionsPromise; if (pdfDocument !== this.pdfDocument) { return; // The document was closed while the actions resolved. @@ -1599,14 +1590,15 @@ const PDFViewerApplication = { }; const pageClose = async ({ pageNumber }) => { - const promise = visitedPages.get(pageNumber); - if (!promise) { + const actionsPromise = visitedPages.get(pageNumber); + if (!actionsPromise) { + // Ensure that the "pageclose" event was preceded by a "pageopen" event. return; } visitedPages.set(pageNumber, null); - // Wait for PageOpen has been sent. - await promise; + // Ensure that the "pageopen" event is handled first. + await actionsPromise; if (pdfDocument !== this.pdfDocument) { return; // The document was closed while the actions resolved. diff --git a/web/base_viewer.js b/web/base_viewer.js index c9935ec1b..4060145c9 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -1526,7 +1526,11 @@ class BaseViewer { if (pageView?.renderingState === RenderingStates.FINISHED) { pageOpenPendingSet.delete(pageNumber); - eventBus.dispatch("pageopen", { source: this, pageNumber }); + eventBus.dispatch("pageopen", { + source: this, + pageNumber, + actionsPromise: pageView.pdfPage?.getJSActions(), + }); } else { pageOpenPendingSet.add(pageNumber); }