throttleEvent method

BlocEventCallback<FastThemeBlocEvent> throttleEvent(
  1. BlocEventCallback<FastThemeBlocEvent> function, {
  2. Duration duration = const Duration(milliseconds: 300),
})
inherited

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

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

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

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