Remove event listeners with signal in web/app.js

By using the `signal` option when invoking `addEventListener`, see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#signal), we're able to remove an arbitrary number of event listeners with (effectively) a single line of code.
Besides getting rid of a bunch of `removeEventListener`-calls, which means shorter code, we no longer need to manually keep track of event-handling functions.
This commit is contained in:
Jonas Jenwald 2024-04-18 12:25:55 +02:00
parent 0788c4d918
commit 46a29ff41b

View File

@ -158,6 +158,7 @@ const PDFViewerApplication = {
baseUrl: "", baseUrl: "",
_downloadUrl: "", _downloadUrl: "",
_boundEvents: Object.create(null), _boundEvents: Object.create(null),
_windowAbortController: null,
documentInfo: null, documentInfo: null,
metadata: null, metadata: null,
_contentDispositionFilename: null, _contentDispositionFilename: null,
@ -1906,10 +1907,15 @@ const PDFViewerApplication = {
}, },
bindWindowEvents() { bindWindowEvents() {
if (this._windowAbortController) {
return;
}
this._windowAbortController = new AbortController();
const { const {
eventBus, eventBus,
_boundEvents,
appConfig: { mainContainer }, appConfig: { mainContainer },
_windowAbortController: { signal },
} = this; } = this;
function addWindowResolutionChange(evt = null) { function addWindowResolutionChange(evt = null) {
@ -1921,58 +1927,73 @@ const PDFViewerApplication = {
); );
mediaQueryList.addEventListener("change", addWindowResolutionChange, { mediaQueryList.addEventListener("change", addWindowResolutionChange, {
once: true, once: true,
signal,
}); });
_boundEvents.removeWindowResolutionChange ||= function () {
mediaQueryList.removeEventListener("change", addWindowResolutionChange);
_boundEvents.removeWindowResolutionChange = null;
};
} }
addWindowResolutionChange(); addWindowResolutionChange();
_boundEvents.windowResize = () => { window.addEventListener("visibilitychange", webViewerVisibilityChange, {
eventBus.dispatch("resize", { source: window }); signal,
}; });
_boundEvents.windowHashChange = () => { window.addEventListener("wheel", webViewerWheel, {
eventBus.dispatch("hashchange", { passive: false,
source: window, signal,
hash: document.location.hash.substring(1), });
});
};
_boundEvents.windowBeforePrint = () => {
eventBus.dispatch("beforeprint", { source: window });
};
_boundEvents.windowAfterPrint = () => {
eventBus.dispatch("afterprint", { source: window });
};
_boundEvents.windowUpdateFromSandbox = event => {
eventBus.dispatch("updatefromsandbox", {
source: window,
detail: event.detail,
});
};
window.addEventListener("visibilitychange", webViewerVisibilityChange);
window.addEventListener("wheel", webViewerWheel, { passive: false });
window.addEventListener("touchstart", webViewerTouchStart, { window.addEventListener("touchstart", webViewerTouchStart, {
passive: false, passive: false,
signal,
}); });
window.addEventListener("touchmove", webViewerTouchMove, { window.addEventListener("touchmove", webViewerTouchMove, {
passive: false, passive: false,
signal,
}); });
window.addEventListener("touchend", webViewerTouchEnd, { window.addEventListener("touchend", webViewerTouchEnd, {
passive: false, passive: false,
signal,
}); });
window.addEventListener("click", webViewerClick); window.addEventListener("click", webViewerClick, { signal });
window.addEventListener("keydown", webViewerKeyDown); window.addEventListener("keydown", webViewerKeyDown, { signal });
window.addEventListener("keyup", webViewerKeyUp); window.addEventListener("keyup", webViewerKeyUp, { signal });
window.addEventListener("resize", _boundEvents.windowResize); window.addEventListener(
window.addEventListener("hashchange", _boundEvents.windowHashChange); "resize",
window.addEventListener("beforeprint", _boundEvents.windowBeforePrint); () => {
window.addEventListener("afterprint", _boundEvents.windowAfterPrint); eventBus.dispatch("resize", { source: window });
},
{ signal }
);
window.addEventListener(
"hashchange",
() => {
eventBus.dispatch("hashchange", {
source: window,
hash: document.location.hash.substring(1),
});
},
{ signal }
);
window.addEventListener(
"beforeprint",
() => {
eventBus.dispatch("beforeprint", { source: window });
},
{ signal }
);
window.addEventListener(
"afterprint",
() => {
eventBus.dispatch("afterprint", { source: window });
},
{ signal }
);
window.addEventListener( window.addEventListener(
"updatefromsandbox", "updatefromsandbox",
_boundEvents.windowUpdateFromSandbox event => {
eventBus.dispatch("updatefromsandbox", {
source: window,
detail: event.detail,
});
},
{ signal }
); );
if ( if (
@ -1987,17 +2008,18 @@ const PDFViewerApplication = {
// TODO: remove them once the bug is fixed. // TODO: remove them once the bug is fixed.
({ scrollTop: this._lastScrollTop, scrollLeft: this._lastScrollLeft } = ({ scrollTop: this._lastScrollTop, scrollLeft: this._lastScrollLeft } =
mainContainer); mainContainer);
const scrollend = (_boundEvents.mainContainerScrollend = () => { const scrollend = () => {
({ scrollTop: this._lastScrollTop, scrollLeft: this._lastScrollLeft } = ({ scrollTop: this._lastScrollTop, scrollLeft: this._lastScrollLeft } =
mainContainer); mainContainer);
this._isScrolling = false; this._isScrolling = false;
mainContainer.addEventListener("scroll", scroll, { mainContainer.addEventListener("scroll", scroll, {
passive: true, passive: true,
signal,
}); });
mainContainer.removeEventListener("scrollend", scrollend); mainContainer.removeEventListener("scrollend", scrollend, { signal });
mainContainer.removeEventListener("blur", scrollend); mainContainer.removeEventListener("blur", scrollend, { signal });
}); };
const scroll = (_boundEvents.mainContainerScroll = () => { const scroll = () => {
if ( if (
this._isCtrlKeyDown || this._isCtrlKeyDown ||
(this._lastScrollTop === mainContainer.scrollTop && (this._lastScrollTop === mainContainer.scrollTop &&
@ -2007,13 +2029,15 @@ const PDFViewerApplication = {
} }
mainContainer.removeEventListener("scroll", scroll, { mainContainer.removeEventListener("scroll", scroll, {
passive: true, passive: true,
signal,
}); });
this._isScrolling = true; this._isScrolling = true;
mainContainer.addEventListener("scrollend", scrollend); mainContainer.addEventListener("scrollend", scrollend, { signal });
mainContainer.addEventListener("blur", scrollend); mainContainer.addEventListener("blur", scrollend, { signal });
}); };
mainContainer.addEventListener("scroll", scroll, { mainContainer.addEventListener("scroll", scroll, {
passive: true, passive: true,
signal,
}); });
}, },
@ -2077,54 +2101,8 @@ const PDFViewerApplication = {
}, },
unbindWindowEvents() { unbindWindowEvents() {
const { this._windowAbortController?.abort();
_boundEvents, this._windowAbortController = null;
appConfig: { mainContainer },
} = this;
window.removeEventListener("visibilitychange", webViewerVisibilityChange);
window.removeEventListener("wheel", webViewerWheel, { passive: false });
window.removeEventListener("touchstart", webViewerTouchStart, {
passive: false,
});
window.removeEventListener("touchmove", webViewerTouchMove, {
passive: false,
});
window.removeEventListener("touchend", webViewerTouchEnd, {
passive: false,
});
window.removeEventListener("click", webViewerClick);
window.removeEventListener("keydown", webViewerKeyDown);
window.removeEventListener("keyup", webViewerKeyUp);
window.removeEventListener("resize", _boundEvents.windowResize);
window.removeEventListener("hashchange", _boundEvents.windowHashChange);
window.removeEventListener("beforeprint", _boundEvents.windowBeforePrint);
window.removeEventListener("afterprint", _boundEvents.windowAfterPrint);
window.removeEventListener(
"updatefromsandbox",
_boundEvents.windowUpdateFromSandbox
);
mainContainer.removeEventListener(
"scroll",
_boundEvents.mainContainerScroll
);
mainContainer.removeEventListener(
"scrollend",
_boundEvents.mainContainerScrollend
);
mainContainer.removeEventListener(
"blur",
_boundEvents.mainContainerScrollend
);
_boundEvents.removeWindowResolutionChange?.();
_boundEvents.windowResize = null;
_boundEvents.windowHashChange = null;
_boundEvents.windowBeforePrint = null;
_boundEvents.windowAfterPrint = null;
_boundEvents.windowUpdateFromSandbox = null;
_boundEvents.mainContainerScroll = null;
_boundEvents.mainContainerScrollend = null;
}, },
_accumulateTicks(ticks, prop) { _accumulateTicks(ticks, prop) {