Chromium Code Reviews| Index: tools/dom/src/Timer.dart |
| diff --git a/tools/dom/src/Timer.dart b/tools/dom/src/Timer.dart |
| index 01048ea844913e43e04b6cb342eff985d46d9597..175376f992f7c1fed9007f67eab1e3458fc21bb6 100644 |
| --- a/tools/dom/src/Timer.dart |
| +++ b/tools/dom/src/Timer.dart |
| @@ -28,3 +28,58 @@ 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); |
|
vsm
2013/03/27 14:17:36
Is _HELPER_ISOLATE defined? I don't see it.
Anton Muhin
2013/03/27 14:32:19
May bad, fixed.
|
| + 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); |
|
vsm
2013/03/27 14:17:36
nit: length
Anton Muhin
2013/03/27 14:32:19
Done.
|
| +} |
| + |
| +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)); |