Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: sdk/lib/async/timer.dart

Issue 12316103: Introduce optimized Timer.run queue for immediate runs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/isolate/timer_cancel_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 _runCallbacks.add(callback);
56 if (_runCallbacks.length == 1) {
floitsch 2013/02/25 18:02:33 Add comment explaining that this is an optimizatio
Anders Johnsen 2013/06/11 12:14:31 Done.
57 new Timer(const Duration(milliseconds: 0), () {
58 List runCallbacks = _runCallbacks;
59 // Create new list to make sure we don't call newly added callbacks in
60 // this event.
61 _runCallbacks = [];
62 for (int i = 0; i < runCallbacks.length; i++) {
63 Function callback = runCallbacks[i];
64 try {
65 callback();
66 } catch (e) {
67 List newCallbacks = _runCallbacks;
68 _runCallbacks = [];
69 _runCallbacks.addAll(_runCallbacks.skip(i + 1));
floitsch 2013/02/25 18:02:33 runCallbacks
Anders Johnsen 2013/06/11 12:14:31 Done.
70 _runCallbacks.addAll(runCallbacks);
floitsch 2013/02/25 18:02:33 newCallbacks
Anders Johnsen 2013/06/11 12:14:31 Done.
71 throw;
72 }
73 }
74 });
75 }
56 } 76 }
57 77
58 /** 78 /**
59 * Cancels the timer. 79 * Cancels the timer.
60 */ 80 */
61 void cancel(); 81 void cancel();
62 } 82 }
OLDNEW
« no previous file with comments | « no previous file | tests/isolate/timer_cancel_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698