A package for creating debounce in dart will possibilities of multi instances debounce. With no singleton pattern
, this package provides a convenient way to handle debouncing scenarios for user interactions, such as button presses or text input changes, in order to enhance the user experience and avoid unintended actions, unnecessary logic execution or frequent updates.
First, instanciate a Debouncer class. Notice: it is not a singletton so you have the option to create multiple instances of deboncer each one of them with one logic.
final Debouncer debouncer = Debouncer();
Then, in any place, use it.
TextFormField(
...
onChanged: (final String text) {
decouncer.resetDebounce(() {
// Do something with the text
});
},
),
debouncer.dispose();
is not mandatory
. But if you don't dispose it the function will be executed even if you are not more in the context where it has been placed. So if, for example, the function inside the debouncer uses a dependencies that dosen't exist any more you will get an error. This will basically cancel the debounce timer and execution.
You can add how many functions you want. They will run in the order they where added.
debouncer.addOnInitFunction(() {
print('start 1');
});
debouncer.addOnInitFunction(() {
print('start 2');
});
You can add how many functions you want. They will run in the order they where added.
debouncer.addOnEndFunction(() {
print('end 1');
});
debouncer.addOnEndFunction(() {
print('end 2');
});
Whant to run a function but ensure it won't run at the same time an asyncronous execute function is running on the debouncer? No problem, Use garanteedExecutionAfterDebounceFinished
for that.
final debouncer = Debouncer(timerDuration: Duration(seconds: 1));
decouncer.resetDebounce(() {
await Future.delayed(Duration(seconds: 1));
print('Done!');
});
// Will not execute until debounce function is running. Will wait it finish first
debounter.garanteedExecutionAfterDebounceFinished(() {
print('Only runned after 2 seconds');
});
print(isDebouncerRunning);
final debouncer = Debouncer(timerDuration: Duration(seconds: 1));
final bool isDebouncerRunning = debounter.isTimerActive();
print(isDebouncerRunning);
// The debouncer time is 1 sec
final debouncer = Debouncer(timerDuration: Duration(seconds: 1));
// Now the timer is 3 secs
debounder.updateTimer(Duration(seconds: 3));
Extension methods for num, to make specifying durations easier. For example: 2.seconds, 0.1.minutes, or 300.ms.
Made with ❤ by Igor Miranda
If you like the package, give a 👍