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

Unified Diff: tools/dom/src/Timer.dart

Issue 12594027: Support pure isolate timers. (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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/dom/src/Timer.dart
diff --git a/tools/dom/src/Timer.dart b/tools/dom/src/Timer.dart
index 01048ea844913e43e04b6cb342eff985d46d9597..b3e6bf44ce72f0022ae411627f91f83e202ceb5a 100644
--- a/tools/dom/src/Timer.dart
+++ b/tools/dom/src/Timer.dart
@@ -28,3 +28,59 @@ get _timerFactoryClosure => (int milliSeconds, void callback(Timer timer), bool
timer = new _Timer(() { canceller(id); });
return timer;
};
+
+const _NEW_TIMER = 'NEW_TIMER';
+const _CANCEL_TIMER = 'CANCEL_TIMER';
+const _TIMER_PING = 'TIMER_PING';
+
+class _PureIsolateTimer implements Timer {
+ final ReceivePort _port = new ReceivePort();
+ SendPort _sendPort; // Effectively final.
+
+ _PureIsolateTimer(int milliSeconds, callback, repeating) {
+ _sendPort = _port.toSendPort();
+ _port.receive((msg, replyTo) {
+ assert(msg == _TIMER_PING);
+ assert(replyTo == _HELPER_ISOLATE_PORT);
+ callback(this);
+ if (!repeating) _cancel();
+ });
+ _HELPER_ISOLATE_PORT.then((port) {
+ port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort);
+ });
+ }
+
+ void cancel() {
+ _cancel();
+ _HELPER_ISOLATE_PORT.send([_CANCEL_TIMER], _sendPort);
+ }
+
+ void _cancel() {
+ _port.close();
+ }
+
+ static final Future<SendPort> _HELPER_ISOLATE_PORT =
+ spawnDomFunction(_helperIsolateMain);
+}
+
+final _TIMER_REGISTRY = new Map<SendPort, Timer>();
+
+_helperIsolateMain() {
+ port.receive((msg, replyTo) {
+ final cmd = msg[0];
+ if (cmd == _NEW_TIMER) {
+ final duration = new Duration(milliseconds: msg[1]);
+ bool periodic = msg[2];
+ final callback = () { replyTo.send(_TIMER_PING); };
+ _TIMER_REGISTRY[replyTo] = periodic ?
+ new Timer.periodic(duration, callback) :
+ new Timer(duration, callback);
+ } else if (cmd == _CANCEL_TIMER) {
+ _TIMER_REGISTRY.remove(replyTo).cancel();
+ }
+ });
+}
+
+get _pureIsolateTimerFactoryClosure =>
+ ((int milliSeconds, void callback(Timer time), bool repeating) =>
+ new _PureIsolateTimer(milliSeconds, callback, repeating));
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698