1212
1313import 'dart:async' show FutureOr, Timer;
1414
15- import 'package:df_type/df_type.dart' show Sequential;
15+ import 'package:df_safer_dart/df_safer_dart.dart' ;
16+
17+ typedef _VoidCallback = FutureOr <void > Function ();
1618
1719// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
1820
@@ -25,25 +27,35 @@ final class Debouncer {
2527
2628 Timer ? _timer;
2729 bool _hasStarted = false ;
28- final _sequential = Sequential ();
30+ final _sequencer = SafeSequencer ();
2931
3032 /// The function to call once at the very beginning.
31- final FutureOr <void > Function ()? onStart;
33+ final _VoidCallback ? _onStart;
34+ _VoidCallback ? get onStart => _onStart;
3235
3336 /// The function to call after the [delay] has passed.
34- final FutureOr <void > Function ()? onWaited;
37+ final _VoidCallback ? _onWaited;
38+ _VoidCallback ? get onWaited => _onWaited;
3539
3640 /// The function to call immediately.
37- final FutureOr <void > Function ()? onCall;
41+ final _VoidCallback ? _onCall;
42+ _VoidCallback ? get onCall => _onCall;
3843
39- /// The delay before calling the [onWaited ] function.
44+ /// The delay before calling the [_onWaited ] function.
4045 final Duration delay;
4146
4247 //
4348 //
4449 //
4550
46- Debouncer ({required this .delay, this .onStart, this .onWaited, this .onCall});
51+ Debouncer ({
52+ required this .delay,
53+ FutureOr <void > Function ()? onStart,
54+ FutureOr <void > Function ()? onWaited,
55+ FutureOr <void > Function ()? onCall,
56+ }) : _onCall = onCall,
57+ _onWaited = onWaited,
58+ _onStart = onStart;
4759
4860 //
4961 //
@@ -58,31 +70,31 @@ final class Debouncer {
5870 }) {
5971 cancel ();
6072 if (! _hasStarted) {
61- if (this .onStart != null ) {
62- _sequential .add ((_) => this . onStart !() );
73+ if (_onStart != null ) {
74+ _sequencer .add (_onStart );
6375 }
6476 if (onStart != null ) {
65- _sequential .add ((_) => onStart () );
77+ _sequencer .add (onStart);
6678 }
6779 _hasStarted = true ;
6880 }
69- if (this .onCall != null ) {
70- _sequential .add ((_) => this . onCall !() );
81+ if (_onCall != null ) {
82+ _sequencer .add (_onCall );
7183 }
7284 if (onCall != null ) {
73- _sequential .add ((_) => onCall () );
85+ _sequencer .add (onCall);
7486 }
7587 _timer = Timer (delay, () {
76- if (this .onWaited != null ) {
77- _sequential .add ((_) => this . onWaited !() );
88+ if (_onWaited != null ) {
89+ _sequencer .add (_onWaited );
7890 }
7991 if (onWaited != null ) {
80- _sequential .add ((_) => onWaited () );
92+ _sequencer .add (onWaited);
8193 }
8294 _hasStarted = false ;
8395 });
8496
85- return _sequential .last;
97+ return _sequencer .last.value ;
8698 }
8799
88100 //
@@ -92,13 +104,13 @@ final class Debouncer {
92104 /// Finalizes the debouncer and calls the [onWaited] function.
93105 FutureOr <void > finalize ({FutureOr <void > Function ()? onWaited}) {
94106 if (cancel ()) {
95- if (this .onWaited != null ) {
96- _sequential .add ((_) => this . onWaited !() );
107+ if (_onWaited != null ) {
108+ _sequencer .add (_onWaited );
97109 }
98110 if (onWaited != null ) {
99- _sequential .add ((_) => onWaited () );
111+ _sequencer .add (onWaited);
100112 }
101- return _sequential .last;
113+ return _sequencer .last.value ;
102114 }
103115 }
104116
0 commit comments