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

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
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..c4fb98bcd7ff9a9d519f9bc40529adcdffe0a0d0
--- /dev/null
+++ b/lib/isolate/dart2js/timer_provider.dart
@@ -0,0 +1,49 @@
+// 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() native
kasperl 2012/08/09 06:15:33 Could this be using the JS helper rather than bein
Siggi Cherem (dart-lang) 2012/08/09 20:37:14 Done.
+ "return typeof window != 'undefined' ? window : (void 0);";
+
+class _Timer implements Timer {
+ bool _once;
kasperl 2012/08/09 06:15:33 It seems perfectly possible to make both fields fi
Siggi Cherem (dart-lang) 2012/08/09 20:37:14 Done for '_once'. Can't do it for _handle, though
+ 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) {
+ if (repeating) {
kasperl 2012/08/09 06:15:33 Maybe use ?:.
Siggi Cherem (dart-lang) 2012/08/09 20:37:14 Done.
+ return new _Timer.repeating(millis, callback);
+ } else {
+ return new _Timer(millis, callback);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698