call method
Dynamically call this Debounce with the specified arguments.
Acts the same as calling _func
with positional arguments
corresponding to the elements of args
and
named arguments corresponding to the elements of namedArgs
.
This includes giving the same errors if _func
isn't callable or
if it expects different parameters.
Example:
List<Movie> fetchMovies(
String movieName, {
bool adult = false,
}) async {
final data = api.getData(query);
doSomethingWithTheData(data);
}
final debouncedFetchMovies = Debounce(
fetchMovies,
const Duration(milliseconds: 350),
);
debouncedFetchMovies(['tenet'], {#adult: true});
gives exactly the same result as
fetchMovies('tenet', adult: true).
Implementation
Object? call([List<Object?>? args, Map<Symbol, Object?>? namedArgs]) {
final time = DateTime.now().millisecondsSinceEpoch;
final isInvoking = _shouldInvoke(time);
_lastArgs = args;
_lastNamedArgs = namedArgs;
_lastCallTime = time;
if (isInvoking) {
if (_timer == null) {
return _leadingEdge(_lastCallTime!);
}
if (_maxing) {
// Handle invocations in a tight loop.
_timer = _startTimer(_timerExpired, _wait);
return _invokeFunc(_lastCallTime!);
}
}
_timer ??= _startTimer(_timerExpired, _wait);
return _result;
}