Skip to content

Commit

Permalink
Remove the temporary "visibilitychange" listener, in PDFViewer, wit…
Browse files Browse the repository at this point in the history
…h `AbortSignal.any()`

This is similar to a lot of other code, where we've been replacing explicit `removeEventListener`-calls.
Given the somewhat limited availability of `AbortSignal.any()`, see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/any_static#browser_compatibility), this event listener will no longer be immediately removed in older browsers (however that should be fine).
  • Loading branch information
Snuffleupagus committed Oct 8, 2024
1 parent 81cf42d commit 31b0a49
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,22 +673,29 @@ class PDFViewer {

// Handle the window/tab becoming inactive *after* rendering has started;
// fixes (another part of) bug 1746213.
const hiddenCapability = Promise.withResolvers();
function onVisibilityChange() {
if (document.visibilityState === "hidden") {
hiddenCapability.resolve();
const hiddenCapability = Promise.withResolvers(),
ac = new AbortController();
document.addEventListener(
"visibilitychange",
() => {
if (document.visibilityState === "hidden") {
hiddenCapability.resolve();
}
},
{
signal:
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
typeof AbortSignal.any === "function"
? AbortSignal.any([signal, ac.signal])
: signal,
}
}
document.addEventListener("visibilitychange", onVisibilityChange, {
signal,
});
);

await Promise.race([
this._onePageRenderedCapability.promise,
hiddenCapability.promise,
]);
// Ensure that the "visibilitychange" listener is removed immediately.
document.removeEventListener("visibilitychange", onVisibilityChange);
ac.abort(); // Remove the "visibilitychange" listener immediately.
}

async getAllText() {
Expand Down

0 comments on commit 31b0a49

Please sign in to comment.