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 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 Loading... |
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; | |
1359 | 1358 |
1360 TimerImpl(int milliseconds, void callback()) : _once = true { | 1359 TimerImpl(int milliseconds, void callback()) : _once = true { |
1361 if (milliseconds == 0 && (!hasTimer() || _globalState.isWorker)) { | 1360 if (milliseconds == 0 && (!hasTimer() || _globalState.isWorker)) { |
1362 void internalCallback() { | 1361 void internalCallback() { |
1363 _handle = null; | 1362 _handle = null; |
1364 callback(); | 1363 callback(); |
1365 } | 1364 } |
1366 | 1365 |
1367 // Setting _handle to something different from null indicates that the | 1366 // Setting _handle to something different from null indicates that the |
1368 // callback has not been run. Hence, the choice of 1 is arbitrary. | 1367 // callback has not been run. Hence, the choice of 1 is arbitrary. |
1369 _handle = 1; | 1368 _handle = 1; |
1370 | 1369 |
1371 // This makes a dependency between the async library and the | 1370 // This makes a dependency between the async library and the |
1372 // event loop of the isolate library. The compiler makes sure | 1371 // event loop of the isolate library. The compiler makes sure |
1373 // that the event loop is compiled if [Timer] is used. | 1372 // that the event loop is compiled if [Timer] is used. |
1374 // TODO(7907): In case of web workers, we need to use the event | 1373 // TODO(7907): In case of web workers, we need to use the event |
1375 // loop instead of setTimeout, to make sure the futures get executed in | 1374 // loop instead of setTimeout, to make sure the futures get executed in |
1376 // order. | 1375 // order. |
1377 _globalState.topEventLoop | 1376 _globalState.topEventLoop |
1378 .enqueue(_globalState.currentContext, internalCallback, 'timer'); | 1377 .enqueue(_globalState.currentContext, internalCallback, 'timer'); |
1379 _inEventLoop = true; | 1378 _inEventLoop = true; |
1380 } else if (hasTimer()) { | 1379 } else if (hasTimer()) { |
1381 void internalCallback() { | 1380 void internalCallback() { |
1382 _handle = null; | 1381 _handle = null; |
1383 leaveJsAsync(); | 1382 leaveJsAsync(); |
1384 _tick = 1; | |
1385 callback(); | 1383 callback(); |
1386 } | 1384 } |
1387 | 1385 |
1388 enterJsAsync(); | 1386 enterJsAsync(); |
1389 | 1387 |
1390 _handle = JS( | 1388 _handle = JS( |
1391 'int', '#.setTimeout(#, #)', global, internalCallback, milliseconds); | 1389 'int', '#.setTimeout(#, #)', global, internalCallback, milliseconds); |
1392 } else { | 1390 } else { |
1393 assert(milliseconds > 0); | 1391 assert(milliseconds > 0); |
1394 throw new UnsupportedError("Timer greater than 0."); | 1392 throw new UnsupportedError("Timer greater than 0."); |
1395 } | 1393 } |
1396 } | 1394 } |
1397 | 1395 |
1398 TimerImpl.periodic(int milliseconds, void callback(Timer timer)) | 1396 TimerImpl.periodic(int milliseconds, void callback(Timer timer)) |
1399 : _once = false { | 1397 : _once = false { |
1400 if (hasTimer()) { | 1398 if (hasTimer()) { |
1401 enterJsAsync(); | 1399 enterJsAsync(); |
1402 int start = JS('int', 'Date.now()'); | |
1403 _handle = JS('int', '#.setInterval(#, #)', global, () { | 1400 _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; | |
1412 callback(this); | 1401 callback(this); |
1413 }, milliseconds); | 1402 }, milliseconds); |
1414 } else { | 1403 } else { |
1415 throw new UnsupportedError("Periodic timer."); | 1404 throw new UnsupportedError("Periodic timer."); |
1416 } | 1405 } |
1417 } | 1406 } |
1418 | 1407 |
1419 int get tick => _tick; | |
1420 | |
1421 void cancel() { | 1408 void cancel() { |
1422 if (hasTimer()) { | 1409 if (hasTimer()) { |
1423 if (_inEventLoop) { | 1410 if (_inEventLoop) { |
1424 throw new UnsupportedError("Timer in event loop cannot be canceled."); | 1411 throw new UnsupportedError("Timer in event loop cannot be canceled."); |
1425 } | 1412 } |
1426 if (_handle == null) return; | 1413 if (_handle == null) return; |
1427 leaveJsAsync(); | 1414 leaveJsAsync(); |
1428 if (_once) { | 1415 if (_once) { |
1429 JS('void', '#.clearTimeout(#)', global, _handle); | 1416 JS('void', '#.clearTimeout(#)', global, _handle); |
1430 } else { | 1417 } else { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1472 } | 1459 } |
1473 | 1460 |
1474 bool operator ==(Object other) { | 1461 bool operator ==(Object other) { |
1475 if (identical(other, this)) return true; | 1462 if (identical(other, this)) return true; |
1476 if (other is CapabilityImpl) { | 1463 if (other is CapabilityImpl) { |
1477 return identical(_id, other._id); | 1464 return identical(_id, other._id); |
1478 } | 1465 } |
1479 return false; | 1466 return false; |
1480 } | 1467 } |
1481 } | 1468 } |
OLD | NEW |