debounceStream<T> function
Returns a stream that re-emits values from source, but suppresses any
value that is followed by another within duration. Each value resets the
timer; the value is forwarded only once duration elapses with no newer
value.
If source closes while a value is still pending, that final value is
emitted immediately before the stream closes — otherwise the last keystroke
of a burst would be lost. Errors are forwarded as they arrive (not
debounced), so failures surface promptly.
The returned stream mirrors source's single/broadcast nature is NOT
preserved: it is a single-subscription stream. Listen to it once.
Example:
// Only the last query in each 300ms typing burst reaches the server.
debounceStream(queryStream, const Duration(milliseconds: 300))
.listen(runSearch);
Audited: 2026-06-12 11:26 EDT
Implementation
Stream<T> debounceStream<T>(Stream<T> source, Duration duration) =>
_debounce(source, duration, emitFirstImmediately: false);