timeUntilAvailable method

Duration timeUntilAvailable()

How long until the next event would be admitted, or Duration.zero if one is admissible now. When at the limit, that is when the oldest in-window event ages out (oldest + window). Audited: 2026-06-12 11:26 EDT

Implementation

Duration timeUntilAvailable() {
  final DateTime now = _now();
  _prune(now);
  final DateTime? oldest = _events.firstOrNull;
  // Below the limit (or empty) → admissible now. Otherwise the oldest event
  // ages out at `oldest + window`, freeing the next slot.
  if (oldest == null || _events.length < limit) {
    return Duration.zero;
  }
  final Duration wait = oldest.add(window).difference(now);
  return wait.isNegative ? Duration.zero : wait;
}