From 723c76929ce9ef18ee264ed28d28d79f5cda51d8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 10 Aug 2024 11:16:42 +0200 Subject: [PATCH] [Firefox] Remove the "loadaiengineprogress" listener with `AbortSignal.any()` --- web/firefoxcom.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/web/firefoxcom.js b/web/firefoxcom.js index 2d9650e165be9..f8e711686b998 100644 --- a/web/firefoxcom.js +++ b/web/firefoxcom.js @@ -466,24 +466,28 @@ class MLManager { }); (this.#enabled ||= new Map()).set("altText", promise); if (listenToProgress) { + const ac = new AbortController(); + const signal = AbortSignal.any([this.#abortSignal, ac.signal]); + this.hasProgress = true; - const callback = ({ detail }) => { - this.#eventBus.dispatch("loadaiengineprogress", { - source: this, - detail, - }); - if (detail.finished) { - this.hasProgress = false; - window.removeEventListener("loadAIEngineProgress", callback); - } - }; - window.addEventListener("loadAIEngineProgress", callback, { - signal: this.#abortSignal, - }); + window.addEventListener( + "loadAIEngineProgress", + ({ detail }) => { + this.#eventBus.dispatch("loadaiengineprogress", { + source: this, + detail, + }); + if (detail.finished) { + ac.abort(); + this.hasProgress = false; + } + }, + { signal } + ); promise.then(ok => { if (!ok) { + ac.abort(); this.hasProgress = false; - window.removeEventListener("loadAIEngineProgress", callback); } }); }