From bf785f2180b570e325d758fba2287d1dd6f4f125 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 18 Apr 2024 20:06:02 +0200 Subject: [PATCH] Remove event listeners with `signal` in web/pdf_history.js --- web/pdf_history.js | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/web/pdf_history.js b/web/pdf_history.js index 4ba506ede..5d3baa38d 100644 --- a/web/pdf_history.js +++ b/web/pdf_history.js @@ -53,6 +53,8 @@ function getCurrentHash() { } class PDFHistory { + #eventAbortController = null; + /** * @param {PDFHistoryOptions} options */ @@ -64,7 +66,6 @@ class PDFHistory { this._fingerprint = ""; this.reset(); - this._boundEvents = null; // Ensure that we don't miss a "pagesinit" event, // by registering the listener immediately. this.eventBus._on("pagesinit", () => { @@ -679,29 +680,22 @@ class PDFHistory { } #bindEvents() { - if (this._boundEvents) { + if (this.#eventAbortController) { return; // The event listeners were already added. } - this._boundEvents = { - updateViewarea: this.#updateViewarea.bind(this), - popState: this.#popState.bind(this), - pageHide: this.#pageHide.bind(this), - }; + this.#eventAbortController = new AbortController(); + const { signal } = this.#eventAbortController; - this.eventBus._on("updateviewarea", this._boundEvents.updateViewarea); - window.addEventListener("popstate", this._boundEvents.popState); - window.addEventListener("pagehide", this._boundEvents.pageHide); + this.eventBus._on("updateviewarea", this.#updateViewarea.bind(this), { + signal, + }); + window.addEventListener("popstate", this.#popState.bind(this), { signal }); + window.addEventListener("pagehide", this.#pageHide.bind(this), { signal }); } #unbindEvents() { - if (!this._boundEvents) { - return; // The event listeners were already removed. - } - this.eventBus._off("updateviewarea", this._boundEvents.updateViewarea); - window.removeEventListener("popstate", this._boundEvents.popState); - window.removeEventListener("pagehide", this._boundEvents.pageHide); - - this._boundEvents = null; + this.#eventAbortController?.abort(); + this.#eventAbortController = null; } }