easy_debounce 1.0.1

easy_debounce #

An extremely easy-to-use method call debouncer package for Dart/Flutter.

Debouncing is needed when there is a possibility of multiple calls to a method being made within a short duration of each other, and it's desireable that only the last of those calls actually invoke the target method.

So basically each call starts a timer, and if another call happens before the timer executes, the timer is reset and starts waiting for the desired duration again. When the timer finally does time out, the target method is invoked.

Installation #

Add easy_debounce as a dependency in pubspec.yaml:

  easy_debounce: ^1.0.0 <-- Replace with desired minimum version

Usage #

Debouncing #

Use the debouncer by calling debounce:

EasyDebounce.debounce(
    'my-debouncer',                 // <-- An ID for this particular debouncer
    Duration(milliseconds: 500),    // <-- The debounce duration
    () => myMethod()                // <-- The target method
);

The above call will invoke myMethod() after 500 ms, unless another call to debounce() with the same tag is made within 500 ms. A tag identifies this particular debouncer, which means you can have multiple different debouncers running concurrently and independent of each other.

Cancelling a debouncer #

A debouncer which hasn't yet executed its target function can be called by calling cancel() with the debouncers tag:

EasyDebounce.cancel('my-debouncer');


Counting active debouncers #

You can get the number of active debouncers (debouncers which haven't yet executed their target methods) by calling count():

print('Active debouncers: ${EasyDebounce.count()}'); 

[1.0.1] September 8, 2019

Calling debounce() with a zero-duration will now invoke the target method immediately, i.e. synchronously.

[1.0.0] September 8, 2019

Updated description and link to GitHub repo. Flutter-formatted source code.

[0.0.1] September 8, 2019

Initial release.

example/main.dart

import 'package:easy_debounce/easy_debounce.dart';




void myMethod(String message){
    print(message);
}




void main() async {

    // Example 1
    // This will result in a single call to myMethod() after ~200 ms.
    print('\nExample 1');
    for(int i=0; i<5; i++){
        EasyDebounce.debounce('debouncer1', Duration(milliseconds: 200), () => myMethod('Executing debouncer1! (i: $i)'));
    }

    // Make sure the above example finishes before continuting
    await Future.delayed(Duration(milliseconds: 400));


    // Example 2
    // This will result in five calls to myMethod() since we are waiting 300 ms before running the next iteration
    // of the for-loop.
    print('\nExample 2');
    for(int i=0; i<5; i++){
        EasyDebounce.debounce('debouncer2', Duration(milliseconds: 200), () => myMethod('Executing debouncer2! (i: $i)'));
        await Future.delayed(Duration(milliseconds: 300)); // Wait 300 ms
    }


    // Make sure the above example finishes before continuting
    await Future.delayed(Duration(milliseconds: 400));


    // Example 3
    // This will result in two calls to myMethod() after ~200 ms, each with a different [message].
    // Since we are using two different tags ("debouncer3" and "debouncer4"), the two debouncers
    // operate independently of each other.
    print('\nExample 3');
    for(int i=0; i<5; i++){
        EasyDebounce.debounce('debouncer3', Duration(milliseconds: 200), () => myMethod('Executing debouncer3! (i: $i)'));
        EasyDebounce.debounce('debouncer4', Duration(milliseconds: 200), () => myMethod('Executing debouncer4! (i: $i)'));
    }


    // Make sure the above example finishes before continuting
    await Future.delayed(Duration(milliseconds: 400));


    // Example 4
    // When passing in a zero-duration, onExecute() is called synchronously. So the output of the code below would be
    // Executing debouncer5! (x: 1)
    // After debouncer5: x: 1
    print('\nExample 4');
    int x = 0;
    EasyDebounce.debounce('debouncer5', Duration.zero, (){ ++x; myMethod('Executing debouncer5! (x: $x)'); });
    print('After debouncer5: x: $x');


    // Make sure the above example finishes before continuting
    await Future.delayed(Duration(milliseconds: 400));


    // Example 5
    // Opposite to Example 4, when passing in a non-zero duration, onExecute() is called asynchronously using a Timer.
    // So the output of the code below would be
    // After debouncer6: y: 0
    // Executing debouncer6! (y: 1)
    print('\nExample 5');
    int y = 0;
    EasyDebounce.debounce('debouncer6', Duration(milliseconds: 10), (){ ++y; myMethod('Executing debouncer6! (y: $y)'); });
    print('After debouncer6: y: $y');

}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  easy_debounce: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:easy_debounce/easy_debounce.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
31
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

We analyzed this package on Sep 13, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test