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 factory _Timer.timerFactoryClosure(int milliSeconds, |
|
floitsch
2013/07/01 17:50:56
Make this the default constructor. If you use the
zarah
2013/07/02 11:22:01
Done.
| |
| 208 void callback(Timer timer), | |
| 209 bool repeating) { | |
| 210 var maker; | |
| 211 var canceller; | |
| 212 if (repeating) { | |
| 213 maker = window._setInterval; | |
| 214 canceller = window._clearInterval; | |
| 215 } else { | |
| 216 maker = window._setTimeout; | |
| 217 canceller = window._clearTimeout; | |
| 218 } | |
| 219 Timer timer; | |
| 220 final int id = maker(() { _isDone = true; callback(timer);}, milliSeconds); | |
|
floitsch
2013/07/01 17:50:56
Don't put more than one statement on the same line
zarah
2013/07/02 11:22:01
Done.
| |
| 221 timer = new _Timer(() { canceller(id); }); | |
|
floitsch
2013/07/01 17:50:56
Nit: it is annoying to me that we wrap the cancele
zarah
2013/07/02 11:22:01
Done.
| |
| 222 return timer; | |
| 223 } | |
| 224 | |
| 225 void cancel() { | |
| 226 _isDone = true; | |
| 227 canceller(); | |
| 228 } | |
| 229 | |
| 230 bool get isActive => !_isDone; | |
| 207 } | 231 } |
| 208 | 232 |
| 209 get _timerFactoryClosure => (int milliSeconds, void callback(Timer timer), bool repeating) { | 233 get _timerFactoryClosure => |
| 210 var maker; | 234 (int milliSeconds, void callback(Timer timer), bool repeating) { |
| 211 var canceller; | 235 return _Timer.timerFactoryClosure(milliseconds, callback, repeating); |
| 212 if (repeating) { | |
| 213 maker = window._setInterval; | |
| 214 canceller = window._clearInterval; | |
| 215 } else { | |
| 216 maker = window._setTimeout; | |
| 217 canceller = window._clearTimeout; | |
| 218 } | |
| 219 Timer timer; | |
| 220 final int id = maker(() { callback(timer); }, milliSeconds); | |
| 221 timer = new _Timer(() { canceller(id); }); | |
| 222 return timer; | |
| 223 }; | 236 }; |
| 224 | 237 |
| 225 class _PureIsolateTimer implements Timer { | 238 class _PureIsolateTimer implements Timer { |
| 239 bool _isDone = false; | |
| 226 final ReceivePort _port = new ReceivePort(); | 240 final ReceivePort _port = new ReceivePort(); |
| 227 SendPort _sendPort; // Effectively final. | 241 SendPort _sendPort; // Effectively final. |
| 228 | 242 |
| 229 static SendPort _SEND_PORT; | 243 static SendPort _SEND_PORT; |
| 230 | 244 |
| 231 _PureIsolateTimer(int milliSeconds, callback, repeating) { | 245 _PureIsolateTimer(int milliSeconds, callback, repeating) { |
| 232 _sendPort = _port.toSendPort(); | 246 _sendPort = _port.toSendPort(); |
| 233 _port.receive((msg, replyTo) { | 247 _port.receive((msg, replyTo) { |
| 234 assert(msg == _TIMER_PING); | 248 assert(msg == _TIMER_PING); |
| 249 _isDone = !repeating; | |
| 235 callback(this); | 250 callback(this); |
| 236 if (!repeating) _cancel(); | 251 if (!repeating) _cancel(); |
| 237 }); | 252 }); |
| 238 | 253 |
| 239 _send([_NEW_TIMER, milliSeconds, repeating]); | 254 _send([_NEW_TIMER, milliSeconds, repeating]); |
| 240 } | 255 } |
| 241 | 256 |
| 242 void cancel() { | 257 void cancel() { |
| 243 _cancel(); | 258 _cancel(); |
| 244 _send([_CANCEL_TIMER]); | 259 _send([_CANCEL_TIMER]); |
| 245 } | 260 } |
| 246 | 261 |
| 247 void _cancel() { | 262 void _cancel() { |
| 263 _isDone = true; | |
| 248 _port.close(); | 264 _port.close(); |
| 249 } | 265 } |
| 250 | 266 |
| 251 _send(msg) { | 267 _send(msg) { |
| 252 _sendToHelperIsolate(msg, _sendPort); | 268 _sendToHelperIsolate(msg, _sendPort); |
| 253 } | 269 } |
| 270 | |
| 271 bool get isActive => !_isDone; | |
| 254 } | 272 } |
| 255 | 273 |
| 256 get _pureIsolateTimerFactoryClosure => | 274 get _pureIsolateTimerFactoryClosure => |
| 257 ((int milliSeconds, void callback(Timer time), bool repeating) => | 275 ((int milliSeconds, void callback(Timer time), bool repeating) => |
| 258 new _PureIsolateTimer(milliSeconds, callback, repeating)); | 276 new _PureIsolateTimer(milliSeconds, callback, repeating)); |
| OLD | NEW |