debounce_throttle 1.1.0

debounce_throttle #

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

ClassPurpose
DebouncerWait for changes to stop before notifying.
ThrottleNotifies 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

[1.1.0] - August 4, 2019

  • Use Observable from the simple_observable package.

[1.0.1] - July 19, 2019

  • Fix Throttle.

[1.0.0] - July 19, 2019

  • Renamed from simple_observable.

example/main.dart

// Example 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

import 'dart:io';
import 'dart:async';
import 'dart:math';
import 'package:debounce_throttle/debounce_throttle.dart';

void main() async {
  final debouncer = Debouncer<String>(Duration(milliseconds: 300),
      onChanged: debounceTick, initialValue: '');
  final throttle = Throttle<String>(Duration(milliseconds: 300),
      onChanged: throttleTick, initialValue: '');

  final random = Random();

  void doChange() {
    changeTick();
    debouncer.value += '.';
    throttle.value += '.';
  }

  startTicking();

  // Change frequently at first to show difference between debounce/throttle.
  for (int i = 0; i < 20; i++) {
    await Future.delayed(Duration(milliseconds: 50));
    doChange();
  }

  while (true) {
    // Wait 50 to 550 milliseconds.
    await Future.delayed(Duration(milliseconds: 50 + random.nextInt(500)));
    doChange();
  }
}

Timer startTicking() => Timer.periodic(Duration(milliseconds: 100), (timer) {
      String changed = '', debounced = '', throttled = '';
      ticks.forEach((tick) {
        changed += tick.changed ? '-' : ' ';
        debounced += tick.debounced ? tick.debounceError ? 'E' : 'D' : ' ';
        throttled += tick.throttled ? tick.throttleError ? 'E' : 'T' : ' ';
      });
      if (Platform.isWindows) {
        print(Process.runSync("cls", [], runInShell: true).stdout);
      } else {
        print(Process.runSync("clear", [], runInShell: true).stdout);
      }
      print('Tick     : ${'|' * ticks.length}');
      print('Changed  : $changed');
      print('Debounced: $debounced');
      print('Throttled: $throttled');
      if (ticks.length >= stdout.terminalColumns - 11) {
        exit(0);
      }
      ticks.add(Tick());
    });

/// For visual representation in the terminal.
class Tick {
  bool changed = false;
  bool debounced = false;
  bool throttled = false;
  bool debounceError = false;
  bool throttleError = false;
}

final ticks = [Tick()];

void changeTick() => ticks.last.changed = true;
void debounceTick(String value) {
  ticks.last.debounced = true;
  if (!debounceStopwatch.isRunning)
    debounceStopwatch.start();
  else {
    if (debounceStopwatch.elapsedMilliseconds < 300)
      ticks.last.debounceError = true;
    debounceStopwatch.reset();
  }
}

void throttleTick(String value) {
  ticks.last.throttled = true;
  if (!throttleStopwatch.isRunning)
    throttleStopwatch.start();
  else {
    if (throttleStopwatch.elapsedMilliseconds < 300)
      ticks.last.throttleError = true;
    throttleStopwatch.reset();
  }
}

final debounceStopwatch = Stopwatch();
final throttleStopwatch = Stopwatch();

Use this package as a library

1. Depend on it

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


dependencies:
  debounce_throttle: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or 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:debounce_throttle/debounce_throttle.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
47
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]
73
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.0.0 1.1.7
simple_observable ^1.1.0 1.1.0+2
Dev dependencies
test ^1.0.0