throttle method

void throttle(
  1. String id,
  2. Duration duration,
  3. void callback()
)

Throttles a callback function.

  • id: A unique identifier for this operation.
  • duration: The lockout duration after execution.
  • callback: The function to execute.

Ensures the callback runs at most once every duration. If called during the cool down period, the call is ignored.

Implementation

void throttle(String id, Duration duration, void Function() callback) {
  if (_timers.containsKey(id)) return;

  callback();
  _timers[id] = Timer(duration, () {
    _timers.remove(id);
  });
}