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

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: Addressed 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
« no previous file with comments | « 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{
202 final canceller; 202 var _canceler;
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 callback(this);
209 }, milliSeconds);)
210 _canceler = () => window._clearInterval(id);
211 } else {
212 int id = window._setTimeout(() {
213 _canceler = null;
214 callback(this);
215 }, milliSeconds);)
216 _canceler = () => window._clearTimeout(id);
217 }
218 }
219
220 void cancel() {
221 if (_canceler != null) {
222 _canceler();
223 }
224 _canceler = null;
225 }
226
227 bool get isActive => _canceler != null;
207 } 228 }
208 229
209 get _timerFactoryClosure => (int milliSeconds, void callback(Timer timer), bool repeating) { 230 get _timerFactoryClosure =>
210 var maker; 231 (int milliSeconds, void callback(Timer timer), bool repeating) {
211 var canceller; 232 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 }; 233 };
224 234
235
225 class _PureIsolateTimer implements Timer { 236 class _PureIsolateTimer implements Timer {
237 bool _isActive = true;
226 final ReceivePort _port = new ReceivePort(); 238 final ReceivePort _port = new ReceivePort();
227 SendPort _sendPort; // Effectively final. 239 SendPort _sendPort; // Effectively final.
228 240
229 static SendPort _SEND_PORT; 241 static SendPort _SEND_PORT;
230 242
231 _PureIsolateTimer(int milliSeconds, callback, repeating) { 243 _PureIsolateTimer(int milliSeconds, callback, repeating) {
232 _sendPort = _port.toSendPort(); 244 _sendPort = _port.toSendPort();
233 _port.receive((msg, replyTo) { 245 _port.receive((msg, replyTo) {
234 assert(msg == _TIMER_PING); 246 assert(msg == _TIMER_PING);
247 _isActive = repeating;
235 callback(this); 248 callback(this);
236 if (!repeating) _cancel(); 249 if (!repeating) _cancel();
237 }); 250 });
238 251
239 _send([_NEW_TIMER, milliSeconds, repeating]); 252 _send([_NEW_TIMER, milliSeconds, repeating]);
240 } 253 }
241 254
242 void cancel() { 255 void cancel() {
243 _cancel(); 256 _cancel();
244 _send([_CANCEL_TIMER]); 257 _send([_CANCEL_TIMER]);
245 } 258 }
246 259
247 void _cancel() { 260 void _cancel() {
261 _isActive = false;
248 _port.close(); 262 _port.close();
249 } 263 }
250 264
251 _send(msg) { 265 _send(msg) {
252 _sendToHelperIsolate(msg, _sendPort); 266 _sendToHelperIsolate(msg, _sendPort);
253 } 267 }
268
269 bool get isActive => _isActive;
254 } 270 }
255 271
256 get _pureIsolateTimerFactoryClosure => 272 get _pureIsolateTimerFactoryClosure =>
257 ((int milliSeconds, void callback(Timer time), bool repeating) => 273 ((int milliSeconds, void callback(Timer time), bool repeating) =>
258 new _PureIsolateTimer(milliSeconds, callback, repeating)); 274 new _PureIsolateTimer(milliSeconds, callback, repeating));
OLDNEW
« no previous file with comments | « tests/lib/lib.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698