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

Unified Diff: lib/src/restartable_timer.dart

Issue 1417373004: Add a ResetTimer class. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Code review changes Created 5 years, 2 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/async.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/restartable_timer.dart
diff --git a/lib/src/restartable_timer.dart b/lib/src/restartable_timer.dart
new file mode 100644
index 0000000000000000000000000000000000000000..05196d2873675ef9ad222be92aadf5cc74ee9ce6
--- /dev/null
+++ b/lib/src/restartable_timer.dart
@@ -0,0 +1,48 @@
+// Copyright (c) 2015, 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 async.restartable_timer;
+
+import 'dart:async';
+
+/// A non-periodic timer that can be restarted any number of times.
+///
+/// Once restarted (via [reset]), the timer counts down from its original
+/// duration again.
+class RestartableTimer implements Timer {
+ /// The duration of the timer.
+ final Duration _duration;
+
+ /// The callback to call when the timer fires.
+ final ZoneCallback _callback;
+
+ /// The timer for the current or most recent countdown.
+ ///
+ /// This timer is canceled and overwritten every time this [RestartableTimer]
+ /// is reset.
+ Timer _timer;
+
+ /// Creates a new timer.
+ ///
+ /// The [callback] function is invoked after the given [duration]. Unlike a
+ /// normal non-periodic [Timer], [callback] may be called more than once.
+ RestartableTimer(this._duration, this._callback) {
+ _timer = new Timer(_duration, _callback);
+ }
+
+ bool get isActive => _timer.isActive;
+
+ /// Restarts the timer so that it counts down from its original duration
+ /// again.
+ ///
+ /// This restarts the timer even if it has already fired or has been canceled.
+ void reset() {
+ _timer.cancel();
+ _timer = new Timer(_duration, _callback);
+ }
+
+ void cancel() {
+ _timer.cancel();
+ }
+}
« no previous file with comments | « lib/async.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698