my_debounce 0.2.0 copy "my_debounce: ^0.2.0" to clipboard
my_debounce: ^0.2.0 copied to clipboard

Exactly what you are thinking about - debouncing a function call

debounce #

Dart package for exactly what you are thinking about - debouncing a function call.

void main() async {
  Debouncer d = Debouncer(time: Duration(milliseconds: 100));

  Function func = () {
    value += 1;
  };

  d.debounce(func);
  d.debounce(func);
  d.debounce(func);
  d.debounce(func);

  await Future.delayed(Duration(milliseconds: 110));

  print(value); // 1
}

The Debouncer class has an optional named parameter called leading. If you set it to true your callback will be fired immediately. For example:

void main() async {
  Debouncer d = Debouncer(time: Duration(milliseconds: 100, leading: true));

  Function func = () {
    value += 1;
  };

  d.debounce(func); // at this point value is 1
  d.debounce(func); // nothing happens
  d.debounce(func); // nothing happens
  d.debounce(func); // nothing happens
}

This library exists to satisfy the need of debouncing callbacks passed to methods like onPanUpdate. For example:

Debouncer d = Debouncer(time: Duration(milliseconds: 300));
...
GestureDetector(
  onPanUpdate: (details) {
    d.debounce(() {
      if (details.delta.dx > 0) {
        print('left');
      } else {
        print('right');
      }
    });
  },
0
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Exactly what you are thinking about - debouncing a function call

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

More

Packages that depend on my_debounce