Merge pull request #12820 from Snuffleupagus/pageclose-skip

Don't dispatch "pageclose" events if a "pageopen" wasn't dispatched for the page (PR 12747 follow-up)
This commit is contained in:
Tim van der Meij 2021-01-07 23:58:23 +01:00 committed by GitHub
commit 35845d1bbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -654,7 +654,7 @@ class BaseViewer {
this._pagesCapability = createPromiseCapability(); this._pagesCapability = createPromiseCapability();
this._scrollMode = ScrollMode.VERTICAL; this._scrollMode = ScrollMode.VERTICAL;
this._spreadMode = SpreadMode.NONE; this._spreadMode = SpreadMode.NONE;
this._pageOpenPendingSet = null; this._pageOpenPendingSet?.clear();
if (this._onBeforeDraw) { if (this._onBeforeDraw) {
this.eventBus._off("pagerender", this._onBeforeDraw); this.eventBus._off("pagerender", this._onBeforeDraw);
@ -1514,22 +1514,23 @@ class BaseViewer {
if (!this.enableScripting) { if (!this.enableScripting) {
return; return;
} }
const { eventBus } = this; const eventBus = this.eventBus,
pageOpenPendingSet = (this._pageOpenPendingSet ||= new Set());
const dispatchPageClose = pageNumber => { const dispatchPageClose = pageNumber => {
if (pageOpenPendingSet.has(pageNumber)) {
return; // No "pageopen" event was dispatched for the previous page.
}
eventBus.dispatch("pageclose", { source: this, pageNumber }); eventBus.dispatch("pageclose", { source: this, pageNumber });
}; };
const dispatchPageOpen = (pageNumber, force = false) => { const dispatchPageOpen = (pageNumber, force = false) => {
const pageView = this._pages[pageNumber - 1]; const pageView = this._pages[pageNumber - 1];
if (force || pageView?.renderingState === RenderingStates.FINISHED) { if (force || pageView?.renderingState === RenderingStates.FINISHED) {
this._pageOpenPendingSet?.delete(pageNumber); pageOpenPendingSet.delete(pageNumber);
eventBus.dispatch("pageopen", { source: this, pageNumber }); eventBus.dispatch("pageopen", { source: this, pageNumber });
} else { } else {
if (!this._pageOpenPendingSet) { pageOpenPendingSet.add(pageNumber);
this._pageOpenPendingSet = new Set();
}
this._pageOpenPendingSet.add(pageNumber);
} }
}; };
@ -1542,10 +1543,7 @@ class BaseViewer {
}); });
eventBus._on("pagerendered", ({ pageNumber }) => { eventBus._on("pagerendered", ({ pageNumber }) => {
if (!this._pageOpenPendingSet) { if (!pageOpenPendingSet.has(pageNumber)) {
return; // No pending "pageopen" events.
}
if (!this._pageOpenPendingSet.has(pageNumber)) {
return; // No pending "pageopen" event for the newly rendered page. return; // No pending "pageopen" event for the newly rendered page.
} }
if (pageNumber !== this._currentPageNumber) { if (pageNumber !== this._currentPageNumber) {