debounce method

  1. @visibleForTesting
  2. @protected
void debounce({
  1. required void action(),
  2. required String tag,
  3. Duration duration = const Duration(milliseconds: 300),
  4. bool immediate = true,
})
inherited

Executes the action so that it will only be executed when there is no further repeated actions with same tag in a given frame of duration.

If immediate is false, then then first action will also be debounced.

Implementation

@visibleForTesting
@protected
void debounce({
  required void Function() action,
  required String tag,
  Duration duration = const Duration(milliseconds: 300),
  bool immediate = true,
}) {
  final timer = _debounceTimers[tag];

  final timerPending = timer?.isActive ?? false;
  final canExecute = immediate && !timerPending;

  timer?.cancel();
  _debounceTimers[tag] = Timer(
    duration,
    () {
      _debounceTimers.remove(tag);
      if (!immediate) action();
    },
  );

  if (canExecute) action();
}