| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// A library that wraps [Timer] in a way that can be mocked out in test code. | 5 /// A library that wraps [Timer] in a way that can be mocked out in test code. |
| 6 /// Application code only needs to use [newTimer] to get an instance of [Timer]. | 6 /// Application code only needs to use [newTimer] to get an instance of [Timer]. |
| 7 /// Then test code can call [mock] to mock out all new [Timer] instances so that | 7 /// Then test code can call [mock] to mock out all new [Timer] instances so that |
| 8 /// they're controllable by a returned [Clock] object. | 8 /// they're controllable by a returned [Clock] object. |
| 9 library mock_clock; | 9 library mock_clock; |
| 10 | 10 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 /// The stream of millisecond ticks of the clock. | 53 /// The stream of millisecond ticks of the clock. |
| 54 Stream<int> get onTick => _broadcastController.stream; | 54 Stream<int> get onTick => _broadcastController.stream; |
| 55 | 55 |
| 56 /// Advances the clock forward by [milliseconds]. This works like synchronous | 56 /// Advances the clock forward by [milliseconds]. This works like synchronous |
| 57 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled | 57 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled |
| 58 /// to fire during the interval will do so asynchronously once control returns | 58 /// to fire during the interval will do so asynchronously once control returns |
| 59 /// to the event loop. | 59 /// to the event loop. |
| 60 void tick([int milliseconds = 1]) { | 60 void tick([int milliseconds = 1]) { |
| 61 for (var i = 0; i < milliseconds; i++) { | 61 for (var i = 0; i < milliseconds; i++) { |
| 62 var tickTime = ++_time; | 62 var tickTime = ++_time; |
| 63 runAsync(() { | 63 scheduleMicrotask(() { |
| 64 _broadcastController.add(tickTime); | 64 _broadcastController.add(tickTime); |
| 65 }); | 65 }); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 /// Automatically progresses forward in time as long as there are still | 69 /// Automatically progresses forward in time as long as there are still |
| 70 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each | 70 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each |
| 71 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent | 71 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent |
| 72 /// code runs before the next tick. | 72 /// code runs before the next tick. |
| 73 void run() { | 73 void run() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 95 : _time = _clock.time + duration.inMilliseconds { | 95 : _time = _clock.time + duration.inMilliseconds { |
| 96 _subscription = _clock.onTick.listen((time) { | 96 _subscription = _clock.onTick.listen((time) { |
| 97 if (time < _time) return; | 97 if (time < _time) return; |
| 98 _subscription.cancel(); | 98 _subscription.cancel(); |
| 99 _callback(); | 99 _callback(); |
| 100 }); | 100 }); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void cancel() => _subscription.cancel(); | 103 void cancel() => _subscription.cancel(); |
| 104 } | 104 } |
| OLD | NEW |