From dc4a6e94f347ebecf2701cbbc617c59f53ea61ff Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 18 Dec 2021 21:25:31 +0100 Subject: [PATCH] Unblock the "load" event when the windows/tabs becomes inactive (bug 1746213) *This addresses the following case missing from the previous patch:* The viewer is loaded in an *active* window/tab, and enough time is allowed to pass in order to allow rendering to start. However, if the user then switches to another tab (or another program) *before* rendering has finished, the "load" event also needs to be unblocked. --- web/base_viewer.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/web/base_viewer.js b/web/base_viewer.js index 1f26fe2fd..757d65d12 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -207,6 +207,8 @@ class BaseViewer { #scrollModePageState = null; + #onVisibilityChange = null; + /** * @param {PDFViewerOptions} options */ @@ -546,7 +548,29 @@ class BaseViewer { ) { return Promise.resolve(); } - return this._onePageRenderedCapability.promise; + + // Handle the window/tab becoming inactive *after* rendering has started; + // fixes (another part of) bug 1746213. + const visibilityChangePromise = new Promise(resolve => { + this.#onVisibilityChange = () => { + if (document.visibilityState !== "hidden") { + return; + } + resolve(); + + document.removeEventListener( + "visibilitychange", + this.#onVisibilityChange + ); + this.#onVisibilityChange = null; + }; + document.addEventListener("visibilitychange", this.#onVisibilityChange); + }); + + return Promise.race([ + this._onePageRenderedCapability.promise, + visibilityChangePromise, + ]); } /** @@ -618,6 +642,14 @@ class BaseViewer { this.eventBus._off("pagerendered", this._onAfterDraw); this._onAfterDraw = null; + + if (this.#onVisibilityChange) { + document.removeEventListener( + "visibilitychange", + this.#onVisibilityChange + ); + this.#onVisibilityChange = null; + } }; this.eventBus._on("pagerendered", this._onAfterDraw); @@ -818,6 +850,13 @@ class BaseViewer { this.eventBus._off("pagerendered", this._onAfterDraw); this._onAfterDraw = null; } + if (this.#onVisibilityChange) { + document.removeEventListener( + "visibilitychange", + this.#onVisibilityChange + ); + this.#onVisibilityChange = null; + } // Remove the pages from the DOM... this.viewer.textContent = ""; // ... and reset the Scroll mode CSS class(es) afterwards.