debounce method

Future<void> debounce(
  1. Duration duration
)

Delays an execution by a bit such that if a dependency changes multiple time rapidly, the rest of the code is only run once.

Implementation

Future<void> debounce(Duration duration) {
  final completer = Completer<void>();
  final timer = Timer(duration, () {
    if (!completer.isCompleted) completer.complete();
  });
  onDispose(() {
    timer.cancel();
    if (!completer.isCompleted) {
      completer.completeError(StateError('Cancelled'));
    }
  });
  return completer.future;
}