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

Unified Diff: pkg/polymer/lib/job.dart

Issue 24149003: Port of github.com/polymer/polymer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rebase Created 7 years, 3 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 | « pkg/polymer/lib/deserialize.dart ('k') | pkg/polymer/lib/platform.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/polymer/lib/job.dart
diff --git a/pkg/polymer/lib/job.dart b/pkg/polymer/lib/job.dart
new file mode 100644
index 0000000000000000000000000000000000000000..677784a2de0be6613a4f196cb7eb61ad3f688fbd
--- /dev/null
+++ b/pkg/polymer/lib/job.dart
@@ -0,0 +1,53 @@
+// Copyright (c) 2013, 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.
+
+library polymer.job;
+
+import 'dart:async' show Timer;
+
+/**
+ * Invoke [callback] in [wait], unless the job is re-registered,
+ * which resets the timer. For example:
+ *
+ * _myJob = runJob(_myJob, callback, const Duration(milliseconds: 100));
+ *
+ * Returns a job handle which can be used to re-register a job.
+ */
+// Dart note: renamed to runJob to avoid conflict with instance member "job".
+Job runJob(Job job, void callback(), Duration wait) {
+ if (job != null) {
+ job.stop();
+ } else {
+ job = new Job();
+ }
+ job.go(callback, wait);
+ return job;
+}
+
+// TODO(jmesserly): it isn't clear to me what is supposed to be public API here.
+// Or what name we should use. "Job" is awfully generic.
+// (The type itself is not exported in Polymer.)
+class Job {
+ Function _callback;
+ Timer _timer;
+
+ void go(void callback(), Duration wait) {
+ this._callback = callback;
+ _timer = new Timer(wait, complete);
+ }
+
+ void stop() {
+ if (_timer != null) {
+ _timer.cancel();
+ _timer = null;
+ }
+ }
+
+ void complete() {
+ if (_timer != null) {
+ stop();
+ _callback();
+ }
+ }
+}
« no previous file with comments | « pkg/polymer/lib/deserialize.dart ('k') | pkg/polymer/lib/platform.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698