safe_debouncer 0.1.1
safe_debouncer: ^0.1.1 copied to clipboard
A safe, async-aware debouncer for Dart & Flutter that prevents overlapping executions and timer leaks.
safe_debouncer #
A safe, async-aware debouncer for Dart & Flutter that prevents overlapping executions, timer leaks, and repeated boilerplate code.
safe_debouncer is a logic-only utility designed to handle one of the most common pain points in app development: debouncing actions correctly.
β¨ Why safe_debouncer? #
Most debounce implementations:
- leak
Timers - break on rebuilds
- donβt handle async callbacks
- allow overlapping executions
- get re-written differently in every project
safe_debouncer solves this once and for all.
π Features #
- β Supports sync & async callbacks
- β Prevents overlapping executions
- β Safe cancellation and disposal
- β Rebuild-safe
- β
Key-based debouncing for multiple streams (
SafeDebouncerMap) - β
Individual debouncer instances (
SafeDebouncer) - β Pure Dart (no Flutter dependency)
- β Tiny, focused API
π¦ Installation #
Add to your pubspec.yaml:
dependencies:
safe_debouncer: ^0.0.1
π Usage #
Basic Usage #
import 'package:safe_debouncer/safe_debouncer.dart';
void main() {
final debouncer = SafeDebouncer(Duration(milliseconds: 500));
debouncer.call(() {
print('Debounced action executed!');
});
}
Async Callbacks #
import 'package:safe_debouncer/safe_debouncer.dart';
void main() async {
final debouncer = SafeDebouncer(Duration(milliseconds: 500));
await debouncer.callAsync(() async {
print('Debounced async action executed!');
});
}
Key-based Debouncing #
import 'package:safe_debouncer/safe_debouncer.dart';
void main() {
final debouncerMap = SafeDebouncerMap(Duration(milliseconds: 500));
debouncerMap.call('key1', () {
print('Debounced action executed for key1!');
});
debouncerMap.call('key2', () {
print('Debounced action executed for key2!');
});
}
π API Reference #
SafeDebouncer #
SafeDebouncer(Duration duration): Creates a newSafeDebouncerinstance with the specifiedduration.call(VoidCallback callback): Calls thecallbackafter the specifiedduration.callAsync(Future<void> Function() callback): Calls thecallbackafter the specifieddurationand returns aFuturethat completes when thecallbackis executed.
SafeDebouncerMap #
SafeDebouncerMap(Duration duration): Creates a newSafeDebouncerMapinstance with the specifiedduration.call(String key, VoidCallback callback): Calls thecallbackafter the specifieddurationfor the specifiedkey.callAsync(String key, Future<void> Function() callback): Calls thecallbackafter the specifieddurationfor the specifiedkeyand returns aFuturethat completes when thecallbackis executed.