| 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:isolate'; | 8 import 'dart:isolate'; |
| 9 | 9 |
| 10 ReceivePort lazyPort; | 10 ReceivePort lazyPort; |
| (...skipping 1236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1247 throw "Unexpected serialized object"; | 1247 throw "Unexpected serialized object"; |
| 1248 } | 1248 } |
| 1249 } | 1249 } |
| 1250 | 1250 |
| 1251 class TimerImpl implements Timer { | 1251 class TimerImpl implements Timer { |
| 1252 final bool _once; | 1252 final bool _once; |
| 1253 int _handle; | 1253 int _handle; |
| 1254 | 1254 |
| 1255 TimerImpl(int milliseconds, void callback(Timer timer)) | 1255 TimerImpl(int milliseconds, void callback(Timer timer)) |
| 1256 : _once = true { | 1256 : _once = true { |
| 1257 _handle = JS('int', '#.setTimeout(#, #)', | 1257 if (hasTimer() && !_globalState.isWorker) { |
| 1258 globalThis, | 1258 _handle = JS('int', '#.setTimeout(#, #)', |
| 1259 convertDartClosureToJS(() => callback(this), 0), | 1259 globalThis, |
| 1260 milliseconds); | 1260 convertDartClosureToJS(() => callback(this), 0), |
| 1261 milliseconds); |
| 1262 } else if (milliseconds != 0) { |
| 1263 throw new UnsupportedError("Non DOM isolate with timer greater than 0."); |
| 1264 } else { |
| 1265 // This makes a dependency between the async library and the |
| 1266 // event loop of the isolate library. The compiler makes sure |
| 1267 // that the event loop is compiled if [Timer] is used. |
| 1268 _globalState.topEventLoop.enqueue(_globalState.currentContext, () { |
| 1269 callback(this); |
| 1270 }, 'timer'); |
| 1271 } |
| 1261 } | 1272 } |
| 1262 | 1273 |
| 1263 TimerImpl.repeating(int milliseconds, void callback(Timer timer)) | 1274 TimerImpl.repeating(int milliseconds, void callback(Timer timer)) |
| 1264 : _once = false { | 1275 : _once = false { |
| 1265 _handle = JS('int', '#.setInterval(#, #)', | 1276 if (hasTimer() && !_globalState.isWorker) { |
| 1266 globalThis, | 1277 _handle = JS('int', '#.setInterval(#, #)', |
| 1267 convertDartClosureToJS(() => callback(this), 0), | 1278 globalThis, |
| 1268 milliseconds); | 1279 convertDartClosureToJS(() => callback(this), 0), |
| 1280 milliseconds); |
| 1281 } else { |
| 1282 throw new UnsupportedError("Non DOM isolate with repeating timer."); |
| 1283 } |
| 1269 } | 1284 } |
| 1270 | 1285 |
| 1271 void cancel() { | 1286 void cancel() { |
| 1272 if (_once) { | 1287 if (hasTimer() && !_globalState.isWorker) { |
| 1273 JS('void', '#.clearTimeout(#)', globalThis, _handle); | 1288 if (_once) { |
| 1289 JS('void', '#.clearTimeout(#)', globalThis, _handle); |
| 1290 } else { |
| 1291 JS('void', '#.clearInterval(#)', globalThis, _handle); |
| 1292 } |
| 1274 } else { | 1293 } else { |
| 1275 JS('void', '#.clearInterval(#)', globalThis, _handle); | 1294 throw new UnsupportedError("Canceling a timer on a non DOM isolate."); |
| 1276 } | 1295 } |
| 1277 } | 1296 } |
| 1278 } | 1297 } |
| 1279 | 1298 |
| 1280 bool hasTimer() => JS('', '#.setTimeout', globalThis) != null; | 1299 bool hasTimer() => JS('', '#.setTimeout', globalThis) != null; |
| OLD | NEW |