| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of html; | |
| 6 | |
| 7 class _Timer implements Timer { | |
| 8 final canceller; | |
| 9 | |
| 10 _Timer(this.canceller); | |
| 11 | |
| 12 void cancel() { canceller(); } | |
| 13 } | |
| 14 | |
| 15 get _timerFactoryClosure => (int milliSeconds, void callback(Timer timer), bool
repeating) { | |
| 16 var maker; | |
| 17 var canceller; | |
| 18 if (repeating) { | |
| 19 maker = window._setInterval; | |
| 20 canceller = window._clearInterval; | |
| 21 } else { | |
| 22 maker = window._setTimeout; | |
| 23 canceller = window._clearTimeout; | |
| 24 } | |
| 25 Timer timer; | |
| 26 final int id = maker(() { callback(timer); }, milliSeconds); | |
| 27 timer = new _Timer(() { canceller(id); }); | |
| 28 return timer; | |
| 29 }; | |
| 30 | |
| 31 class _PureIsolateTimer implements Timer { | |
| 32 final ReceivePort _port = new ReceivePort(); | |
| 33 SendPort _sendPort; // Effectively final. | |
| 34 | |
| 35 static SendPort _SEND_PORT; | |
| 36 | |
| 37 _PureIsolateTimer(int milliSeconds, callback, repeating) { | |
| 38 _sendPort = _port.toSendPort(); | |
| 39 _port.receive((msg, replyTo) { | |
| 40 assert(msg == _TIMER_PING); | |
| 41 callback(this); | |
| 42 if (!repeating) _cancel(); | |
| 43 }); | |
| 44 | |
| 45 _send([_NEW_TIMER, milliSeconds, repeating]); | |
| 46 } | |
| 47 | |
| 48 void cancel() { | |
| 49 _cancel(); | |
| 50 _send([_CANCEL_TIMER]); | |
| 51 } | |
| 52 | |
| 53 void _cancel() { | |
| 54 _port.close(); | |
| 55 } | |
| 56 | |
| 57 _send(msg) { | |
| 58 _sendToHelperIsolate(msg, _sendPort); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 get _pureIsolateTimerFactoryClosure => | |
| 63 ((int milliSeconds, void callback(Timer time), bool repeating) => | |
| 64 new _PureIsolateTimer(milliSeconds, callback, repeating)); | |
| OLD | NEW |