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

Side by Side Diff: pkg/dev_compiler/tool/input_sdk/private/isolate_helper.dart

Issue 3003853002: Add ticks counter to Timer. (Closed)
Patch Set: Test that tick increments by more than one Created 3 years, 3 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
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 library dart._isolate_helper; 5 library dart._isolate_helper;
6 6
7 import 'dart:_js_embedded_names' 7 import 'dart:_js_embedded_names'
8 show 8 show
9 CLASS_ID_EXTRACTOR, 9 CLASS_ID_EXTRACTOR,
10 CLASS_FIELDS_EXTRACTOR, 10 CLASS_FIELDS_EXTRACTOR,
(...skipping 1337 matching lines...) Expand 10 before | Expand all | Expand 10 after
1348 _controller.close(); 1348 _controller.close();
1349 } 1349 }
1350 1350
1351 SendPort get sendPort => _rawPort.sendPort; 1351 SendPort get sendPort => _rawPort.sendPort;
1352 } 1352 }
1353 1353
1354 class TimerImpl implements Timer { 1354 class TimerImpl implements Timer {
1355 final bool _once; 1355 final bool _once;
1356 bool _inEventLoop = false; 1356 bool _inEventLoop = false;
1357 int _handle; 1357 int _handle;
1358 int _tick = 0;
1358 1359
1359 TimerImpl(int milliseconds, void callback()) : _once = true { 1360 TimerImpl(int milliseconds, void callback()) : _once = true {
1360 if (milliseconds == 0 && (!hasTimer() || _globalState.isWorker)) { 1361 if (milliseconds == 0 && (!hasTimer() || _globalState.isWorker)) {
1361 void internalCallback() { 1362 void internalCallback() {
1362 _handle = null; 1363 _handle = null;
1363 callback(); 1364 callback();
1364 } 1365 }
1365 1366
1366 // Setting _handle to something different from null indicates that the 1367 // Setting _handle to something different from null indicates that the
1367 // callback has not been run. Hence, the choice of 1 is arbitrary. 1368 // callback has not been run. Hence, the choice of 1 is arbitrary.
1368 _handle = 1; 1369 _handle = 1;
1369 1370
1370 // This makes a dependency between the async library and the 1371 // This makes a dependency between the async library and the
1371 // event loop of the isolate library. The compiler makes sure 1372 // event loop of the isolate library. The compiler makes sure
1372 // that the event loop is compiled if [Timer] is used. 1373 // that the event loop is compiled if [Timer] is used.
1373 // TODO(7907): In case of web workers, we need to use the event 1374 // TODO(7907): In case of web workers, we need to use the event
1374 // loop instead of setTimeout, to make sure the futures get executed in 1375 // loop instead of setTimeout, to make sure the futures get executed in
1375 // order. 1376 // order.
1376 _globalState.topEventLoop 1377 _globalState.topEventLoop
1377 .enqueue(_globalState.currentContext, internalCallback, 'timer'); 1378 .enqueue(_globalState.currentContext, internalCallback, 'timer');
1378 _inEventLoop = true; 1379 _inEventLoop = true;
1379 } else if (hasTimer()) { 1380 } else if (hasTimer()) {
1380 void internalCallback() { 1381 void internalCallback() {
1381 _handle = null; 1382 _handle = null;
1382 leaveJsAsync(); 1383 leaveJsAsync();
1384 _tick = 1;
1383 callback(); 1385 callback();
1384 } 1386 }
1385 1387
1386 enterJsAsync(); 1388 enterJsAsync();
1387 1389
1388 _handle = JS( 1390 _handle = JS(
1389 'int', '#.setTimeout(#, #)', global, internalCallback, milliseconds); 1391 'int', '#.setTimeout(#, #)', global, internalCallback, milliseconds);
1390 } else { 1392 } else {
1391 assert(milliseconds > 0); 1393 assert(milliseconds > 0);
1392 throw new UnsupportedError("Timer greater than 0."); 1394 throw new UnsupportedError("Timer greater than 0.");
1393 } 1395 }
1394 } 1396 }
1395 1397
1396 TimerImpl.periodic(int milliseconds, void callback(Timer timer)) 1398 TimerImpl.periodic(int milliseconds, void callback(Timer timer))
1397 : _once = false { 1399 : _once = false {
1398 if (hasTimer()) { 1400 if (hasTimer()) {
1399 enterJsAsync(); 1401 enterJsAsync();
1402 int start = JS('int', 'Date.now()');
1400 _handle = JS('int', '#.setInterval(#, #)', global, () { 1403 _handle = JS('int', '#.setInterval(#, #)', global, () {
1404 int tick = this._tick + 1;
1405 if (milliseconds > 0) {
1406 int duration = JS('int', 'Date.now()') - start;
1407 if (duration > (tick + 1) * milliseconds) {
1408 tick = duration ~/ milliseconds;
1409 }
1410 }
1411 this._tick = tick;
1401 callback(this); 1412 callback(this);
1402 }, milliseconds); 1413 }, milliseconds);
1403 } else { 1414 } else {
1404 throw new UnsupportedError("Periodic timer."); 1415 throw new UnsupportedError("Periodic timer.");
1405 } 1416 }
1406 } 1417 }
1407 1418
1419 int get tick => _tick;
1420
1408 void cancel() { 1421 void cancel() {
1409 if (hasTimer()) { 1422 if (hasTimer()) {
1410 if (_inEventLoop) { 1423 if (_inEventLoop) {
1411 throw new UnsupportedError("Timer in event loop cannot be canceled."); 1424 throw new UnsupportedError("Timer in event loop cannot be canceled.");
1412 } 1425 }
1413 if (_handle == null) return; 1426 if (_handle == null) return;
1414 leaveJsAsync(); 1427 leaveJsAsync();
1415 if (_once) { 1428 if (_once) {
1416 JS('void', '#.clearTimeout(#)', global, _handle); 1429 JS('void', '#.clearTimeout(#)', global, _handle);
1417 } else { 1430 } else {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1459 } 1472 }
1460 1473
1461 bool operator ==(Object other) { 1474 bool operator ==(Object other) {
1462 if (identical(other, this)) return true; 1475 if (identical(other, this)) return true;
1463 if (other is CapabilityImpl) { 1476 if (other is CapabilityImpl) {
1464 return identical(_id, other._id); 1477 return identical(_id, other._id);
1465 } 1478 }
1466 return false; 1479 return false;
1467 } 1480 }
1468 } 1481 }
OLDNEW
« no previous file with comments | « no previous file | pkg/dev_compiler/tool/input_sdk/private/preambles/d8.js » ('j') | sdk/lib/async/timer.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698