Debounce and throttle utilities with Stream transformers and cancellation
dart pub add philiprehberger_debounceDebounce and throttle utilities with Stream transformers and cancellation
Add to your pubspec.yaml:
dependencies:
philiprehberger_debounce: ^0.1.1
Then run:
dart pub get
import 'package:philiprehberger_debounce/debounce.dart';
final debouncer = Debouncer(delay: Duration(milliseconds: 300));
debouncer.call(() => print('Search executed'));
debouncer.call(() => print('Only this one runs'));
final throttler = Throttler(interval: Duration(seconds: 1));
throttler.call(() => print('Scroll handler'));
throttler.call(() => print('Skipped - too soon'));
// Debounce a stream
textFieldStream
.debounce(Duration(milliseconds: 300))
.listen((query) => search(query));
// Throttle a stream
scrollStream
.throttle(Duration(milliseconds: 100))
.listen((offset) => updateUI(offset));
| Method | Description |
|---|---|
Debouncer(delay:) | Create a debouncer with the given delay |
Debouncer.call(action) | Schedule action after delay, resetting on repeated calls |
Debouncer.cancel() | Cancel any pending debounced call |
Debouncer.isActive | Whether a debounced call is pending |
Throttler(interval:, leading:, trailing:) | Create a throttler with interval and edge options |
Throttler.call(action) | Execute action respecting the throttle interval |
Throttler.cancel() | Cancel any pending throttled call |
Throttler.isActive | Whether the throttler is in its cooldown period |
Stream.debounce(delay) | Emit only the last value after a pause of delay |
Stream.throttle(interval) | Emit at most one value per interval |
dart pub get
dart analyze --fatal-infos
dart test
If you find this project useful: