throttleEvent method

BlocThrottleEventCallback<E> throttleEvent(
  1. BlocThrottleEventCallback<E> function, {
  2. Duration duration = const Duration(milliseconds: 300),
})

Throttles an event. The event will be ignored if it is dispatched within the specified duration. The last event will be dispatched after the duration has passed.

For example:

 final throttled = throttleEvent((BlocEvent event) {
     // heavy stuff
 });

Implementation

BlocThrottleEventCallback<E> throttleEvent(
  BlocThrottleEventCallback<E> function, {
  Duration duration = const Duration(milliseconds: 300),
}) {
  final throttler = PublishSubject<Tuple2<BlocThrottleEventCallback<E>, E>>();
  publishers.add(throttler);

  subxList.add(
    throttler
        .throttleTime(duration)
        .listen((Tuple2<BlocThrottleEventCallback<E>, E> tuple) {
      tuple.item1(tuple.item2);
    }),
  );

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