Chromium Code Reviews| Index: lib/src/reset_timer.dart |
| diff --git a/lib/src/reset_timer.dart b/lib/src/reset_timer.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0a7f026eb2384e25d9909e75fcfdd3f08093723f |
| --- /dev/null |
| +++ b/lib/src/reset_timer.dart |
| @@ -0,0 +1,43 @@ |
| +// 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.reset_timer; |
| + |
| +import 'dart:async'; |
| + |
| +/// A non-periodic timer that can be reset. |
|
Lasse Reichstein Nielsen
2015/10/27 09:45:14
"reset" isn't a very descriptive word, it doesn't
nweiz
2015/10/27 21:08:12
I chose "reset" because it's already used by a num
Lasse Reichstein Nielsen
2015/10/28 09:51:02
I have no problem with "reset" as the name of the
nweiz
2015/10/28 21:10:43
Done.
|
| +class ResetTimer implements Timer { |
| + /// The duration of the timer. |
| + final Duration _duration; |
| + |
| + /// The callback to call when the timer fires. |
|
Lasse Reichstein Nielsen
2015/10/27 09:45:14
Would it be useful to make the callback settable?
nweiz
2015/10/27 21:08:12
I don't think so; if that's what you want, just ho
|
| + final ZoneCallback _callback; |
| + |
| + /// The inner timer. |
|
Lasse Reichstein Nielsen
2015/10/27 09:45:14
Being "inner" doesn't say anything about the role
nweiz
2015/10/27 21:08:12
Done.
|
| + /// |
| + /// This is canceled and re-created every time this is reset. |
|
Lasse Reichstein Nielsen
2015/10/27 09:45:14
Two uses of "this" referring to different things.
nweiz
2015/10/27 21:08:12
Done.
|
| + Timer _timer; |
| + |
| + bool get isActive => _timer.isActive; |
|
Lasse Reichstein Nielsen
2015/10/27 09:45:14
Move getter below constructor.
nweiz
2015/10/27 21:08:12
Done.
|
| + |
| + /// 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. |
| + ResetTimer(this._duration, this._callback) { |
| + _timer = new Timer(_duration, _callback); |
| + } |
| + |
| + /// Resets the timer so that it starts counting from its original duration again. |
|
Lasse Reichstein Nielsen
2015/10/27 09:45:14
Resets -> restarts.
The "reset" name is fine for t
Lasse Reichstein Nielsen
2015/10/27 09:45:14
long line.
so that it starts ... -> and starts co
nweiz
2015/10/27 21:08:12
Done.
nweiz
2015/10/27 21:08:12
Done.
|
| + /// |
| + /// This resets the timer even if it's already fired or has been canceled. |
|
Lasse Reichstein Nielsen
2015/10/27 09:45:14
resets -> restarts
it's -> it has
nweiz
2015/10/27 21:08:12
Done.
|
| + void reset() { |
| + _timer.cancel(); |
| + _timer = new Timer(_duration, _callback); |
| + } |
| + |
| + void cancel() { |
| + _timer.cancel(); |
| + } |
| +} |