| 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. |
| 9 static List _runCallbacks = []; |
| 10 |
| 8 /** | 11 /** |
| 9 * Creates a new timer. | 12 * Creates a new timer. |
| 10 * | 13 * |
| 11 * The [callback] callback is invoked after the given [duration] | 14 * The [callback] callback is invoked after the given [duration] |
| 12 * (a [Duration]) has passed. A negative duration is treated similar to | 15 * (a [Duration]) has passed. A negative duration is treated similar to |
| 13 * a duration of 0. | 16 * a duration of 0. |
| 14 * | 17 * |
| 15 * If the [duration] is statically known to be 0, consider using [run]. | 18 * If the [duration] is statically known to be 0, consider using [run]. |
| 16 * | 19 * |
| 17 * Frequently the [duration] is either a constant or computed as in the | 20 * Frequently the [duration] is either a constant or computed as in the |
| (...skipping 22 matching lines...) Expand all Loading... |
| 40 * canceled. A negative duration is treated similar to a duration of 0. | 43 * canceled. A negative duration is treated similar to a duration of 0. |
| 41 * | 44 * |
| 42 * *Deprecation warning*: this constructor used to take an [int] (the time | 45 * *Deprecation warning*: this constructor used to take an [int] (the time |
| 43 * in milliseconds). This has changed to a [Duration]. | 46 * in milliseconds). This has changed to a [Duration]. |
| 44 */ | 47 */ |
| 45 external factory Timer.repeating(var duration, | 48 external factory Timer.repeating(var duration, |
| 46 void callback(Timer timer)); | 49 void callback(Timer timer)); |
| 47 | 50 |
| 48 /** | 51 /** |
| 49 * Runs the given [callback] asynchronously as soon as possible. | 52 * Runs the given [callback] asynchronously as soon as possible. |
| 50 * | |
| 51 * Returns a [Timer] that can be cancelled if the callback is not necessary | |
| 52 * anymore. | |
| 53 */ | 53 */ |
| 54 static Timer run(void callback()) { | 54 static void run(void callback()) { |
| 55 return new Timer(const Duration(), callback); | 55 // Optimizing a group of Timer.run callbacks to be executed in the |
| 56 // same Timer callback. |
| 57 _runCallbacks.add(callback); |
| 58 if (_runCallbacks.length == 1) { |
| 59 new Timer(const Duration(milliseconds: 0), () { |
| 60 List runCallbacks = _runCallbacks; |
| 61 // Create new list to make sure we don't call newly added callbacks in |
| 62 // this event. |
| 63 _runCallbacks = []; |
| 64 for (int i = 0; i < runCallbacks.length; i++) { |
| 65 Function callback = runCallbacks[i]; |
| 66 try { |
| 67 callback(); |
| 68 } catch (e) { |
| 69 List newCallbacks = _runCallbacks; |
| 70 _runCallbacks = []; |
| 71 _runCallbacks.addAll(runCallbacks.skip(i + 1)); |
| 72 _runCallbacks.addAll(newCallbacks); |
| 73 throw; |
| 74 } |
| 75 } |
| 76 }); |
| 77 } |
| 56 } | 78 } |
| 57 | 79 |
| 58 /** | 80 /** |
| 59 * Cancels the timer. | 81 * Cancels the timer. |
| 60 */ | 82 */ |
| 61 void cancel(); | 83 void cancel(); |
| 62 } | 84 } |
| OLD | NEW |