throttleFuture<T> method

void throttleFuture<T>(
  1. DebounceCallback<T> callback, {
  2. DebounceResultConsumer<T>? consumer,
  3. DebounceErrorConsumer? errorConsumer,
})

Throttle a callback. Instead of waiting for the duration to call the callback, it will wait if the previous callback is still running.

Implementation

void throttleFuture<T>(
  DebounceCallback<T> callback, {
  DebounceResultConsumer<T>? consumer,
  DebounceErrorConsumer? errorConsumer,
}) {
  assert(!_debugDisposed, 'DebounceController is disposed');
  if (_session == null) {
    _session = _DebounceFutureSession();
    notifyListeners();
    var result = callback();
    if (result is Future<T>) {
      result.then((value) {
        consumer?.call(value);
      }).catchError((error, stackTrace) {
        errorConsumer?.call(error, stackTrace);
      }).whenComplete(() {
        _session = null;
        notifyListeners();
      });
      return;
    } else {
      consumer?.call(result);
      _session = null;
      notifyListeners();
    }
  }
}