Line data Source code
1 : import 'dart:async';
2 : import 'package:flutter/foundation.dart';
3 : import '../debouncer_config.dart';
4 : import 'debounce_strategy.dart';
5 :
6 : /// Fires the action once, after [DebouncerConfig.delay] has passed
7 : /// since the **last** call.
8 : ///
9 : /// This is the default debounce behaviour — ideal for search fields,
10 : /// form validation, and resize handlers where you only care about
11 : /// the final value after the user stops typing.
12 : ///
13 : /// Timeline:
14 : /// ```
15 : /// calls: --A--B--C-----------
16 : /// fires: -------------------C
17 : /// ```
18 : class TrailingEdgeStrategy extends DebouncerStrategy {
19 6 : const TrailingEdgeStrategy();
20 :
21 2 : @override
22 : void execute(
23 : VoidCallback action,
24 : DebouncerConfig config,
25 : Timer? currentTimer,
26 : void Function(Timer?) updateTimer,
27 : ) {
28 2 : currentTimer?.cancel();
29 6 : updateTimer(Timer(config.delay, action));
30 : }
31 : }
|