debounce method

Stream<T> debounce(
  1. Duration duration, {
  2. bool leading = false,
  3. bool trailing = true,
})

Suppresses events with less inter-event spacing than duration.

Events which are emitted with less than duration elapsed between them are considered to be part of the same "series". If leading is true, the first event of this series is emitted immediately. If trailing is true the last event of this series is emitted with a delay of at least duration. By default only trailing events are emitted, both arguments must be specified with leading: true, trailing: false to emit only leading events.

If this stream is a broadcast stream, the result will be as well. Errors are forwarded immediately.

If there is a trailing event waiting during the debounce period when the source stream closes the returned stream will wait to emit it following the debounce period before closing. If there is no pending debounced event when this stream closes the returned stream will close immediately.

For example:

source.debounce(Duration(seconds: 1));

source: 1-2-3---4---5-6-|
result: ------3---4-----6|

source.debounce(Duration(seconds: 1), leading: true, trailing: false);

source: 1-2-3---4---5-6-|
result: 1-------4---5---|

source.debounce(Duration(seconds: 1), leading: true);

source: 1-2-3---4---5-6-|
result: 1-----3-4---5---6|

To collect values emitted during the debounce period see debounceBuffer.

Implementation

Stream<T> debounce(Duration duration,
        {bool leading = false, bool trailing = true}) =>
    _debounceAggregate(duration, _dropPrevious,
        leading: leading, trailing: trailing);