throttle method
- String tag,
- Duration duration,
- VoidCallback onExecute, {
- VoidCallback? onAfter,
Will execute onExecute
immediately and ignore additional attempts to
call throttle with the same tag
happens for the given duration
.
tag
is any arbitrary String, and is used to identify this particular throttle
operation in subsequent calls to throttle() or cancel()
.
duration
is the amount of time subsequent attempts will be ignored.
Returns whether the operation was throttled
Implementation
bool throttle(
String tag,
Duration duration,
VoidCallback onExecute, {
VoidCallback? onAfter,
}) {
var throttled = _throttleOperations.containsKey(tag);
if (throttled) {
return true;
}
_throttleOperations[tag] = _InnerOperation(
timer: Timer(duration, () {
_throttleOperations[tag]?.timer.cancel();
_InnerOperation? removed = _throttleOperations.remove(tag);
removed?.onAfter?.call();
}),
callback: onExecute,
onAfter: onAfter,
);
onExecute();
return false;
}