Chromium Code Reviews| Index: tools/dom/src/Timer.dart |
| diff --git a/tools/dom/src/Timer.dart b/tools/dom/src/Timer.dart |
| index da98db52278f60fce37c31a802c271ec7331598c..8d3660976a89b6319a3c335345ed5dac3014f78f 100644 |
| --- a/tools/dom/src/Timer.dart |
| +++ b/tools/dom/src/Timer.dart |
| @@ -33,6 +33,8 @@ class _PureIsolateTimer implements Timer { |
| final ReceivePort _port = new ReceivePort(); |
| SendPort _sendPort; // Effectively final. |
| + static SendPort _SEND_PORT; |
| + |
| _PureIsolateTimer(int milliSeconds, callback, repeating) { |
| _sendPort = _port.toSendPort(); |
| _port.receive((msg, replyTo) { |
| @@ -40,21 +42,36 @@ class _PureIsolateTimer implements Timer { |
| callback(this); |
| if (!repeating) _cancel(); |
| }); |
| - _HELPER_ISOLATE_PORT.then((port) { |
| - port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); |
| - }); |
| + |
| + _send([_NEW_TIMER, milliSeconds, repeating]); |
| } |
| void cancel() { |
| _cancel(); |
| - _HELPER_ISOLATE_PORT.then((port) { |
| - port.send([_CANCEL_TIMER], _sendPort); |
| - }); |
| + _send([_CANCEL_TIMER]); |
| } |
| void _cancel() { |
| _port.close(); |
| } |
| + |
| + // Tricky part. |
| + // Once _HELPER_ISOLATE_PORT gets resolved, it will still delay in .then |
| + // and to delay Timer.run is used. However, Timer.run will try to register |
| + // another Timer and here we got stuck: event cannot be posted as then |
| + // callback is not executed because it's delayed with timer. |
| + // Therefore once future is resolved, it's unsafe to call .then on it |
| + // in Timer code. |
|
vsm
2013/04/01 15:27:32
Thanks, this looks good.
|
| + _send(msg) { |
| + if (_SEND_PORT != null) { |
| + _SEND_PORT.send(msg, _sendPort); |
| + } else { |
| + _HELPER_ISOLATE_PORT.then((port) { |
| + _SEND_PORT = port; |
| + _SEND_PORT.send(msg, _sendPort); |
| + }); |
| + } |
| + } |
| } |
| get _pureIsolateTimerFactoryClosure => |