Chromium Code Reviews| Index: tools/dom/src/Timer.dart |
| diff --git a/tools/dom/src/Timer.dart b/tools/dom/src/Timer.dart |
| index 43a208afe0a43f0a43360e1361ec6cc656f1d36b..9fa22617829bb7960851264f2c22d8ae0dee7ae9 100644 |
| --- a/tools/dom/src/Timer.dart |
| +++ b/tools/dom/src/Timer.dart |
| @@ -37,6 +37,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) { |
| @@ -45,9 +47,22 @@ class _PureIsolateTimer implements Timer { |
| callback(this); |
| if (!repeating) _cancel(); |
| }); |
| - _HELPER_ISOLATE_PORT.then((port) { |
| - port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); |
| - }); |
| + |
| + registerTimer() { |
| + _SEND_PORT.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); |
| + } |
| + |
| + // Tricky part: Future.then is always delayed might prevent |
| + // new timer registration if many events come to this isolate. |
| + // Therefore, let's resort to then only if really necessary. |
|
vsm
2013/03/29 14:37:39
I'm confused by this. What was the problem you we
Anton Muhin
2013/03/29 16:09:15
(whatever outcome of our discuss be, I need to mod
vsm
2013/03/29 17:34:36
I think I'm starting to get this - was the old cod
Anton Muhin
2013/04/01 14:52:57
I've updated the comment. May you, please, tell m
|
| + if (_SEND_PORT != null) { |
| + registerTimer(); |
| + } else { |
| + _HELPER_ISOLATE_PORT.then((port) { |
| + _SEND_PORT = port; |
| + registerTimer(); |
| + }); |
| + } |
| } |
| void cancel() { |
| @@ -73,12 +88,13 @@ _helperIsolateMain() { |
| if (cmd == _NEW_TIMER) { |
| final duration = new Duration(milliseconds: msg[1]); |
| bool periodic = msg[2]; |
| - final callback = () { replyTo.send(_TIMER_PING); }; |
| + ping() { replyTo.send(_TIMER_PING); } |
| _TIMER_REGISTRY[replyTo] = periodic ? |
| - new Timer.periodic(duration, callback) : |
| - new Timer(duration, callback); |
| + new Timer.periodic(duration, (_) { ping(); }) : |
| + new Timer(duration, ping); |
| } else if (cmd == _CANCEL_TIMER) { |
| - _TIMER_REGISTRY.remove(replyTo).cancel(); |
| + Timer timer = _TIMER_REGISTRY.remove(replyTo); |
| + if (timer != null) timer.cancel(); |
| } |
| }); |
| } |