Skip to content

Commit a3db783

Browse files
committed
Support variable duration in RestartableTimer
Closes #900 Rename the existing field to `_defaultDuration` for clarity and update doc comment references to disambiguate against the argument to `reset`.
1 parent 4be18fb commit a3db783

4 files changed

Lines changed: 42 additions & 13 deletions

File tree

pkgs/async/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
## 2.14.0-wip
2+
3+
- Add a `Duration` argument to `RestartableTimer.reset` to allow restarting
4+
with variable intervals.
5+
16
## 2.13.1
27

3-
- Fix `StreamGroup.broadcast().close()` to properly complete when all streams in the group close without being explicitly removed.
8+
- Fix `StreamGroup.broadcast().close()` to properly complete when all streams in
9+
the group close without being explicitly removed.
410
- Run `dart format` with the new style.
511

612
## 2.13.0

pkgs/async/lib/src/restartable_timer.dart

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import 'dart:async';
77
/// A non-periodic timer that can be restarted any number of times.
88
///
99
/// Once restarted (via [reset]), the timer counts down from its original
10-
/// duration again.
10+
/// or a passed duration.
1111
class RestartableTimer implements Timer {
12-
/// The duration of the timer.
13-
final Duration _duration;
12+
/// The default duration of the timer.
13+
final Duration _defaultDuration;
1414

1515
/// The callback to call when the timer fires.
1616
final ZoneCallback _callback;
@@ -23,21 +23,24 @@ class RestartableTimer implements Timer {
2323

2424
/// Creates a new timer.
2525
///
26-
/// The [_callback] function is invoked after the given [_duration]. Unlike a
27-
/// normal non-periodic [Timer], [_callback] may be called more than once.
28-
RestartableTimer(this._duration, this._callback)
29-
: _timer = Timer(_duration, _callback);
26+
/// The [_callback] function is invoked after the given [_defaultDuration] by
27+
/// default. Unlike a normal non-periodic [Timer], [_callback] may be called
28+
/// more than once.
29+
RestartableTimer(this._defaultDuration, this._callback)
30+
: _timer = Timer(_defaultDuration, _callback);
3031

3132
@override
3233
bool get isActive => _timer.isActive;
3334

34-
/// Restarts the timer so that it counts down from its original duration
35-
/// again.
35+
/// Restarts the timer so that it counts down again.
3636
///
3737
/// This restarts the timer even if it has already fired or has been canceled.
38-
void reset() {
38+
/// If [duration] is passed it is used for this cycle of the timer but
39+
/// does not change the default duration for this timer if future calls to
40+
/// `reset` omit the [duration] argument.
41+
void reset([Duration? duration]) {
3942
_timer.cancel();
40-
_timer = Timer(_duration, _callback);
43+
_timer = Timer(duration ?? _defaultDuration, _callback);
4144
}
4245

4346
@override

pkgs/async/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: async
2-
version: 2.13.1
2+
version: 2.14.0-wip
33
description: Utility functions and classes related to the 'dart:async' library.
44
repository: https://github.com/dart-lang/core/tree/main/pkgs/async
55
issue_tracker: https://github.com/dart-lang/core/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aasync

pkgs/async/test/restartable_timer_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,24 @@ void main() {
106106
async.elapse(const Duration(seconds: 10));
107107
});
108108
});
109+
110+
test('can be reset with a variable duration', () {
111+
FakeAsync().run((async) {
112+
var fired = 0;
113+
var timer = RestartableTimer(const Duration(seconds: 5), () {
114+
fired++;
115+
});
116+
117+
timer.reset(const Duration(seconds: 10));
118+
async.elapse(const Duration(seconds: 6));
119+
expect(fired, equals(0));
120+
121+
async.elapse(const Duration(seconds: 5));
122+
expect(fired, equals(1));
123+
124+
timer.reset();
125+
async.elapse(const Duration(seconds: 5));
126+
expect(fired, equals(2));
127+
});
128+
});
109129
}

0 commit comments

Comments
 (0)