mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-19 06:38:07 +02:00
Remove event listeners with signal
in web/pdf_presentation_mode.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:
parent
4866686749
commit
0788c4d918
1 changed files with 50 additions and 43 deletions
|
@ -49,6 +49,10 @@ class PDFPresentationMode {
|
||||||
|
|
||||||
#args = null;
|
#args = null;
|
||||||
|
|
||||||
|
#fullscreenChangeAbortController = null;
|
||||||
|
|
||||||
|
#windowAbortController = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {PDFPresentationModeOptions} options
|
* @param {PDFPresentationModeOptions} options
|
||||||
*/
|
*/
|
||||||
|
@ -346,59 +350,62 @@ class PDFPresentationMode {
|
||||||
}
|
}
|
||||||
|
|
||||||
#addWindowListeners() {
|
#addWindowListeners() {
|
||||||
this.showControlsBind = this.#showControls.bind(this);
|
if (this.#windowAbortController) {
|
||||||
this.mouseDownBind = this.#mouseDown.bind(this);
|
return;
|
||||||
this.mouseWheelBind = this.#mouseWheel.bind(this);
|
}
|
||||||
this.resetMouseScrollStateBind = this.#resetMouseScrollState.bind(this);
|
this.#windowAbortController = new AbortController();
|
||||||
this.contextMenuBind = this.#contextMenu.bind(this);
|
const { signal } = this.#windowAbortController;
|
||||||
this.touchSwipeBind = this.#touchSwipe.bind(this);
|
|
||||||
|
|
||||||
window.addEventListener("mousemove", this.showControlsBind);
|
const touchSwipeBind = this.#touchSwipe.bind(this);
|
||||||
window.addEventListener("mousedown", this.mouseDownBind);
|
|
||||||
window.addEventListener("wheel", this.mouseWheelBind, { passive: false });
|
window.addEventListener("mousemove", this.#showControls.bind(this), {
|
||||||
window.addEventListener("keydown", this.resetMouseScrollStateBind);
|
signal,
|
||||||
window.addEventListener("contextmenu", this.contextMenuBind);
|
});
|
||||||
window.addEventListener("touchstart", this.touchSwipeBind);
|
window.addEventListener("mousedown", this.#mouseDown.bind(this), {
|
||||||
window.addEventListener("touchmove", this.touchSwipeBind);
|
signal,
|
||||||
window.addEventListener("touchend", this.touchSwipeBind);
|
});
|
||||||
|
window.addEventListener("wheel", this.#mouseWheel.bind(this), {
|
||||||
|
passive: false,
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
window.addEventListener("keydown", this.#resetMouseScrollState.bind(this), {
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
window.addEventListener("contextmenu", this.#contextMenu.bind(this), {
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
window.addEventListener("touchstart", touchSwipeBind, { signal });
|
||||||
|
window.addEventListener("touchmove", touchSwipeBind, { signal });
|
||||||
|
window.addEventListener("touchend", touchSwipeBind, { signal });
|
||||||
}
|
}
|
||||||
|
|
||||||
#removeWindowListeners() {
|
#removeWindowListeners() {
|
||||||
window.removeEventListener("mousemove", this.showControlsBind);
|
this.#windowAbortController?.abort();
|
||||||
window.removeEventListener("mousedown", this.mouseDownBind);
|
this.#windowAbortController = null;
|
||||||
window.removeEventListener("wheel", this.mouseWheelBind, {
|
|
||||||
passive: false,
|
|
||||||
});
|
|
||||||
window.removeEventListener("keydown", this.resetMouseScrollStateBind);
|
|
||||||
window.removeEventListener("contextmenu", this.contextMenuBind);
|
|
||||||
window.removeEventListener("touchstart", this.touchSwipeBind);
|
|
||||||
window.removeEventListener("touchmove", this.touchSwipeBind);
|
|
||||||
window.removeEventListener("touchend", this.touchSwipeBind);
|
|
||||||
|
|
||||||
delete this.showControlsBind;
|
|
||||||
delete this.mouseDownBind;
|
|
||||||
delete this.mouseWheelBind;
|
|
||||||
delete this.resetMouseScrollStateBind;
|
|
||||||
delete this.contextMenuBind;
|
|
||||||
delete this.touchSwipeBind;
|
|
||||||
}
|
|
||||||
|
|
||||||
#fullscreenChange() {
|
|
||||||
if (/* isFullscreen = */ document.fullscreenElement) {
|
|
||||||
this.#enter();
|
|
||||||
} else {
|
|
||||||
this.#exit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#addFullscreenChangeListeners() {
|
#addFullscreenChangeListeners() {
|
||||||
this.fullscreenChangeBind = this.#fullscreenChange.bind(this);
|
if (this.#fullscreenChangeAbortController) {
|
||||||
window.addEventListener("fullscreenchange", this.fullscreenChangeBind);
|
return;
|
||||||
|
}
|
||||||
|
this.#fullscreenChangeAbortController = new AbortController();
|
||||||
|
|
||||||
|
window.addEventListener(
|
||||||
|
"fullscreenchange",
|
||||||
|
() => {
|
||||||
|
if (/* isFullscreen = */ document.fullscreenElement) {
|
||||||
|
this.#enter();
|
||||||
|
} else {
|
||||||
|
this.#exit();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ signal: this.#fullscreenChangeAbortController.signal }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#removeFullscreenChangeListeners() {
|
#removeFullscreenChangeListeners() {
|
||||||
window.removeEventListener("fullscreenchange", this.fullscreenChangeBind);
|
this.#fullscreenChangeAbortController?.abort();
|
||||||
delete this.fullscreenChangeBind;
|
this.#fullscreenChangeAbortController = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue