throttle static method

bool throttle(
  1. String tag,
  2. Duration duration,
  3. EasyThrottleCallback onExecute, {
  4. EasyThrottleCallback? 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

static bool throttle(
  String tag,
  Duration duration,
  EasyThrottleCallback onExecute, {
  EasyThrottleCallback? onAfter,
}) {
  var throttled = _operations.containsKey(tag);
  if (throttled) {
    return true;
  }

  _operations[tag] = _EasyThrottleOperation(
    onExecute,
    Timer(duration, () {
      _operations[tag]?.timer.cancel();
      _EasyThrottleOperation? removed = _operations.remove(tag);

      removed?.onAfter?.call();
    }),
    onAfter: onAfter,
  );

  onExecute();

  return false;
}