Coder Social home page Coder Social logo

dart_debouncer's Introduction

<style> .heading-1{ font-size: 360%!important; } </style>

Dart debouncer

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.


How to use

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
    });
  },
),

Don't forget to dispose the debounce timer after using it

debouncer.dispose();

⚠️ Note: Calling 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.

Other helpfull functions

Add a function that will be executed before debounce execute function

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');
});

Add a function that will be executed after debounce execute function

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');
});

Garantee an function will not bee runned in parallel to the debouncer function

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);

Know if debounce function is active/running

final debouncer = Debouncer(timerDuration: Duration(seconds: 1));
final bool isDebouncerRunning = debounter.isTimerActive();
print(isDebouncerRunning);

Change debounce duration

// The debouncer time is 1 sec
final debouncer = Debouncer(timerDuration: Duration(seconds: 1));

// Now the timer is 3 secs
debounder.updateTimer(Duration(seconds: 3));

Duration extensions

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 👍

dart_debouncer's People

Contributors

igormidev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.