| 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 | |
| 36 class _PureIsolateTimer implements Timer { | 32 class _PureIsolateTimer implements Timer { |
| 37 final ReceivePort _port = new ReceivePort(); | 33 final ReceivePort _port = new ReceivePort(); |
| 38 SendPort _sendPort; // Effectively final. | 34 SendPort _sendPort; // Effectively final. |
| 39 | 35 |
| 40 _PureIsolateTimer(int milliSeconds, callback, repeating) { | 36 _PureIsolateTimer(int milliSeconds, callback, repeating) { |
| 41 _sendPort = _port.toSendPort(); | 37 _sendPort = _port.toSendPort(); |
| 42 _port.receive((msg, replyTo) { | 38 _port.receive((msg, replyTo) { |
| 43 assert(msg == _TIMER_PING); | 39 assert(msg == _TIMER_PING); |
| 44 callback(this); | 40 callback(this); |
| 45 if (!repeating) _cancel(); | 41 if (!repeating) _cancel(); |
| 46 }); | 42 }); |
| 47 _HELPER_ISOLATE_PORT.then((port) { | 43 _HELPER_ISOLATE_PORT.then((port) { |
| 48 port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); | 44 port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); |
| 49 }); | 45 }); |
| 50 } | 46 } |
| 51 | 47 |
| 52 void cancel() { | 48 void cancel() { |
| 53 _cancel(); | 49 _cancel(); |
| 54 _HELPER_ISOLATE_PORT.then((port) { | 50 _HELPER_ISOLATE_PORT.then((port) { |
| 55 port.send([_CANCEL_TIMER], _sendPort); | 51 port.send([_CANCEL_TIMER], _sendPort); |
| 56 }); | 52 }); |
| 57 } | 53 } |
| 58 | 54 |
| 59 void _cancel() { | 55 void _cancel() { |
| 60 _port.close(); | 56 _port.close(); |
| 61 } | 57 } |
| 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 }); | |
| 83 } | 58 } |
| 84 | 59 |
| 85 get _pureIsolateTimerFactoryClosure => | 60 get _pureIsolateTimerFactoryClosure => |
| 86 ((int milliSeconds, void callback(Timer time), bool repeating) => | 61 ((int milliSeconds, void callback(Timer time), bool repeating) => |
| 87 new _PureIsolateTimer(milliSeconds, callback, repeating)); | 62 new _PureIsolateTimer(milliSeconds, callback, repeating)); |
| OLD | NEW |