throttle static method

bool throttle({
  1. required String tag,
  2. required Duration duration,
  3. required FastThrottleVoidCallback onExecute,
  4. FastThrottleVoidCallback? onAfter,
})

Implementation

static bool throttle({
  required String tag,
  required Duration duration,
  required FastThrottleVoidCallback onExecute,
  FastThrottleVoidCallback? onAfter,
}) {
  bool check = _operations.containsKey(tag);
  if (check) {
    ///节流中
    return true;
  }
  _operations[tag] = _FastThrottleOperation(
    onExecute: onExecute,
    onAfter: onAfter,
    timer: Timer(
      duration,
      () {
        _operations[tag]?.timer.cancel();
        _FastThrottleOperation? removed = _operations.remove(tag);
        removed?.onAfter?.call();
      },
    ),
  );
  onExecute();
  return false;
}