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

Unified Diff: lib/isolate/dart2js/timer_provider.dart

Issue 10830230: Add Timer implementation for dart2js. Also moves tests to appropriate location. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/isolate/dart2js/isolateimpl.dart ('k') | lib/isolate/isolate_dart2js.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/isolate/dart2js/timer_provider.dart
diff --git a/lib/isolate/dart2js/timer_provider.dart b/lib/isolate/dart2js/timer_provider.dart
new file mode 100644
index 0000000000000000000000000000000000000000..3d271f35d681365370800485b0980d289ec8ac5f
--- /dev/null
+++ b/lib/isolate/dart2js/timer_provider.dart
@@ -0,0 +1,45 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+
+// We don't want to import the DOM library just because of window.setTimeout,
+// so we reconstruct the Window class here. The only conflict that could happen
+// with the other DOMWindow class would be because of subclasses.
+// Currently, none of the two Dart classes have subclasses.
+typedef void _TimeoutHandler();
+
+class _Window native "@*DOMWindow" {
+ int setTimeout(_TimeoutHandler handler, int timeout) native;
+ int setInterval(_TimeoutHandler handler, int timeout) native;
+}
+
+_Window get _window() =>
+ JS('bool', 'typeof window != "undefined"') ? JS('_Window', 'window') : null;
+
+class _Timer implements Timer {
+ final bool _once;
+ int _handle;
+
+ _Timer(int milliSeconds, void callback(Timer timer))
+ : _once = true {
+ _handle = _window.setTimeout(() => callback(this), milliSeconds);
+ }
+
+ _Timer.repeating(int milliSeconds, void callback(Timer timer))
+ : _once = false {
+ _handle = _window.setInterval(() => callback(this), milliSeconds);
+ }
+
+ void cancel() {
+ if (_once) {
+ _window.clearTimeout(_handle);
+ } else {
+ _window.clearInterval(_handle);
+ }
+ }
+}
+
+Timer _timerFactory(int millis, void callback(Timer timer), bool repeating) =>
+ repeating ? new _Timer.repeating(millis, callback)
+ : new _Timer(millis, callback);
« no previous file with comments | « lib/isolate/dart2js/isolateimpl.dart ('k') | lib/isolate/isolate_dart2js.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698