| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.async; | 5 part of dart.async; |
| 6 | 6 |
| 7 abstract class Timer { | 7 abstract class Timer { |
| 8 // Internal list used to group Timer.run callbacks. | 8 // Internal list used to group Timer.run callbacks. |
| 9 static List _runCallbacks = []; | 9 static List _runCallbacks = []; |
| 10 | 10 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 * The [callback] is invoked repeatedly with [duration] intervals until | 39 * The [callback] is invoked repeatedly with [duration] intervals until |
| 40 * canceled. A negative duration is treated similar to a duration of 0. | 40 * canceled. A negative duration is treated similar to a duration of 0. |
| 41 */ | 41 */ |
| 42 external factory Timer.periodic(Duration duration, | 42 external factory Timer.periodic(Duration duration, |
| 43 void callback(Timer timer)); | 43 void callback(Timer timer)); |
| 44 | 44 |
| 45 /** | 45 /** |
| 46 * Runs the given [callback] asynchronously as soon as possible. | 46 * Runs the given [callback] asynchronously as soon as possible. |
| 47 */ | 47 */ |
| 48 static void run(void callback()) { | 48 static void run(void callback()) { |
| 49 schedule() { | 49 // Optimizing a group of Timer.run callbacks to be executed in the |
| 50 // same Timer callback. |
| 51 _runCallbacks.add(callback); |
| 52 if (_runCallbacks.length == 1) { |
| 50 new Timer(const Duration(milliseconds: 0), () { | 53 new Timer(const Duration(milliseconds: 0), () { |
| 51 List runCallbacks = _runCallbacks; | 54 List runCallbacks = _runCallbacks; |
| 52 // Create new list to make sure we don't call newly added callbacks in | 55 // Create new list to make sure we don't call newly added callbacks in |
| 53 // this event. | 56 // this event. |
| 54 _runCallbacks = []; | 57 _runCallbacks = []; |
| 55 for (int i = 0; i < runCallbacks.length; i++) { | 58 for (int i = 0; i < runCallbacks.length; i++) { |
| 56 Function callback = runCallbacks[i]; | 59 Function callback = runCallbacks[i]; |
| 57 try { | 60 try { |
| 58 callback(); | 61 callback(); |
| 59 } catch (e) { | 62 } catch (e) { |
| 60 List newCallbacks = _runCallbacks; | 63 List newCallbacks = _runCallbacks; |
| 61 _runCallbacks = []; | 64 _runCallbacks = []; |
| 62 i++; // Skip the current; | 65 i++; // Skip the current; |
| 63 _runCallbacks.addAll( | 66 _runCallbacks.addAll( |
| 64 runCallbacks.sublist(i)); | 67 runCallbacks.sublist(i)); |
| 65 _runCallbacks.addAll(newCallbacks); | 68 _runCallbacks.addAll(newCallbacks); |
| 66 if (!_runCallbacks.isEmpty) schedule(); | |
| 67 throw; | 69 throw; |
| 68 } | 70 } |
| 69 } | 71 } |
| 70 }); | 72 }); |
| 71 } | 73 } |
| 72 | |
| 73 // Optimizing a group of Timer.run callbacks to be executed in the | |
| 74 // same Timer callback. | |
| 75 _runCallbacks.add(callback); | |
| 76 if (_runCallbacks.length == 1) schedule(); | |
| 77 } | 74 } |
| 78 | 75 |
| 79 /** | 76 /** |
| 80 * Cancels the timer. | 77 * Cancels the timer. |
| 81 */ | 78 */ |
| 82 void cancel(); | 79 void cancel(); |
| 83 } | 80 } |
| OLD | NEW |