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

Unified Diff: sdk/lib/async/timer.dart

Issue 1848933002: Add tasks to zones. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sdk/lib/async/zone.dart » ('j') | sdk/lib/async/zone.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/async/timer.dart
diff --git a/sdk/lib/async/timer.dart b/sdk/lib/async/timer.dart
index fafe65228db5060519b0498e950edbd4347e6fe2..134bbef31dc8bf55639841cbfea4d0ea5d6c3af0 100644
--- a/sdk/lib/async/timer.dart
+++ b/sdk/lib/async/timer.dart
@@ -47,10 +47,13 @@ abstract class Timer {
if (Zone.current == Zone.ROOT) {
// No need to bind the callback. We know that the root's timer will
// be invoked in the root zone.
- return Zone.current.createTimer(duration, callback);
+ return Timer._createTimer(duration, callback);
}
- return Zone.current.createTimer(
- duration, Zone.current.bindCallback(callback, runGuarded: true));
+ SingleShotTimerTaskSpecification specification =
+ new SingleShotTimerTaskSpecification(duration, callback);
+ Task task =
+ Zone.current.createTask(specification, _createSingleShotTimerTask);
+ return new _TimerTaskWrapper(task);
}
/**
@@ -70,14 +73,60 @@ abstract class Timer {
* scheduled for - even if the actual callback was delayed.
*/
factory Timer.periodic(Duration duration,
- void callback(Timer timer)) {
+ void callback(Timer timer)) {
if (Zone.current == Zone.ROOT) {
// No need to bind the callback. We know that the root's timer will
// be invoked in the root zone.
- return Zone.current.createPeriodicTimer(duration, callback);
+ return Timer._createPeriodicTimer(duration, callback);
}
- return Zone.current.createPeriodicTimer(
- duration, Zone.current.bindUnaryCallback(callback, runGuarded: true));
+ PeriodicTimerTaskSpecification specification =
+ new PeriodicTimerTaskSpecification(duration, callback);
+ Task task =
+ Zone.current.createTask(specification, _createPeriodicTimerTask);
+ return new _TimerTaskWrapper(task);
+ }
+
+ static TimerTask _createSingleShotTimerTask(
+ SingleShotTimerTaskSpecification specification, Zone zone) {
+ ZoneCallback registeredCallback =
+ zone.registerCallback(specification.callback);
+
+ TimerTask timerTask;
+
+ Timer timer = Timer._createTimer(specification.duration, () {
+ timerTask.zone.runTask(timerTask, null, _runSingleShotCallback);
+ });
+
+ timerTask = new SingleShotTimerTask(
+ timer, registeredCallback, specification, zone);
+ return timerTask;
+ }
+
+ static void _runSingleShotCallback(
+ SingleShotTimerTask timerTask, Object _, Zone zone) {
+ timerTask.callback();
+ }
+
+ static TimerTask _createPeriodicTimerTask(
+ PeriodicTimerTaskSpecification specification, Zone zone) {
+ ZoneUnaryCallback registeredCallback =
+ zone.registerUnaryCallback(specification.callback);
+
+ TimerTask timerTask;
+
+ Timer timer = Timer._createPeriodicTimer(specification.duration, (Timer _) {
+ timerTask.zone.runTask(timerTask, null, _runPeriodicCallback);
+ });
+
+ timerTask = new PeriodicTimerTask(
+ timer, registeredCallback, specification, zone);
+ return timerTask;
+ }
+
+ static void _runPeriodicCallback(
+ PeriodicTimerTask timerTask, Object _, Zone zone) {
+ // TODO(floitsch): cache the TimerWrapper?
+ timerTask.callback(new _TimerTaskWrapper(timerTask));
}
/**
« no previous file with comments | « no previous file | sdk/lib/async/zone.dart » ('j') | sdk/lib/async/zone.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698