| 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 _isolate_helper; | 5 library _isolate_helper; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection' show Queue, HashMap; | 8 import 'dart:collection' show Queue, HashMap; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 import 'dart:_js_helper' show convertDartClosureToJS, | 10 import 'dart:_js_helper' show convertDartClosureToJS, |
| (...skipping 1342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1353 } | 1353 } |
| 1354 | 1354 |
| 1355 class TimerImpl implements Timer { | 1355 class TimerImpl implements Timer { |
| 1356 final bool _once; | 1356 final bool _once; |
| 1357 bool _inEventLoop = false; | 1357 bool _inEventLoop = false; |
| 1358 int _handle; | 1358 int _handle; |
| 1359 | 1359 |
| 1360 TimerImpl(int milliseconds, void callback()) | 1360 TimerImpl(int milliseconds, void callback()) |
| 1361 : _once = true { | 1361 : _once = true { |
| 1362 if (milliseconds == 0 && (!hasTimer() || _globalState.isWorker)) { | 1362 if (milliseconds == 0 && (!hasTimer() || _globalState.isWorker)) { |
| 1363 |
| 1364 void internalCallback() { |
| 1365 _handle = null; |
| 1366 callback(); |
| 1367 } |
| 1368 |
| 1369 // Setting _handle to something different from null indicates that the |
| 1370 // callback has not been run. Hence, the choice of 1 is arbitrary. |
| 1371 _handle = 1; |
| 1372 |
| 1363 // This makes a dependency between the async library and the | 1373 // This makes a dependency between the async library and the |
| 1364 // event loop of the isolate library. The compiler makes sure | 1374 // event loop of the isolate library. The compiler makes sure |
| 1365 // that the event loop is compiled if [Timer] is used. | 1375 // that the event loop is compiled if [Timer] is used. |
| 1366 // TODO(7907): In case of web workers, we need to use the event | 1376 // TODO(7907): In case of web workers, we need to use the event |
| 1367 // loop instead of setTimeout, to make sure the futures get executed in | 1377 // loop instead of setTimeout, to make sure the futures get executed in |
| 1368 // order. | 1378 // order. |
| 1369 _globalState.topEventLoop.enqueue( | 1379 _globalState.topEventLoop.enqueue( |
| 1370 _globalState.currentContext, callback, 'timer'); | 1380 _globalState.currentContext, internalCallback, 'timer'); |
| 1371 _inEventLoop = true; | 1381 _inEventLoop = true; |
| 1372 } else if (hasTimer()) { | 1382 } else if (hasTimer()) { |
| 1373 _globalState.topEventLoop.activeTimerCount++; | 1383 |
| 1374 void internalCallback() { | 1384 void internalCallback() { |
| 1375 callback(); | |
| 1376 _handle = null; | 1385 _handle = null; |
| 1377 _globalState.topEventLoop.activeTimerCount--; | 1386 _globalState.topEventLoop.activeTimerCount--; |
| 1387 callback(); |
| 1378 } | 1388 } |
| 1389 |
| 1390 _globalState.topEventLoop.activeTimerCount++; |
| 1379 _handle = JS('int', '#.setTimeout(#, #)', | 1391 _handle = JS('int', '#.setTimeout(#, #)', |
| 1380 globalThis, | 1392 globalThis, |
| 1381 convertDartClosureToJS(internalCallback, 0), | 1393 convertDartClosureToJS(internalCallback, 0), |
| 1382 milliseconds); | 1394 milliseconds); |
| 1383 } else { | 1395 } else { |
| 1384 assert(milliseconds > 0); | 1396 assert(milliseconds > 0); |
| 1385 throw new UnsupportedError("Timer greater than 0."); | 1397 throw new UnsupportedError("Timer greater than 0."); |
| 1386 } | 1398 } |
| 1387 } | 1399 } |
| 1388 | 1400 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1409 if (_once) { | 1421 if (_once) { |
| 1410 JS('void', '#.clearTimeout(#)', globalThis, _handle); | 1422 JS('void', '#.clearTimeout(#)', globalThis, _handle); |
| 1411 } else { | 1423 } else { |
| 1412 JS('void', '#.clearInterval(#)', globalThis, _handle); | 1424 JS('void', '#.clearInterval(#)', globalThis, _handle); |
| 1413 } | 1425 } |
| 1414 _handle = null; | 1426 _handle = null; |
| 1415 } else { | 1427 } else { |
| 1416 throw new UnsupportedError("Canceling a timer."); | 1428 throw new UnsupportedError("Canceling a timer."); |
| 1417 } | 1429 } |
| 1418 } | 1430 } |
| 1431 |
| 1432 bool get isActive => _handle != null; |
| 1419 } | 1433 } |
| 1420 | 1434 |
| 1421 bool hasTimer() => JS('', '#.setTimeout', globalThis) != null; | 1435 bool hasTimer() => JS('', '#.setTimeout', globalThis) != null; |
| OLD | NEW |