debounce<S, T> function

Debounceable<S, T> debounce<S, T>(
  1. Debounceable<S?, T> function, {
  2. int duration = 300,
})

Returns a new function that is a debounced version of the given function.

This means that the original function will be called only after no calls have been made for the given Duration.

Implementation

Debounceable<S, T> debounce<S, T>(Debounceable<S?, T> function,
    {int duration = 300}) {
  _DebounceTimer? debounceTimer;

  return (T parameter) async {
    if (debounceTimer != null && !debounceTimer!.isCompleted) {
      debounceTimer!.cancel();
    }
    debounceTimer = _DebounceTimer(Duration(milliseconds: duration));
    try {
      await debounceTimer!.future;
    } catch (error) {
      if (error is _CancelException) {
        return null;
      }
      rethrow;
    }
    return function(parameter);
  };
}