| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of html; | 5 part of html; |
| 6 | 6 |
| 7 // TODO(antonm): support not DOM isolates too. | 7 // TODO(antonm): support not DOM isolates too. |
| 8 class _Timer implements Timer { | 8 class _Timer implements Timer { |
| 9 final canceller; | 9 final canceller; |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 } else { | 22 } else { |
| 23 maker = window._setTimeout; | 23 maker = window._setTimeout; |
| 24 canceller = window._clearTimeout; | 24 canceller = window._clearTimeout; |
| 25 } | 25 } |
| 26 Timer timer; | 26 Timer timer; |
| 27 final int id = maker(() { callback(timer); }, milliSeconds); | 27 final int id = maker(() { callback(timer); }, milliSeconds); |
| 28 timer = new _Timer(() { canceller(id); }); | 28 timer = new _Timer(() { canceller(id); }); |
| 29 return timer; | 29 return timer; |
| 30 }; | 30 }; |
| 31 | 31 |
| 32 const _NEW_TIMER = 'NEW_TIMER'; |
| 33 const _CANCEL_TIMER = 'CANCEL_TIMER'; |
| 34 const _TIMER_PING = 'TIMER_PING'; |
| 35 |
| 32 class _PureIsolateTimer implements Timer { | 36 class _PureIsolateTimer implements Timer { |
| 33 final ReceivePort _port = new ReceivePort(); | 37 final ReceivePort _port = new ReceivePort(); |
| 34 SendPort _sendPort; // Effectively final. | 38 SendPort _sendPort; // Effectively final. |
| 35 | 39 |
| 36 _PureIsolateTimer(int milliSeconds, callback, repeating) { | 40 _PureIsolateTimer(int milliSeconds, callback, repeating) { |
| 37 _sendPort = _port.toSendPort(); | 41 _sendPort = _port.toSendPort(); |
| 38 _port.receive((msg, replyTo) { | 42 _port.receive((msg, replyTo) { |
| 39 assert(msg == _TIMER_PING); | 43 assert(msg == _TIMER_PING); |
| 40 callback(this); | 44 callback(this); |
| 41 if (!repeating) _cancel(); | 45 if (!repeating) _cancel(); |
| 42 }); | 46 }); |
| 43 _HELPER_ISOLATE_PORT.then((port) { | 47 _HELPER_ISOLATE_PORT.then((port) { |
| 44 port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); | 48 port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); |
| 45 }); | 49 }); |
| 46 } | 50 } |
| 47 | 51 |
| 48 void cancel() { | 52 void cancel() { |
| 49 _cancel(); | 53 _cancel(); |
| 50 _HELPER_ISOLATE_PORT.then((port) { | 54 _HELPER_ISOLATE_PORT.then((port) { |
| 51 port.send([_CANCEL_TIMER], _sendPort); | 55 port.send([_CANCEL_TIMER], _sendPort); |
| 52 }); | 56 }); |
| 53 } | 57 } |
| 54 | 58 |
| 55 void _cancel() { | 59 void _cancel() { |
| 56 _port.close(); | 60 _port.close(); |
| 57 } | 61 } |
| 62 |
| 63 static final Future<SendPort> _HELPER_ISOLATE_PORT = |
| 64 spawnDomFunction(_helperIsolateMain); |
| 65 } |
| 66 |
| 67 final _TIMER_REGISTRY = new Map<SendPort, Timer>(); |
| 68 |
| 69 _helperIsolateMain() { |
| 70 port.receive((msg, replyTo) { |
| 71 final cmd = msg[0]; |
| 72 if (cmd == _NEW_TIMER) { |
| 73 final duration = new Duration(milliseconds: msg[1]); |
| 74 bool periodic = msg[2]; |
| 75 final callback = () { replyTo.send(_TIMER_PING); }; |
| 76 _TIMER_REGISTRY[replyTo] = periodic ? |
| 77 new Timer.periodic(duration, callback) : |
| 78 new Timer(duration, callback); |
| 79 } else if (cmd == _CANCEL_TIMER) { |
| 80 _TIMER_REGISTRY.remove(replyTo).cancel(); |
| 81 } |
| 82 }); |
| 58 } | 83 } |
| 59 | 84 |
| 60 get _pureIsolateTimerFactoryClosure => | 85 get _pureIsolateTimerFactoryClosure => |
| 61 ((int milliSeconds, void callback(Timer time), bool repeating) => | 86 ((int milliSeconds, void callback(Timer time), bool repeating) => |
| 62 new _PureIsolateTimer(milliSeconds, callback, repeating)); | 87 new _PureIsolateTimer(milliSeconds, callback, repeating)); |
| OLD | NEW |