From 65cecb5a83dd65591dbaaca05c5b7d161efbaf90 Mon Sep 17 00:00:00 2001 From: Alexandre Poirot Date: Fri, 14 Jan 2022 17:03:35 +0000 Subject: [PATCH] Bug 1749769 - [devtools] Lazy load target legacy listeners. r=nchevobbe Differential Revision: https://phabricator.services.mozilla.com/D135832 --- .../webconsole/test/node/mocha-test-setup.js | 13 +++- .../legacy-processes-watcher.js | 2 +- .../legacy-serviceworkers-watcher.js | 6 +- .../legacy-sharedworkers-watcher.js | 6 +- .../legacy-workers-watcher.js | 6 +- .../shared/commands/target/target-command.js | 70 +++++++++---------- 6 files changed, 51 insertions(+), 52 deletions(-) diff --git a/devtools/client/webconsole/test/node/mocha-test-setup.js b/devtools/client/webconsole/test/node/mocha-test-setup.js index aa1284d71ec9..c4e009aad0d4 100644 --- a/devtools/client/webconsole/test/node/mocha-test-setup.js +++ b/devtools/client/webconsole/test/node/mocha-test-setup.js @@ -57,6 +57,10 @@ global.loader = { "devtools/client/shared/telemetry", "devtools/client/shared/screenshot", "devtools/client/shared/focus", + "devtools/shared/commands/target/legacy-target-watchers/legacy-processes-watcher", + "devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher", + "devtools/shared/commands/target/legacy-target-watchers/legacy-sharedworkers-watcher", + "devtools/shared/commands/target/legacy-target-watchers/legacy-serviceworkers-watcher", ]; if (!excluded.includes(path)) { if (!Array.isArray(names)) { @@ -64,8 +68,13 @@ global.loader = { } for (const name of names) { - const module = require(path); - global[name] = destruct ? module[name] : module; + Object.defineProperty(global, name, { + get() { + const module = require(path); + return destruct ? module[name] : module; + }, + configurable: true, + }); } } }, diff --git a/devtools/shared/commands/target/legacy-target-watchers/legacy-processes-watcher.js b/devtools/shared/commands/target/legacy-target-watchers/legacy-processes-watcher.js index 39ed12415f8d..e0c5b18d5155 100644 --- a/devtools/shared/commands/target/legacy-target-watchers/legacy-processes-watcher.js +++ b/devtools/shared/commands/target/legacy-target-watchers/legacy-processes-watcher.js @@ -69,4 +69,4 @@ class LegacyProcessesWatcher { } } -module.exports = { LegacyProcessesWatcher }; +module.exports = LegacyProcessesWatcher; diff --git a/devtools/shared/commands/target/legacy-target-watchers/legacy-serviceworkers-watcher.js b/devtools/shared/commands/target/legacy-target-watchers/legacy-serviceworkers-watcher.js index cd41f81bc759..e858862d7281 100644 --- a/devtools/shared/commands/target/legacy-target-watchers/legacy-serviceworkers-watcher.js +++ b/devtools/shared/commands/target/legacy-target-watchers/legacy-serviceworkers-watcher.js @@ -7,9 +7,7 @@ // eslint-disable-next-line mozilla/reject-some-requires const { WorkersListener } = require("devtools/client/shared/workers-listener"); -const { - LegacyWorkersWatcher, -} = require("devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher"); +const LegacyWorkersWatcher = require("devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher"); class LegacyServiceWorkersWatcher extends LegacyWorkersWatcher { // Holds the current target URL object @@ -317,4 +315,4 @@ class LegacyServiceWorkersWatcher extends LegacyWorkersWatcher { } } -module.exports = { LegacyServiceWorkersWatcher }; +module.exports = LegacyServiceWorkersWatcher; diff --git a/devtools/shared/commands/target/legacy-target-watchers/legacy-sharedworkers-watcher.js b/devtools/shared/commands/target/legacy-target-watchers/legacy-sharedworkers-watcher.js index 540950a35f47..36adb219349f 100644 --- a/devtools/shared/commands/target/legacy-target-watchers/legacy-sharedworkers-watcher.js +++ b/devtools/shared/commands/target/legacy-target-watchers/legacy-sharedworkers-watcher.js @@ -4,9 +4,7 @@ "use strict"; -const { - LegacyWorkersWatcher, -} = require("devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher"); +const LegacyWorkersWatcher = require("devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher"); class LegacySharedWorkersWatcher extends LegacyWorkersWatcher { // Flag used from the parent class to listen to process targets. @@ -18,4 +16,4 @@ class LegacySharedWorkersWatcher extends LegacyWorkersWatcher { } } -module.exports = { LegacySharedWorkersWatcher }; +module.exports = LegacySharedWorkersWatcher; diff --git a/devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher.js b/devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher.js index cbfde09697ff..206db680fd93 100644 --- a/devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher.js +++ b/devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher.js @@ -4,9 +4,7 @@ "use strict"; -const { - LegacyProcessesWatcher, -} = require("devtools/shared/commands/target/legacy-target-watchers/legacy-processes-watcher"); +const LegacyProcessesWatcher = require("devtools/shared/commands/target/legacy-target-watchers/legacy-processes-watcher"); class LegacyWorkersWatcher { constructor(targetCommand, onTargetAvailable, onTargetDestroyed) { @@ -237,4 +235,4 @@ class LegacyWorkersWatcher { } } -module.exports = { LegacyWorkersWatcher }; +module.exports = LegacyWorkersWatcher; diff --git a/devtools/shared/commands/target/target-command.js b/devtools/shared/commands/target/target-command.js index b9d7d1c6e25a..38fb44d393c2 100644 --- a/devtools/shared/commands/target/target-command.js +++ b/devtools/shared/commands/target/target-command.js @@ -9,19 +9,6 @@ const EventEmitter = require("devtools/shared/event-emitter"); const BROWSERTOOLBOX_FISSION_ENABLED = "devtools.browsertoolbox.fission"; -const { - LegacyProcessesWatcher, -} = require("devtools/shared/commands/target/legacy-target-watchers/legacy-processes-watcher"); -const { - LegacyServiceWorkersWatcher, -} = require("devtools/shared/commands/target/legacy-target-watchers/legacy-serviceworkers-watcher"); -const { - LegacySharedWorkersWatcher, -} = require("devtools/shared/commands/target/legacy-target-watchers/legacy-sharedworkers-watcher"); -const { - LegacyWorkersWatcher, -} = require("devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher"); - class TargetCommand extends EventEmitter { #selectedTargetFront; /** @@ -88,29 +75,7 @@ class TargetCommand extends EventEmitter { this._onTargetDestroyed = this._onTargetDestroyed.bind(this); this._onTargetSelected = this._onTargetSelected.bind(this); - this.legacyImplementation = { - process: new LegacyProcessesWatcher( - this, - this._onTargetAvailable, - this._onTargetDestroyed - ), - worker: new LegacyWorkersWatcher( - this, - this._onTargetAvailable, - this._onTargetDestroyed - ), - shared_worker: new LegacySharedWorkersWatcher( - this, - this._onTargetAvailable, - this._onTargetDestroyed - ), - service_worker: new LegacyServiceWorkersWatcher( - this, - this._onTargetAvailable, - this._onTargetDestroyed, - this.commands - ), - }; + this.legacyImplementation = {}; // Public flag to allow listening for workers even if the fission pref is off // This allows listening for workers in the content toolbox outside of fission contexts @@ -466,7 +431,16 @@ class TargetCommand extends EventEmitter { if (!isTargetSwitching) { await this.watcherFront.watchTargets(type); } - } else if (this.legacyImplementation[type]) { + } else if (LegacyTargetWatchers[type]) { + // Instantiate the legacy listener only once for each TargetCommand, and reuse it if we stop and restart listening + if (!this.legacyImplementation[type]) { + this.legacyImplementation[type] = new LegacyTargetWatchers[type]( + this, + this._onTargetAvailable, + this._onTargetDestroyed, + this.commands + ); + } await this.legacyImplementation[type].listen(); } else { throw new Error(`Unsupported target type '${type}'`); @@ -1087,4 +1061,26 @@ TargetCommand.ALL_TYPES = TargetCommand.prototype.ALL_TYPES = Object.values( TargetCommand.TYPES ); +const LegacyTargetWatchers = {}; +loader.lazyRequireGetter( + LegacyTargetWatchers, + TargetCommand.TYPES.PROCESS, + "devtools/shared/commands/target/legacy-target-watchers/legacy-processes-watcher" +); +loader.lazyRequireGetter( + LegacyTargetWatchers, + TargetCommand.TYPES.WORKER, + "devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher" +); +loader.lazyRequireGetter( + LegacyTargetWatchers, + TargetCommand.TYPES.SHARED_WORKER, + "devtools/shared/commands/target/legacy-target-watchers/legacy-sharedworkers-watcher" +); +loader.lazyRequireGetter( + LegacyTargetWatchers, + TargetCommand.TYPES.SERVICE_WORKER, + "devtools/shared/commands/target/legacy-target-watchers/legacy-serviceworkers-watcher" +); + module.exports = TargetCommand;