Bug 1464396 - Part 1: Hold all animation's created time. r?pbro draft
authorDaisuke Akatsuka <dakatsuka@mozilla.com>
Sat, 26 May 2018 00:13:44 +0900
changeset 799919 f5f2e3bf6aed24d286826bfcf4a85d468e5cc147
parent 799918 e1e87f6234517d3587c2dc7deae965242b5e0a41
child 799920 b772cedd59b59846034e6e6e0493c3b57df4bfc3
push id111203
push userbmo:dakatsuka@mozilla.com
push dateFri, 25 May 2018 15:44:00 +0000
reviewerspbro
bugs1464396
milestone62.0a1
Bug 1464396 - Part 1: Hold all animation's created time. r?pbro For now, we had taken over the createdTime to new actor from previous actor. However, if the selected node changed, can not take over that. To resolve this, hold all animation's createdTime. MozReview-Commit-ID: 3i3isMr3j05
devtools/server/actors/animation.js
--- a/devtools/server/actors/animation.js
+++ b/devtools/server/actors/animation.js
@@ -611,25 +611,30 @@ exports.AnimationsActor = protocol.Actor
 
     this.onWillNavigate = this.onWillNavigate.bind(this);
     this.onNavigate = this.onNavigate.bind(this);
     this.onAnimationMutation = this.onAnimationMutation.bind(this);
 
     this.allAnimationsPaused = false;
     this.tabActor.on("will-navigate", this.onWillNavigate);
     this.tabActor.on("navigate", this.onNavigate);
+
+    this.animationCreatedTimeMap = new Map();
   },
 
   destroy: function() {
     Actor.prototype.destroy.call(this);
     this.tabActor.off("will-navigate", this.onWillNavigate);
     this.tabActor.off("navigate", this.onNavigate);
 
     this.stopAnimationPlayerUpdates();
     this.tabActor = this.observer = this.actors = this.walker = null;
+
+    this.animationCreatedTimeMap.clear();
+    this.animationCreatedTimeMap = null;
   },
 
   /**
    * Clients can optionally call this with a reference to their WalkerActor.
    * If they do, then AnimationPlayerActor's forms are going to also include
    * NodeActor IDs when the corresponding NodeActors do exist.
    * This, in turns, is helpful for clients to avoid having to go back once more
    * to the server to get a NodeActor for a particular animation.
@@ -645,39 +650,31 @@ exports.AnimationsActor = protocol.Actor
    * Note that calling this method a second time will destroy all previously
    * retrieved AnimationPlayerActors. Indeed, the lifecycle of these actors
    * is managed here on the server and tied to getAnimationPlayersForNode
    * being called.
    * @param {NodeActor} nodeActor The NodeActor as defined in
    * /devtools/server/actors/inspector
    */
   getAnimationPlayersForNode: function(nodeActor) {
-    let animations = nodeActor.rawNode.getAnimations({subtree: true});
+    this.updateAnimationCreatedTimeMap();
 
-    const createdTimeMap = new Map();
+    let animations = nodeActor.rawNode.getAnimations({subtree: true});
 
     // Destroy previously stored actors
     if (this.actors) {
       for (const actor of this.actors) {
-        // Take the createdTime over new actors.
-        createdTimeMap.set(actor.player, actor.createdTime);
         actor.destroy();
       }
     }
 
     this.actors = [];
 
     for (const animation of animations) {
-      let createdTime = createdTimeMap.get(animation);
-
-      if (typeof createdTime === "undefined") {
-        // If no previous actor, set startTime or currentTime of timeline as created time.
-        createdTime = animation.startTime || animation.timeline.currentTime;
-      }
-
+      const createdTime = this.animationCreatedTimeMap.get(animation);
       const actor = AnimationPlayerActor(this, animation, createdTime);
       this.actors.push(actor);
     }
 
     // When a front requests the list of players for a node, start listening
     // for animation mutations on this node to send updates to the front, until
     // either getAnimationPlayersForNode is called again or
     // stopAnimationPlayerUpdates is called.
@@ -949,9 +946,32 @@ exports.AnimationsActor = protocol.Actor
       // We can not play with playbackRate zero.
       return;
     }
 
     // Play animation in a synchronous fashion by setting the start time directly.
     const currentTime = player.currentTime || 0;
     player.startTime = player.timeline.currentTime - currentTime / player.playbackRate;
   },
+
+  /**
+   * Update animation created time map.
+   */
+  updateAnimationCreatedTimeMap() {
+    const currentAnimations = this.getAllAnimations(this.tabActor.window.document);
+
+    // Remove invalid animation.
+    for (const previousAnimation of this.animationCreatedTimeMap.keys()) {
+      if (!currentAnimations.includes(previousAnimation)) {
+        this.animationCreatedTimeMap.delete(previousAnimation);
+      }
+    }
+
+    for (const animation of currentAnimations) {
+      if (!this.animationCreatedTimeMap.has(animation)) {
+        const createdTime =
+          animation.startTime ||
+          animation.timeline.currentTime - animation.currentTime / animation.playbackRate;
+        this.animationCreatedTimeMap.set(animation, createdTime);
+      }
+    }
+  },
 });