Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: tools/dom/src/native_DOMImplementation.dart

Issue 18325006: Add isActive field on Timer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressd review comments. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« sdk/lib/io/timer_impl.dart ('K') | « tests/lib/lib.status ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 }); 191 });
192 } 192 }
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 {
floitsch 2013/07/02 11:27:19 Remove leading space.
zarah 2013/07/02 11:43:12 Done.
202 final canceller; 202 final _canceler;
floitsch 2013/07/02 11:27:19 Can not be final, since you change it.
zarah 2013/07/02 11:43:12 Done, of course!
203 203
204 _Timer(this.canceller); 204 _Timer(int milliSeconds, void callback(Timer timer), bool repeating) {
205 205
206 void cancel() { canceller(); } 206 if (repeating) {
207 int id = window._setInterval(() {
208 _canceler = null;
floitsch 2013/07/02 11:27:19 No. Don't remove the canceler for repeating timers
zarah 2013/07/02 11:43:12 Done.
209 callback(this);
210 }, milliSeconds);)
211 _canceler = () => window._clearInterval(id);
212 } else {
213 int id = window._setTimeout(() {
214 _canceler = null;
215 callback(this);
216 }, milliSeconds); )
217 _canceler = window._clearTimeout(id);
floitsch 2013/07/02 11:27:19 This must be a closure.
zarah 2013/07/02 11:43:12 Done.
218 }
219 }
220
221 void cancel() {
222 if (_canceler != null) {
223 _canceler();
224 }
225 _canceler = null;
226 }
227
228 bool get isActive => _canceler != null;
207 } 229 }
208 230
209 get _timerFactoryClosure => (int milliSeconds, void callback(Timer timer), bool repeating) { 231 get _timerFactoryClosure =>
210 var maker; 232 (int milliSeconds, void callback(Timer timer), bool repeating) {
211 var canceller; 233 return new _Timer(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 }; 234 };
224 235
236
225 class _PureIsolateTimer implements Timer { 237 class _PureIsolateTimer implements Timer {
238 bool _isDone = false;
floitsch 2013/07/02 11:27:19 Rename _isDone to _isActive
zarah 2013/07/02 11:43:12 Done.
226 final ReceivePort _port = new ReceivePort(); 239 final ReceivePort _port = new ReceivePort();
227 SendPort _sendPort; // Effectively final. 240 SendPort _sendPort; // Effectively final.
228 241
229 static SendPort _SEND_PORT; 242 static SendPort _SEND_PORT;
230 243
231 _PureIsolateTimer(int milliSeconds, callback, repeating) { 244 _PureIsolateTimer(int milliSeconds, callback, repeating) {
232 _sendPort = _port.toSendPort(); 245 _sendPort = _port.toSendPort();
233 _port.receive((msg, replyTo) { 246 _port.receive((msg, replyTo) {
234 assert(msg == _TIMER_PING); 247 assert(msg == _TIMER_PING);
248 _isDone = !repeating;
235 callback(this); 249 callback(this);
236 if (!repeating) _cancel(); 250 if (!repeating) _cancel();
237 }); 251 });
238 252
239 _send([_NEW_TIMER, milliSeconds, repeating]); 253 _send([_NEW_TIMER, milliSeconds, repeating]);
240 } 254 }
241 255
242 void cancel() { 256 void cancel() {
243 _cancel(); 257 _cancel();
244 _send([_CANCEL_TIMER]); 258 _send([_CANCEL_TIMER]);
245 } 259 }
246 260
247 void _cancel() { 261 void _cancel() {
262 _isDone = true;
248 _port.close(); 263 _port.close();
249 } 264 }
250 265
251 _send(msg) { 266 _send(msg) {
252 _sendToHelperIsolate(msg, _sendPort); 267 _sendToHelperIsolate(msg, _sendPort);
253 } 268 }
269
270 bool get isActive => !_isDone;
254 } 271 }
255 272
256 get _pureIsolateTimerFactoryClosure => 273 get _pureIsolateTimerFactoryClosure =>
257 ((int milliSeconds, void callback(Timer time), bool repeating) => 274 ((int milliSeconds, void callback(Timer time), bool repeating) =>
258 new _PureIsolateTimer(milliSeconds, callback, repeating)); 275 new _PureIsolateTimer(milliSeconds, callback, repeating));
OLDNEW
« sdk/lib/io/timer_impl.dart ('K') | « tests/lib/lib.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698