Remove event listeners with signal in web/pdf_history.js

This commit is contained in:
Jonas Jenwald 2024-04-18 20:06:02 +02:00
parent 702ee7b1e1
commit bf785f2180

View File

@ -53,6 +53,8 @@ function getCurrentHash() {
} }
class PDFHistory { class PDFHistory {
#eventAbortController = null;
/** /**
* @param {PDFHistoryOptions} options * @param {PDFHistoryOptions} options
*/ */
@ -64,7 +66,6 @@ class PDFHistory {
this._fingerprint = ""; this._fingerprint = "";
this.reset(); this.reset();
this._boundEvents = null;
// Ensure that we don't miss a "pagesinit" event, // Ensure that we don't miss a "pagesinit" event,
// by registering the listener immediately. // by registering the listener immediately.
this.eventBus._on("pagesinit", () => { this.eventBus._on("pagesinit", () => {
@ -679,29 +680,22 @@ class PDFHistory {
} }
#bindEvents() { #bindEvents() {
if (this._boundEvents) { if (this.#eventAbortController) {
return; // The event listeners were already added. return; // The event listeners were already added.
} }
this._boundEvents = { this.#eventAbortController = new AbortController();
updateViewarea: this.#updateViewarea.bind(this), const { signal } = this.#eventAbortController;
popState: this.#popState.bind(this),
pageHide: this.#pageHide.bind(this),
};
this.eventBus._on("updateviewarea", this._boundEvents.updateViewarea); this.eventBus._on("updateviewarea", this.#updateViewarea.bind(this), {
window.addEventListener("popstate", this._boundEvents.popState); signal,
window.addEventListener("pagehide", this._boundEvents.pageHide); });
window.addEventListener("popstate", this.#popState.bind(this), { signal });
window.addEventListener("pagehide", this.#pageHide.bind(this), { signal });
} }
#unbindEvents() { #unbindEvents() {
if (!this._boundEvents) { this.#eventAbortController?.abort();
return; // The event listeners were already removed. this.#eventAbortController = null;
}
this.eventBus._off("updateviewarea", this._boundEvents.updateViewarea);
window.removeEventListener("popstate", this._boundEvents.popState);
window.removeEventListener("pagehide", this._boundEvents.pageHide);
this._boundEvents = null;
} }
} }