debounce method
- String tag,
- Duration duration,
- VoidCallback onExecute
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
void debounce(String tag, Duration duration, VoidCallback onExecute) {
if (duration == Duration.zero) {
_debounceOperations[tag]?.timer.cancel();
_debounceOperations.remove(tag);
onExecute();
} else {
_debounceOperations[tag]?.timer.cancel();
_debounceOperations[tag] = _InnerOperation(
timer: Timer(duration, () {
_debounceOperations[tag]?.timer.cancel();
_debounceOperations.remove(tag);
onExecute();
}),
callback: onExecute);
}
}