throttleEvent method

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

Creates a throttled function that only invokes function at most once per every duration.

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);
  };
}