Pull web-animations-js into third-party via the third_party/polymer/reproduce.sh...
[chromium-blink-merge.git] / third_party / web-animations-js / sources / src / effect-callback.js
blob6ccd5d9e0110a3e53d5c0cb5a4e43f02f4082528
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 //     You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //     See the License for the specific language governing permissions and
13 // limitations under the License.
14 (function(shared, scope, testing) {
16   var nullTarget = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
18   var sequenceNumber = 0;
19   scope.bindPlayerForCustomEffect = function(player) {
20     var target = player.source.target;
21     var effect = player.source.effect;
22     var timing = player.source.timing;
23     var last = undefined;
24     timing = shared.normalizeTimingInput(timing);
25     var callback = function() {
26       var t = callback._player ? callback._player.currentTime : null;
27       if (t !== null) {
28         t = shared.calculateTimeFraction(shared.calculateActiveDuration(timing), t, timing);
29         if (isNaN(t))
30           t = null;
31       }
32       // FIXME: There are actually more conditions under which the effect
33       // should be called.
34       if (t !== last)
35         effect(t, target, player.source);
36       last = t;
37     };
39     callback._player = player;
40     callback._registered = false;
41     callback._sequenceNumber = sequenceNumber++;
42     player._callback = callback;
43     register(callback);
44   };
46   var callbacks = [];
47   var ticking = false;
48   function register(callback) {
49     if (callback._registered)
50       return;
51     callback._registered = true;
52     callbacks.push(callback);
53     if (!ticking) {
54       ticking = true;
55       requestAnimationFrame(tick);
56     }
57   }
59   function tick(t) {
60     var updating = callbacks;
61     callbacks = [];
62     updating.sort(function(left, right) {
63       return left._sequenceNumber - right._sequenceNumber;
64     });
65     updating.filter(function(callback) {
66       callback();
67       if (!callback._player || callback._player.finished || callback._player.paused)
68         callback._registered = false;
69       return callback._registered;
70     });
71     callbacks.push.apply(callbacks, updating);
73     if (callbacks.length) {
74       ticking = true;
75       requestAnimationFrame(tick);
76     } else {
77       ticking = false;
78     }
79   }
81   scope.Player.prototype._register = function() {
82     if (this._callback)
83       register(this._callback);
84   };
86 })(webAnimationsShared, webAnimationsNext, webAnimationsTesting);