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

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

Issue 2893893002: Use generic functions in zones. (Closed)
Patch Set: Fix more tests. Created 3 years, 4 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
« no previous file with comments | « sdk/lib/async/stream_impl.dart ('k') | sdk/lib/async/zone.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 /** 7 /**
8 * A count-down timer that can be configured to fire once or repeatedly. 8 * A count-down timer that can be configured to fire once or repeatedly.
9 * 9 *
10 * The timer counts down from the specified duration to 0. 10 * The timer counts down from the specified duration to 0.
(...skipping 30 matching lines...) Expand all
41 * 41 *
42 * The [callback] function is invoked after the given [duration]. 42 * The [callback] function is invoked after the given [duration].
43 * 43 *
44 */ 44 */
45 factory Timer(Duration duration, void callback()) { 45 factory Timer(Duration duration, void callback()) {
46 if (Zone.current == Zone.ROOT) { 46 if (Zone.current == Zone.ROOT) {
47 // No need to bind the callback. We know that the root's timer will 47 // No need to bind the callback. We know that the root's timer will
48 // be invoked in the root zone. 48 // be invoked in the root zone.
49 return Zone.current.createTimer(duration, callback); 49 return Zone.current.createTimer(duration, callback);
50 } 50 }
51 return Zone.current.createTimer( 51 return Zone.current
52 duration, Zone.current.bindCallback(callback, runGuarded: true)); 52 .createTimer(duration, Zone.current.bindCallbackGuarded(callback));
53 } 53 }
54 54
55 /** 55 /**
56 * Creates a new repeating timer. 56 * Creates a new repeating timer.
57 * 57 *
58 * The [callback] is invoked repeatedly with [duration] intervals until 58 * The [callback] is invoked repeatedly with [duration] intervals until
59 * canceled with the [cancel] function. 59 * canceled with the [cancel] function.
60 * 60 *
61 * The exact timing depends on the underlying timer implementation. 61 * The exact timing depends on the underlying timer implementation.
62 * No more than `n` callbacks will be made in `duration * n` time, 62 * No more than `n` callbacks will be made in `duration * n` time,
63 * but the time between two consecutive callbacks 63 * but the time between two consecutive callbacks
64 * can be shorter and longer than `duration`. 64 * can be shorter and longer than `duration`.
65 * 65 *
66 * In particular, an implementation may schedule the next callback, e.g., 66 * In particular, an implementation may schedule the next callback, e.g.,
67 * a `duration` after either when the previous callback ended, 67 * a `duration` after either when the previous callback ended,
68 * when the previous callback started, or when the previous callback was 68 * when the previous callback started, or when the previous callback was
69 * scheduled for - even if the actual callback was delayed. 69 * scheduled for - even if the actual callback was delayed.
70 */ 70 */
71 factory Timer.periodic(Duration duration, void callback(Timer timer)) { 71 factory Timer.periodic(Duration duration, void callback(Timer timer)) {
72 if (Zone.current == Zone.ROOT) { 72 if (Zone.current == Zone.ROOT) {
73 // No need to bind the callback. We know that the root's timer will 73 // No need to bind the callback. We know that the root's timer will
74 // be invoked in the root zone. 74 // be invoked in the root zone.
75 return Zone.current.createPeriodicTimer(duration, callback); 75 return Zone.current.createPeriodicTimer(duration, callback);
76 } 76 }
77 // TODO(floitsch): the return type should be 'void', and the type 77 var boundCallback = Zone.current.bindUnaryCallbackGuarded<Timer>(callback);
78 // should be inferred.
79 var boundCallback = Zone.current
80 .bindUnaryCallback<dynamic, Timer>(callback, runGuarded: true);
81 return Zone.current.createPeriodicTimer(duration, boundCallback); 78 return Zone.current.createPeriodicTimer(duration, boundCallback);
82 } 79 }
83 80
84 /** 81 /**
85 * Runs the given [callback] asynchronously as soon as possible. 82 * Runs the given [callback] asynchronously as soon as possible.
86 * 83 *
87 * This function is equivalent to `new Timer(Duration.ZERO, callback)`. 84 * This function is equivalent to `new Timer(Duration.ZERO, callback)`.
88 */ 85 */
89 static void run(void callback()) { 86 static void run(void callback()) {
90 new Timer(Duration.ZERO, callback); 87 new Timer(Duration.ZERO, callback);
(...skipping 11 matching lines...) Expand all
102 * and the timer has not been canceled. 99 * and the timer has not been canceled.
103 * 100 *
104 * A periodic timer is active if it has not been canceled. 101 * A periodic timer is active if it has not been canceled.
105 */ 102 */
106 bool get isActive; 103 bool get isActive;
107 104
108 external static Timer _createTimer(Duration duration, void callback()); 105 external static Timer _createTimer(Duration duration, void callback());
109 external static Timer _createPeriodicTimer( 106 external static Timer _createPeriodicTimer(
110 Duration duration, void callback(Timer timer)); 107 Duration duration, void callback(Timer timer));
111 } 108 }
OLDNEW
« no previous file with comments | « sdk/lib/async/stream_impl.dart ('k') | sdk/lib/async/zone.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698