throttle_debounce 0.1.2+1

  • README.md
  • Example
  • Installing
  • Versions
  • 79

Dart throttle/debounce

A library for throttling or debouncing frequently occuring events. This package is inspired by jquery throttle/debounce http://benalman.com/projects/jquery-throttle-debounce-plugin

Example

import 'package:throttle_debounce/throttle_debounce.dart';
import 'dart:html' as html;
main() {
    html.Element input = html.querySelector('#inputBox');
    List args = ['argument1', 'argument2'];
    var debouncer = new Debouncer(const Duration(milliseconds:250), callback, args);
    input.onKeyUp.listen((e) {
        debouncer.debounce();
    });
}

void callback(List args) {
    print(args[0]); //will print 'argument1'
    print(args[1]); //will print 'argument2'
}

Throttle

Execute a frequently called function only once in a fixed interval. See http://benalman.com/code/projects/jquery-throttle-debounce/examples/throttle/ for example of throttling.

Usage and Arguments

var throttler = new Throttler(Duration delay, callback, List args, [bool noTrailing]);
element.onKeyUp.listen((e) {
    throttler.throttle();
});

delay - (const Duration) A dart constant of type Duration which.

callback - (Function) A function which is executed after delay milliseconds.

args - (List) argument passed to the callback. (Dart does not support variable number of arguments to a function, so we have to pass the arguments in a list)

noTrailing - (bool) Optional, defaults to false. If noTrailing is true, callback will be called after delay only when the throttle function is being called. If set to false, there will also be one trailing call to the callback after delay after the throttle function has stopped executing.

Debouncing

Debounces the execution of a function. This ensures that the function is executed only once, either at the beginning of a series of calls or at the end. See http://benalman.com/code/projects/jquery-throttle-debounce/examples/debounce/ for example of debouncing.

Usage and Arguments

var debouncer = new Debouncer(Duration delay, callback, List args, [bool atBegin]);
element.onKeyUp.listen((e) {
    debouncer.debounce();
});

delay - (const Duration) A dart constant of type Duration which.

callback - (Function) A function which is executed after delay milliseconds.

args - (List) argument passed to the callback. (Dart does not support variable number of arguments to a function, so we have to pass the arguments in a list)

atBegin - (bool) Optional, defaults to false. If atBegins is true, the callback will be executed at the beginning of the debounced call. If there is a pause of delay duration, callback will again be called when the debounced call starts again. If atBegins is false, callback will be executed whenever there is a pause of delay duration between the debounce calls i.e. at the end.

Licenses

Licensed under MIT

Acknowledgement

Inspired by Jquery throttle/debounce plugin of Ben Alman (http://benalman.com)

example/main.dart

import 'package:throttle_debounce/throttle_debounce.dart';
import 'dart:html' as html;


main() {
  html.Element input1 = html.querySelector('#inputBox1');
  var debouncer = new Debouncer(const Duration(milliseconds:250), callback, ['called'], true); 
  input1.onKeyUp.listen((e) {
    debouncer.debounce();
  });
  html.Element input2 = html.querySelector('#inputBox2');
  var throttler = new Throttler(const Duration(milliseconds:250), callback, ['throttled'], true);
  input2.onKeyUp.listen((e) {
    throttler.throttle();
  });
}

callback(List args) {
  print(args[0]);
}

1. Depend on it

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


dependencies:
  throttle_debounce: "^0.1.2+1"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:throttle_debounce/throttle_debounce.dart';
        
Version Uploaded Documentation Archive
0.1.2+1 Nov 6, 2016 Go to the documentation of throttle_debounce 0.1.2+1 Download throttle_debounce 0.1.2+1 archive
0.1.2 Sep 15, 2014 Go to the documentation of throttle_debounce 0.1.2 Download throttle_debounce 0.1.2 archive
0.1.1 Sep 14, 2014 Go to the documentation of throttle_debounce 0.1.1 Download throttle_debounce 0.1.1 archive
0.1.0 Sep 14, 2014 Go to the documentation of throttle_debounce 0.1.0 Download throttle_debounce 0.1.0 archive

Analysis

We analyzed this package on Apr 9, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.46.0
  • pana: 0.10.6

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
80 / 100
Health:
Code health derived from static analysis. [more]
98 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
46 / 100
Overall score:
Weighted score of the above. [more]
79
Learn more about scoring.

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:throttle_debounce/throttle_debounce.dart.

Suggestions

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 hint.

    Run dartfmt to format lib/throttle_debounce.dart.