Skip to content

Commit

Permalink
Merge pull request #18592 from Snuffleupagus/firefoxcom-AbortSignal-any
Browse files Browse the repository at this point in the history
[Firefox] Remove the "loadaiengineprogress" listener with `AbortSignal.any()`
  • Loading branch information
timvandermeij committed Aug 10, 2024
2 parents 4367fc1 + 723c769 commit ec48602
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down

0 comments on commit ec48602

Please sign in to comment.