Showing
1 changed file
with
15 additions
and
1 deletions
1 | import 'dart:async'; | 1 | import 'dart:async'; |
2 | 2 | ||
3 | +/// This "function" class is the implementation of [debouncer()] Worker. | ||
4 | +/// It calls the function passed after specified [delay] parameter. | ||
5 | +/// Example: | ||
6 | +/// ``` | ||
7 | +/// final delayed = Debouncer( delay: Duration( seconds: 1 )) ; | ||
8 | +/// print( 'the next function will be called after 1 sec' ); | ||
9 | +/// delayed( () => print( 'called after 1 sec' )); | ||
10 | +/// ``` | ||
3 | class Debouncer { | 11 | class Debouncer { |
4 | final Duration delay; | 12 | final Duration delay; |
5 | Timer _timer; | 13 | Timer _timer; |
6 | 14 | ||
7 | Debouncer({this.delay}); | 15 | Debouncer({this.delay}); |
8 | 16 | ||
9 | - call(void Function() action) { | 17 | + void call(void Function() action) { |
10 | _timer?.cancel(); | 18 | _timer?.cancel(); |
11 | _timer = Timer(delay, action); | 19 | _timer = Timer(delay, action); |
12 | } | 20 | } |
21 | + | ||
22 | + /// Notifies if the delayed call is active. | ||
23 | + bool get isRunning => _timer?.isActive ?? false; | ||
24 | + | ||
25 | + /// Cancel the current delayed call. | ||
26 | + void cancel() => _timer?.cancel(); | ||
13 | } | 27 | } |
-
Please register or login to post a comment