Bug 1817639 - [devtools] Use abortController to relay target actor events in highlighters.js r=nchevobbe,devtools-reviewers

Depends on D170293

Differential Revision: https://phabricator.services.mozilla.com/D170294
This commit is contained in:
Julian Descottes 2023-03-28 15:37:07 +00:00
Родитель 37af8302a5
Коммит 7610716776
1 изменённых файлов: 15 добавлений и 28 удалений

Просмотреть файл

@ -177,22 +177,18 @@ exports.CustomHighlighterActor = class CustomHighligherActor extends Actor {
*/ */
class HighlighterEnvironment extends EventEmitter { class HighlighterEnvironment extends EventEmitter {
constructor() {
super();
this.relayTargetActorWindowReady = this.relayTargetActorWindowReady.bind(
this
);
this.relayTargetActorNavigate = this.relayTargetActorNavigate.bind(this);
this.relayTargetActorWillNavigate = this.relayTargetActorWillNavigate.bind(
this
);
}
initFromTargetActor(targetActor) { initFromTargetActor(targetActor) {
this._targetActor = targetActor; this._targetActor = targetActor;
this._targetActor.on("window-ready", this.relayTargetActorWindowReady);
this._targetActor.on("navigate", this.relayTargetActorNavigate); const relayedEvents = ["window-ready", "navigate", "will-navigate"];
this._targetActor.on("will-navigate", this.relayTargetActorWillNavigate);
this._abortController = new AbortController();
const signal = this._abortController.signal;
for (const event of relayedEvents) {
this._targetActor.on(event, this.relayTargetEvent.bind(this, event), {
signal,
});
}
} }
initFromWindow(win) { initFromWindow(win) {
@ -302,23 +298,14 @@ class HighlighterEnvironment extends EventEmitter {
return this.docShell && this.docShell.chromeEventHandler; return this.docShell && this.docShell.chromeEventHandler;
} }
relayTargetActorWindowReady(data) { relayTargetEvent(name, data) {
this.emit("window-ready", data); this.emit(name, data);
}
relayTargetActorNavigate(data) {
this.emit("navigate", data);
}
relayTargetActorWillNavigate(data) {
this.emit("will-navigate", data);
} }
destroy() { destroy() {
if (this._targetActor) { if (this._abortController) {
this._targetActor.off("window-ready", this.relayTargetActorWindowReady); this._abortController.abort();
this._targetActor.off("navigate", this.relayTargetActorNavigate); this._abortController = null;
this._targetActor.off("will-navigate", this.relayTargetActorWillNavigate);
} }
// In case the environment was initialized from a window, we need to remove // In case the environment was initialized from a window, we need to remove