Skip to content

Unblock the "load" event in inactive windows/tabs (bug 1746213, PR 11646 follow-up) #14388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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