OLD | NEW |
1 // | 1 // |
2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
3 // | 3 // |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file or at | 5 // license that can be found in the LICENSE file or at |
6 // https://developers.google.com/open-source/licenses/bsd | 6 // https://developers.google.com/open-source/licenses/bsd |
7 // | 7 // |
8 | 8 |
9 /// A [window.requestAnimationFrame] based timer for use with transitions. | 9 /// A [window.requestAnimationFrame] based timer for use with transitions. |
10 /// Uses [dart.async.Timer] when the time until next timeout is too long. | 10 /// Uses [dart.async.Timer] when the time until next timeout is too long. |
11 library charted.core.timer; | 11 library charted.core.timer; |
12 | 12 |
13 import 'dart:async'; | 13 import 'dart:async'; |
14 import 'dart:html' show window; | 14 import 'dart:html' show window; |
15 import 'dart:collection'; | 15 import 'dart:collection'; |
16 | 16 |
17 typedef bool TimerCallback(int time); | 17 typedef bool TimerCallback(int time); |
18 | 18 |
19 class AnimationTimer extends LinkedListEntry { | 19 class AnimationTimer extends LinkedListEntry<AnimationTimer> { |
20 static LinkedList<AnimationTimer> _queue = new LinkedList<AnimationTimer>(); | 20 static LinkedList<AnimationTimer> _queue = new LinkedList<AnimationTimer>(); |
21 | 21 |
22 /// true if we are already waiting for window.animationFrame. At any given | 22 /// true if we are already waiting for window.animationFrame. At any given |
23 /// time, only one of _timerScheduled and _animationFrameRequested are set | 23 /// time, only one of _timerScheduled and _animationFrameRequested are set |
24 static bool _animationFrameRequested = false; | 24 static bool _animationFrameRequested = false; |
25 | 25 |
26 /// Instance of currently scheduled timer. At any given time, only one | 26 /// Instance of currently scheduled timer. At any given time, only one |
27 /// of _timerScheduled and _animationFrameRequested are set. | 27 /// of _timerScheduled and _animationFrameRequested are set. |
28 static Timer _timerScheduled; | 28 static Timer _timerScheduled; |
29 | 29 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 earliest = timer.time; | 71 earliest = timer.time; |
72 } | 72 } |
73 timer = timer.next; | 73 timer = timer.next; |
74 if (finished) ref.unlink(); | 74 if (finished) ref.unlink(); |
75 } | 75 } |
76 active = null; | 76 active = null; |
77 return earliest == null ? earliest : earliest - now; | 77 return earliest == null ? earliest : earliest - now; |
78 } | 78 } |
79 | 79 |
80 /// Internal timer and animation frame handler. | 80 /// Internal timer and animation frame handler. |
81 _step([_]) { | 81 _step([num _]) { |
82 int delay = flush(); | 82 int delay = flush(); |
83 | 83 |
84 if (delay == null) { | 84 if (delay == null) { |
85 _animationFrameRequested = false; | 85 _animationFrameRequested = false; |
86 } else if (delay > 24) { | 86 } else if (delay > 24) { |
87 if (_timerScheduled != null) { | 87 if (_timerScheduled != null) { |
88 _timerScheduled.cancel(); | 88 _timerScheduled.cancel(); |
89 } | 89 } |
90 _timerScheduled = new Timer(new Duration(milliseconds: delay), _step); | 90 _timerScheduled = new Timer(new Duration(milliseconds: delay), _step); |
91 _animationFrameRequested = false; | 91 _animationFrameRequested = false; |
92 } else { | 92 } else { |
93 _animationFrameRequested = true; | 93 _animationFrameRequested = true; |
94 window.animationFrame.then(_step); | 94 window.animationFrame.then(_step); |
95 } | 95 } |
96 } | 96 } |
97 } | 97 } |
OLD | NEW |