Skip to content

An observer, debouncer, and throttle that works with Futures, Streams, and callbacks.

License

Notifications You must be signed in to change notification settings

jifalops/debounce_throttle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

debounce_throttle

A debouncer and throttle that works with Future, Stream, and callbacks.

Class Purpose
Debouncer Wait for changes to stop before notifying.
Throttle Notifies once per Duration for a value that keeps changing.

Usage

  final debouncer = Debouncer<String>(Duration(milliseconds: 200));

  // Run a search whenever the user pauses while typing.
  textEditingController.addListener(() => debouncer.value = textEditingController.text);
  debouncer.values.listen((search) => submitSearch(search));

  final throttle = Throttle<double>(Duration(milliseconds: 100));

  // Limit being notified of continuous sensor data.
  sensor.addListener(throttle.setValue);
  throttle.values.listen((data) => print('$data'));

example/main.dart output

Tick     : ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Changed  : -----------     -  --   - -  -    --  - -    -   -    -  -   - ---  -  - -  - -    -    -    - - - - --    -    -  -   - - -   --    - --    ---    -   -  - -     -    -     -  -  -
Debounced:              D     D   D     D  D          D    D   D    D  D          D         D    D    D             D    D    D  D       D    D       D      D    D        D     D    D     D
Throttled: T  T  T  T  T   T  T  T  T   T  T  T  T  T   T   T    T  T   T  T  T  T  T  T  T   T    T    T  T  T  T  T  T   T  T   T  T  T  T  T  T  T   T  T   T   T  T  T    T    T     T  T  T

About

An observer, debouncer, and throttle that works with Futures, Streams, and callbacks.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages