debounce<T> function

Worker debounce<T>(
  1. RxInterface<T> listener,
  2. dynamic callback(
    1. T
    ),
  3. {Duration time}
)

debounce is similar to interval, but sends the last value. Useful for Anti DDos, every time the user stops typing for 1 second, for instance. When listener emits the last "value", when time hits, it calls callback with the last "value" emitted.

Sample:

worker = debounce(
     count,
     (value) {
       print(value);
       if( value > 20 ) worker.dispose();
     },
     time: 1.seconds,
   );
 }

Implementation

Worker debounce<T>(RxInterface<T> listener, Function(T) callback,
    {Duration time}) {
  final _debouncer =
      Debouncer(delay: time ?? const Duration(milliseconds: 800));
  StreamSubscription sub = listener.subject.stream.listen((event) {
    _debouncer(() {
      callback(event);
    });
  });
  return Worker(sub.cancel, '[debounce]');
}