debounce static method

void debounce(
  1. String tag,
  2. Duration duration,
  3. DebouncerCallback onExecute
)

Will delay the execution of onExecute with the given duration. If another call to debounce() with the same tag happens within this duration, the first call will be cancelled and the debouncer will start waiting for another duration before executing onExecute.

tag is any arbitrary String, and is used to identify this particular debounce operation in subsequent calls to debounce() or cancel().

If duration is Duration.zero, onExecute will be executed immediately, i.e. synchronously.

Implementation

static void debounce(
    String tag, Duration duration, DebouncerCallback onExecute) {
  if (duration == Duration.zero) {
    _operations[tag]?.timer.cancel();
    _operations.remove(tag);
    onExecute();
  } else {
    _operations[tag]?.timer.cancel();

    _operations[tag] = _DebouncerOperation(
      onExecute,
      Timer(
        duration,
        () {
          _operations[tag]?.timer.cancel();
          _operations.remove(tag);

          onExecute();
        },
      ),
    );
  }
}