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.
This commit is contained in:
Jonas Jenwald 2021-12-18 21:25:31 +01:00
parent 472bbf4592
commit dc4a6e94f3

View File

@ -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.