debounce method

void debounce(
  1. String id,
  2. Duration duration,
  3. FutureOr<void> action()
)

Debounces action, executing it only after duration of inactivity.

Subsequent calls with the same id cancel the pending timer and schedule a new one. Automatically cancels active timers when the store is disposed.

Implementation

void debounce(
  String id,
  Duration duration,
  FutureOr<void> Function() action,
) {
  if (_disposed) return;
  _debounceTimers[id]?.cancel();
  _debounceTimers[id] = Timer(duration, () async {
    _debounceTimers.remove(id);
    if (_disposed) return;
    try {
      await action();
    } catch (exception, stackTrace) {
      FlutterError.reportError(FlutterErrorDetails(
        exception: exception,
        stack: stackTrace,
        library: 'orbit',
        context:
            ErrorDescription('inside debounced action "$id" in $runtimeType'),
      ));
    }
  });
}