debounce_throttle 1.0.1 copy "debounce_throttle: ^1.0.1" to clipboard
debounce_throttle: ^1.0.1 copied to clipboard

outdated

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

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);
  final throttle =
      Throttle<String>(Duration(milliseconds: 300), onChanged: throttleTick);

  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();
61
likes
0
pub points
97%
popularity

Publisher

verified publisherdefylogic.dev

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

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta

More

Packages that depend on debounce_throttle