Debouncer constructor

Debouncer({
  1. required Duration delay,
  2. Duration? maxWait,
  3. bool immediate = false,
  4. void onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
  5. String? debugLabel,
  6. int maxHistorySize = 0,
  7. LoggerFunction? logger,
  8. TimerFactory? timerFactory,
})

Creates a new Debouncer instance.

  • delay specifies how long to wait after the last call before executing the action. It must be greater than zero.
  • maxWait sets an upper limit on how long to wait before forcing execution. If provided, it must be greater than delay.
  • When immediate is true, the first call in a burst executes immediately, and later calls during that burst are debounced with no trailing execution until a new burst begins.
  • onError is an optional callback to handle errors thrown during the action.
  • debugLabel helps tag log messages.
  • maxHistorySize defines how many past execution records to store (0 disables history).
  • logger can be provided to inject a custom logging function.
  • timerFactory allows for custom timer creation. Defaults to Timer.

Implementation

Debouncer({
  required Duration delay,
  Duration? maxWait,
  bool immediate = false,
  void Function(Object error, StackTrace stackTrace)? onError,
  String? debugLabel,
  int maxHistorySize = 0,
  LoggerFunction? logger,
  TimerFactory? timerFactory,
}) : _delay = delay,
     _maxWait = maxWait,
     _immediate = immediate,
     _onError = onError,
     _debugLabel = debugLabel,
     _maxHistorySize = maxHistorySize,
     _logger = logger,
     _timerFactory = timerFactory ?? Timer.new {
  if (delay <= Duration.zero) {
    throw ArgumentError('Delay must be greater than zero.');
  }
  if (maxWait != null && maxWait <= delay) {
    throw ArgumentError('maxWait must be greater than delay.');
  }
  if (maxHistorySize < 0) {
    throw ArgumentError('maxHistorySize must be >= 0.');
  }
}