OLD | NEW |
(Empty) | |
| 1 part of angular.animate; |
| 2 |
| 3 /** |
| 4 * Window.animationFrame update loop that tracks and drives |
| 5 * [LoopedAnimations]'s. |
| 6 */ |
| 7 @Injectable() |
| 8 class AnimationLoop { |
| 9 final AnimationFrame _frames; |
| 10 final Profiler _profiler; |
| 11 final List<LoopedAnimation> _animations = []; |
| 12 final VmTurnZone _zone; |
| 13 |
| 14 bool _animationFrameQueued = false; |
| 15 |
| 16 /** |
| 17 * The animation runner requires an [AnimationFrame] to drive the animation |
| 18 * frames, and profiler will report timing information for each of the |
| 19 * animation frames. |
| 20 */ |
| 21 AnimationLoop(this._frames, this._profiler, this._zone); |
| 22 |
| 23 /** |
| 24 * Start and play an animation through the state transitions defined in |
| 25 * [Animation]. |
| 26 */ |
| 27 void play(LoopedAnimation animation) { |
| 28 _animations.add(animation); |
| 29 _queueAnimationFrame(); |
| 30 } |
| 31 |
| 32 void _queueAnimationFrame() { |
| 33 if (!_animationFrameQueued) { |
| 34 _animationFrameQueued = true; |
| 35 |
| 36 // TODO(codleogic): This should run outside of an angular scope digest. |
| 37 _zone.runOutsideAngular(() { |
| 38 _frames.animationFrame.then((timeInMs) => _animationFrame(timeInMs)) |
| 39 .catchError((error) => print(error)); |
| 40 }); |
| 41 } |
| 42 } |
| 43 |
| 44 /* On the browsers animation frame event, update each of the tracked |
| 45 * animations. Group dom reads first, and and writes second. |
| 46 * |
| 47 * At any point any animation may be updated by calling interrupt and cancel |
| 48 * with a reference to the [Animation] to cancel. The [AnimationRunner] will |
| 49 * then forget about the [Animation] and will not call any further methods on |
| 50 * the [Animation]. |
| 51 */ |
| 52 void _animationFrame(num timeInMs) { |
| 53 _profiler.startTimer("AnimationRunner.AnimationFrame"); |
| 54 _animationFrameQueued = false; |
| 55 |
| 56 _profiler.startTimer("AnimationRunner.AnimationFrame.DomReads"); |
| 57 // Dom reads |
| 58 _read(timeInMs); |
| 59 _profiler.stopTimer("AnimationRunner.AnimationFrame.DomReads"); |
| 60 |
| 61 _profiler.startTimer("AnimationRunner.AnimationFrame.DomMutates"); |
| 62 // Dom mutates |
| 63 _update(timeInMs); |
| 64 _profiler.stopTimer("AnimationRunner.AnimationFrame.DomMutates"); |
| 65 |
| 66 // We don't need to continue queuing animation frames |
| 67 // if there are no more animations to process. |
| 68 if (_animations.length > 0) { |
| 69 _queueAnimationFrame(); |
| 70 } |
| 71 |
| 72 _profiler.stopTimer("AnimationRunner.AnimationFrame"); |
| 73 } |
| 74 |
| 75 void _update(num timeInMs) { |
| 76 for (int i=0; i< _animations.length; i++) { |
| 77 var controller = _animations[i]; |
| 78 if (!controller.update(timeInMs)) { |
| 79 _animations.removeAt(i--); |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 void _read(num timeInMs) { |
| 85 for (int i=0; i< _animations.length; i++) { |
| 86 var animation = _animations[i]; |
| 87 animation.read(timeInMs); |
| 88 } |
| 89 } |
| 90 |
| 91 /** |
| 92 * Stop tracking and updating the [animation]. |
| 93 */ |
| 94 void forget(LoopedAnimation animation) { |
| 95 assert(animation != null); |
| 96 _animations.remove(animation); |
| 97 } |
| 98 } |
| 99 |
| 100 /** |
| 101 * Wrapper around window.requestAnimationFrame so it can be intercepted and |
| 102 * tested. |
| 103 */ |
| 104 @Injectable() |
| 105 class AnimationFrame { |
| 106 final dom.Window _wnd; |
| 107 Future<num> get animationFrame => _wnd.animationFrame; |
| 108 |
| 109 AnimationFrame(this._wnd); |
| 110 } |
OLD | NEW |