Skip to content

Commit

Permalink
Merge pull request #14388 from Snuffleupagus/bug-1746213
Browse files Browse the repository at this point in the history
Unblock the "load" event in inactive windows/tabs (bug 1746213, PR 11646 follow-up)
  • Loading branch information
Snuffleupagus authored Dec 21, 2021
2 parents c4d344b + dc4a6e9 commit 41dab8e
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ class BaseViewer {

#scrollModePageState = null;

#onVisibilityChange = null;

/**
* @param {PDFViewerOptions} options
*/
Expand Down Expand Up @@ -533,17 +535,42 @@ class BaseViewer {
// `this._onePageRenderedCapability` thus won't be resolved.
// To ensure that automatic printing, on document load, still works even in
// those cases we force-allow fetching of all pages when:
// - The current window/tab is inactive, which will prevent rendering since
// `requestAnimationFrame` is being used; fixes bug 1746213.
// - The viewer is hidden in the DOM, e.g. in a `display: none` <iframe>
// element; fixes bug 1618621.
// - The viewer is visible, but none of the pages are (e.g. if the
// viewer is very small); fixes bug 1618955.
if (
document.visibilityState === "hidden" ||
!this.container.offsetParent ||
this._getVisiblePages().views.length === 0
) {
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,
]);
}

/**
Expand Down Expand Up @@ -615,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);

Expand Down Expand Up @@ -815,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.
Expand Down

0 comments on commit 41dab8e

Please sign in to comment.