Chromium Code Reviews| 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 class _Utils { | 7 class _Utils { |
| 8 static double dateTimeToDouble(DateTime dateTime) => | 8 static double dateTimeToDouble(DateTime dateTime) => |
| 9 dateTime.millisecondsSinceEpoch.toDouble(); | 9 dateTime.millisecondsSinceEpoch.toDouble(); |
| 10 static DateTime doubleToDateTime(double dateTime) { | 10 static DateTime doubleToDateTime(double dateTime) { |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 | 193 |
| 194 final _printClosure = window.console.log; | 194 final _printClosure = window.console.log; |
| 195 final _pureIsolatePrintClosure = (s) { | 195 final _pureIsolatePrintClosure = (s) { |
| 196 _sendToHelperIsolate([_PRINT, s], null); | 196 _sendToHelperIsolate([_PRINT, s], null); |
| 197 }; | 197 }; |
| 198 | 198 |
| 199 final _forwardingPrintClosure = _Utils.forwardingPrint; | 199 final _forwardingPrintClosure = _Utils.forwardingPrint; |
| 200 | 200 |
| 201 class _Timer implements Timer { | 201 class _Timer implements Timer { |
| 202 final canceller; | 202 final canceller; |
| 203 bool _isDone = false; | |
| 203 | 204 |
| 204 _Timer(this.canceller); | 205 _Timer(this.canceller); |
| 205 | 206 |
| 206 void cancel() { canceller(); } | 207 void cancel() { |
| 208 _isDone = true; | |
| 209 canceller(); | |
| 210 } | |
| 211 | |
| 212 bool get isActive => !_isDone; | |
| 207 } | 213 } |
| 208 | 214 |
| 209 get _timerFactoryClosure => (int milliSeconds, void callback(Timer timer), bool repeating) { | 215 get _timerFactoryClosure => (int milliSeconds, void callback(Timer timer), bool repeating) { |
| 210 var maker; | 216 var maker; |
| 211 var canceller; | 217 var canceller; |
| 212 if (repeating) { | 218 if (repeating) { |
| 213 maker = window._setInterval; | 219 maker = window._setInterval; |
| 214 canceller = window._clearInterval; | 220 canceller = window._clearInterval; |
| 215 } else { | 221 } else { |
| 216 maker = window._setTimeout; | 222 maker = window._setTimeout; |
| 217 canceller = window._clearTimeout; | 223 canceller = window._clearTimeout; |
| 218 } | 224 } |
| 219 Timer timer; | 225 Timer timer; |
| 220 final int id = maker(() { callback(timer); }, milliSeconds); | 226 final int id = maker(() { callback(timer); }, milliSeconds); |
|
floitsch
2013/07/01 12:49:43
This callback invocation will not set the _isDone
zarah
2013/07/01 16:15:35
Done.
| |
| 221 timer = new _Timer(() { canceller(id); }); | 227 timer = new _Timer(() { canceller(id); }); |
| 222 return timer; | 228 return timer; |
| 223 }; | 229 }; |
| 224 | 230 |
| 225 class _PureIsolateTimer implements Timer { | 231 class _PureIsolateTimer implements Timer { |
| 232 bool _isDone = false; | |
| 226 final ReceivePort _port = new ReceivePort(); | 233 final ReceivePort _port = new ReceivePort(); |
| 227 SendPort _sendPort; // Effectively final. | 234 SendPort _sendPort; // Effectively final. |
| 228 | 235 |
| 229 static SendPort _SEND_PORT; | 236 static SendPort _SEND_PORT; |
| 230 | 237 |
| 231 _PureIsolateTimer(int milliSeconds, callback, repeating) { | 238 _PureIsolateTimer(int milliSeconds, callback, repeating) { |
| 232 _sendPort = _port.toSendPort(); | 239 _sendPort = _port.toSendPort(); |
| 233 _port.receive((msg, replyTo) { | 240 _port.receive((msg, replyTo) { |
| 234 assert(msg == _TIMER_PING); | 241 assert(msg == _TIMER_PING); |
| 242 _isDone = !repeating; | |
| 235 callback(this); | 243 callback(this); |
| 236 if (!repeating) _cancel(); | 244 if (!repeating) _cancel(); |
| 237 }); | 245 }); |
| 238 | 246 |
| 239 _send([_NEW_TIMER, milliSeconds, repeating]); | 247 _send([_NEW_TIMER, milliSeconds, repeating]); |
| 240 } | 248 } |
| 241 | 249 |
| 242 void cancel() { | 250 void cancel() { |
| 243 _cancel(); | 251 _cancel(); |
| 244 _send([_CANCEL_TIMER]); | 252 _send([_CANCEL_TIMER]); |
| 245 } | 253 } |
| 246 | 254 |
| 247 void _cancel() { | 255 void _cancel() { |
| 256 _isDone = true; | |
| 248 _port.close(); | 257 _port.close(); |
| 249 } | 258 } |
| 250 | 259 |
| 251 _send(msg) { | 260 _send(msg) { |
| 252 _sendToHelperIsolate(msg, _sendPort); | 261 _sendToHelperIsolate(msg, _sendPort); |
| 253 } | 262 } |
| 263 | |
| 264 bool get isActive => !_isDone; | |
| 254 } | 265 } |
| 255 | 266 |
| 256 get _pureIsolateTimerFactoryClosure => | 267 get _pureIsolateTimerFactoryClosure => |
| 257 ((int milliSeconds, void callback(Timer time), bool repeating) => | 268 ((int milliSeconds, void callback(Timer time), bool repeating) => |
| 258 new _PureIsolateTimer(milliSeconds, callback, repeating)); | 269 new _PureIsolateTimer(milliSeconds, callback, repeating)); |
| OLD | NEW |