philiprehberger_debounce 0.4.0 copy "philiprehberger_debounce: ^0.4.0" to clipboard
philiprehberger_debounce: ^0.4.0 copied to clipboard

Debounce and throttle utilities with Stream transformers and cancellation

philiprehberger_debounce #

Tests pub package Last updated

Debounce and throttle utilities with Stream transformers and cancellation

Requirements #

  • Dart >= 3.6

Installation #

Add to your pubspec.yaml:

dependencies:
  philiprehberger_debounce: ^0.4.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'));

Immediate Mode #

import 'package:philiprehberger_debounce/philiprehberger_debounce.dart';

final debouncer = Debouncer(delay: Duration(milliseconds: 300), immediate: true);
debouncer.call(() => print('Fires immediately, then ignores for 300ms'));

Flush Pending Action #

final debouncer = Debouncer(delay: Duration(milliseconds: 300));
debouncer.call(() => saveData());

// On dispose, ensure the pending save executes immediately
debouncer.flush();

Max Wait #

final debouncer = Debouncer(
  delay: Duration(milliseconds: 300),
  maxWait: Duration(milliseconds: 1000),
);

// Even with continuous calls, the action fires at least every 1000ms
for (var i = 0; i < 100; i++) {
  debouncer.call(() => print('Autosave'));
  await Future.delayed(Duration(milliseconds: 50));
}

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:, {immediate, maxWait}) Create a debouncer with delay, optional immediate mode, and optional max wait
Debouncer.call(action) Schedule action after delay, resetting on repeated calls
Debouncer.flush() Immediately execute any pending action and cancel the timer
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:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License #

MIT

1
likes
160
points
70
downloads

Documentation

API reference

Publisher

verified publisherphiliprehberger.com

Weekly Downloads

Debounce and throttle utilities with Stream transformers and cancellation

Homepage
Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on philiprehberger_debounce