debounce<T> method
void
debounce<T>(
- DebounceCallback<
T> callback, { - Duration duration = const Duration(milliseconds: 300),
- DebounceResultConsumer<
T> ? consumer, - DebounceErrorConsumer? errorConsumer,
Debounce a callback.
callback: The callback to be debounced.duration: The duration to wait before calling the callback.consumer: The consumer to be called when the callback is called.errorConsumer: The consumer to be called when the callback throws an error.
Implementation
void debounce<T>(
DebounceCallback<T> callback, {
Duration duration = const Duration(milliseconds: 300),
DebounceResultConsumer<T>? consumer,
DebounceErrorConsumer? errorConsumer,
}) {
assert(!_debugDisposed, 'DebounceController is disposed');
_session?.cancel();
_session = _DebounceTimerSession(Timer(
duration,
() {
try {
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);
}
} catch (error, stackTrace) {
errorConsumer?.call(error, stackTrace);
}
_session = null;
notifyListeners();
},
));
notifyListeners();
}