debounce static method
Will delay the execution of onExecute
with the given duration
.
If another call to debounce() with the same tag
happens within
this duration, the first call will be cancelled and the debouncer
will start waiting for another duration
before executing onExecute
.
tag
is any arbitrary String, and is used to identify this particular
debounce operation in subsequent calls to debounce() or cancel().
If duration
is Duration.zero
, onExecute
will be executed
immediately, i.e. synchronously.
Implementation
static Future<void> debounce(
String tag,
Duration duration,
FutureFutureEasyDebounceCallback onExecute,
) {
if (duration == Duration.zero) {
_operations[tag]?.$1.timer.cancel();
onExecute().then((_) {
_operations[tag]?.$2.tryComplete();
_operations.remove(tag);
});
} else {
_operations[tag]?.$1.timer.cancel();
_operations[tag] = (
_FutureEasyDebounceOperation(
onExecute,
Timer(duration, () {
_operations[tag]?.$1.timer.cancel();
_operations.remove(tag);
onExecute().then((_) {
_operations[tag]?.$2.tryComplete();
_operations.remove(tag);
});
}),
),
Completer(),
);
}
return _operations[tag]?.$2.future ?? Future.value();
}