debounceEvent method

BlocDebounceEventCallback<E> debounceEvent(
  1. BlocDebounceEventCallback<E> function, {
  2. Duration delay = const Duration(milliseconds: 300),
})

Debounces an event. The function will not be invoked until delay.

For example:

 final debounced = debounce((BlocEvent event) {
     // heavy stuff
 });

Implementation

BlocDebounceEventCallback<E> debounceEvent(
  BlocDebounceEventCallback<E> function, {
  Duration delay = const Duration(milliseconds: 300),
}) {
  final debouncer = PublishSubject<Tuple2<BlocDebounceEventCallback<E>, E>>();
  publishers.add(debouncer);

  subxList.add(
    debouncer
        .debounceTime(delay)
        .listen((Tuple2<BlocDebounceEventCallback<E>, E> tuple) {
      tuple.item1(tuple.item2);
    }),
  );

  return (E event) {
    final tuple = Tuple2<BlocDebounceEventCallback<E>, E>(function, event);
    debouncer.add(tuple);
  };
}