Bug 1637562: Introduce 'ignoreExistingResources' flag in order to ignore existing resources. r=ochameau,nchevobbe
authorDaisuke Akatsuka <daisuke@birchill.co.jp>
Wed, 20 May 2020 08:15:22 +0000
changeset 530968 3d2af0d65f39995d6ebc017ffcc6e6797d1461dd
parent 530967 dc4525f247b901b1f6f958e3fd8518dd85dfff17
child 530969 c5b2ed837172140d6a2b0237250da2dcbfdfaa73
push id37435
push userapavel@mozilla.com
push dateWed, 20 May 2020 15:28:23 +0000
treeherdermozilla-central@5415da14ec9a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersochameau, nchevobbe
bugs1637562
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1637562: Introduce 'ignoreExistingResources' flag in order to ignore existing resources. r=ochameau,nchevobbe Differential Revision: https://phabricator.services.mozilla.com/D75895
devtools/shared/resources/resource-watcher.js
--- a/devtools/shared/resources/resource-watcher.js
+++ b/devtools/shared/resources/resource-watcher.js
@@ -58,33 +58,41 @@ class ResourceWatcher {
    *        List of all resources which should be fetched and observed.
    * @param {Object} options
    *        - {Function} onAvailable: This attribute is mandatory.
    *                                  Function which will be called once per existing
    *                                  resource and each time a resource is created.
    *        - {Function} onDestroyed: This attribute is optional.
    *                                  Function which will be called each time a resource in
    *                                  the remote target is destroyed.
+   *        - {boolean} ignoreExistingResources:
+   *                                  This attribute is optional. Default value is false.
+   *                                  If set to true, onAvailable won't be called with
+   *                                  existing resources.
    */
   async watch(resources, options) {
-    const { onAvailable, onDestroyed } = options;
+    const { ignoreExistingResources = false } = options;
 
     // First ensuring enabling listening to targets.
     // This will call onTargetAvailable for all already existing targets,
     // as well as for the one created later.
     // Do this *before* calling _startListening in order to register
     // "resource-available" listener before requesting for the resources in _startListening.
     await this._watchAllTargets();
 
     for (const resource of resources) {
-      this._availableListeners.on(resource, onAvailable);
-      if (onDestroyed) {
-        this._destroyedListeners.on(resource, onDestroyed);
+      if (ignoreExistingResources) {
+        // Register listeners after _startListening
+        // so that it avoids the listeners to get cached resources.
+        await this._startListening(resource);
+        this._registerListeners(resource, options);
+      } else {
+        this._registerListeners(resource, options);
+        await this._startListening(resource);
       }
-      await this._startListening(resource);
     }
   }
 
   /**
    * Stop watching for given type of resources.
    * See `watch` for the arguments as both methods receive the same.
    */
   unwatch(resources, options) {
@@ -104,16 +112,30 @@ class ResourceWatcher {
       listeners += count;
     }
     if (listeners <= 0) {
       this._unwatchAllTargets();
     }
   }
 
   /**
+   * Register listeners to watch for a given type of resource.
+   *
+   * @param {Object}
+   *        - {Function} onAvailable: mandatory
+   *        - {Function} onDestroyed: optional
+   */
+  _registerListeners(resource, { onAvailable, onDestroyed }) {
+    this._availableListeners.on(resource, onAvailable);
+    if (onDestroyed) {
+      this._destroyedListeners.on(resource, onDestroyed);
+    }
+  }
+
+  /**
    * Start watching for all already existing and future targets.
    *
    * We are using ALL_TYPES, but this won't force listening to all types.
    * It will only listen for types which are defined by `TargetList.startListening`.
    */
   async _watchAllTargets() {
     if (this._isWatchingTargets) {
       return;