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 assert(replyTo == _HELPER_ISOLATE_PORT); | 40 assert(replyTo == _HELPER_ISOLATE_PORT); |
45 callback(this); | 41 callback(this); |
46 if (!repeating) _cancel(); | 42 if (!repeating) _cancel(); |
47 }); | 43 }); |
48 _HELPER_ISOLATE_PORT.then((port) { | 44 _HELPER_ISOLATE_PORT.then((port) { |
49 port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); | 45 port.send([_NEW_TIMER, milliSeconds, repeating], _sendPort); |
50 }); | 46 }); |
51 } | 47 } |
52 | 48 |
53 void cancel() { | 49 void cancel() { |
54 _cancel(); | 50 _cancel(); |
55 _HELPER_ISOLATE_PORT.then((port) { | 51 _HELPER_ISOLATE_PORT.then((port) { |
56 port.send([_CANCEL_TIMER], _sendPort); | 52 port.send([_CANCEL_TIMER], _sendPort); |
57 }); | 53 }); |
58 } | 54 } |
59 | 55 |
60 void _cancel() { | 56 void _cancel() { |
61 _port.close(); | 57 _port.close(); |
62 } | 58 } |
63 | |
64 static final Future<SendPort> _HELPER_ISOLATE_PORT = | |
65 spawnDomFunction(_helperIsolateMain); | |
66 } | |
67 | |
68 final _TIMER_REGISTRY = new Map<SendPort, Timer>(); | |
69 | |
70 _helperIsolateMain() { | |
71 port.receive((msg, replyTo) { | |
72 final cmd = msg[0]; | |
73 if (cmd == _NEW_TIMER) { | |
74 final duration = new Duration(milliseconds: msg[1]); | |
75 bool periodic = msg[2]; | |
76 final callback = () { replyTo.send(_TIMER_PING); }; | |
77 _TIMER_REGISTRY[replyTo] = periodic ? | |
78 new Timer.periodic(duration, callback) : | |
79 new Timer(duration, callback); | |
80 } else if (cmd == _CANCEL_TIMER) { | |
81 _TIMER_REGISTRY.remove(replyTo).cancel(); | |
82 } | |
83 }); | |
84 } | 59 } |
85 | 60 |
86 get _pureIsolateTimerFactoryClosure => | 61 get _pureIsolateTimerFactoryClosure => |
87 ((int milliSeconds, void callback(Timer time), bool repeating) => | 62 ((int milliSeconds, void callback(Timer time), bool repeating) => |
88 new _PureIsolateTimer(milliSeconds, callback, repeating)); | 63 new _PureIsolateTimer(milliSeconds, callback, repeating)); |
OLD | NEW |