Line data Source code
1 : /// Configuration options for a [Debouncer] instance.
2 : ///
3 : /// Pass a [DebouncerConfig] to customise the delay, debug label,
4 : /// and whether to allow leading / trailing edge execution.
5 : class DebouncerConfig {
6 : /// How long to wait after the last call before executing the action.
7 : final Duration delay;
8 :
9 : /// Optional label used in log output for debugging.
10 : final String? debugLabel;
11 :
12 : /// Whether to execute on the leading edge of the timeout.
13 : final bool leading;
14 :
15 : /// Whether to execute on the trailing edge of the timeout.
16 : final bool trailing;
17 :
18 3 : const DebouncerConfig({
19 : this.delay = const Duration(milliseconds: 300),
20 : this.debugLabel,
21 : this.leading = false,
22 : this.trailing = true,
23 : }) : assert(
24 2 : leading || trailing,
25 : 'At least one of leading or trailing must be true.',
26 : );
27 :
28 1 : DebouncerConfig copyWith({
29 : Duration? delay,
30 : String? debugLabel,
31 : bool? leading,
32 : bool? trailing,
33 : }) {
34 1 : return DebouncerConfig(
35 0 : delay: delay ?? this.delay,
36 1 : debugLabel: debugLabel ?? this.debugLabel,
37 1 : leading: leading ?? this.leading,
38 1 : trailing: trailing ?? this.trailing,
39 : );
40 : }
41 :
42 0 : @override
43 : String toString() =>
44 0 : 'DebouncerConfig(delay: $delay, leading: $leading, trailing: $trailing, label: $debugLabel)';
45 : }
|