Merge pull request #18592 from Snuffleupagus/firefoxcom-AbortSignal-any

[Firefox] Remove the "loadaiengineprogress" listener with `AbortSignal.any()`
This commit is contained in:
Tim van der Meij 2024-08-10 18:21:18 +02:00 committed by GitHub
commit ec48602379
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -466,24 +466,28 @@ class MLManager {
}); });
(this.#enabled ||= new Map()).set("altText", promise); (this.#enabled ||= new Map()).set("altText", promise);
if (listenToProgress) { if (listenToProgress) {
const ac = new AbortController();
const signal = AbortSignal.any([this.#abortSignal, ac.signal]);
this.hasProgress = true; this.hasProgress = true;
const callback = ({ detail }) => { window.addEventListener(
"loadAIEngineProgress",
({ detail }) => {
this.#eventBus.dispatch("loadaiengineprogress", { this.#eventBus.dispatch("loadaiengineprogress", {
source: this, source: this,
detail, detail,
}); });
if (detail.finished) { if (detail.finished) {
ac.abort();
this.hasProgress = false; this.hasProgress = false;
window.removeEventListener("loadAIEngineProgress", callback);
} }
}; },
window.addEventListener("loadAIEngineProgress", callback, { { signal }
signal: this.#abortSignal, );
});
promise.then(ok => { promise.then(ok => {
if (!ok) { if (!ok) {
ac.abort();
this.hasProgress = false; this.hasProgress = false;
window.removeEventListener("loadAIEngineProgress", callback);
} }
}); });
} }