philiprehberger_debounce 0.1.1
philiprehberger_debounce: ^0.1.1 copied to clipboard
Debounce and throttle utilities with Stream transformers and cancellation
philiprehberger_debounce #
Debounce and throttle utilities with Stream transformers and cancellation
Requirements #
- Dart >= 3.6
Installation #
Add to your pubspec.yaml:
dependencies:
philiprehberger_debounce: ^0.1.0
Then run:
dart pub get
Usage #
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'));
Throttler #
final throttler = Throttler(interval: Duration(seconds: 1));
throttler.call(() => print('Scroll handler'));
throttler.call(() => print('Skipped - too soon'));
Stream Transformers #
// Debounce a stream
textFieldStream
.debounce(Duration(milliseconds: 300))
.listen((query) => search(query));
// Throttle a stream
scrollStream
.throttle(Duration(milliseconds: 100))
.listen((offset) => updateUI(offset));
API #
| 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 |
Development #
dart pub get
dart analyze --fatal-infos
dart test
Support #
If you find this project useful: