Hamed Rezaee
Hamed Rezaee

Reputation: 7252

how to implement debouncer for events with 'onEvent'?

transformEvents method will be removed in bloc version 8, and we should use onEvent method instead of, how can we implement debounce for events with onEvent?

  @override
  Stream<Transition<PriceProposalEvent, PriceProposalState>> transformEvents(
    Stream<PriceProposalEvent> events,
    TransitionFunction<PriceProposalEvent, PriceProposalState> transitionFn,
  ) =>
      super.transformEvents(
        events.debounceTime(const Duration(milliseconds: 200)),
        transitionFn,
      );

Upvotes: 3

Views: 1699

Answers (2)

Jonas Cerqueira
Jonas Cerqueira

Reputation: 370

New in Bloc 7.2.0 https://verygood.ventures/blog/whats-new-in-bloc-v7-2-0

Now it uses transformer!

import 'package:bloc/bloc.dart';
import 'package:stream_transform/stream_transform.dart';

class YourBloc extends Bloc<Event, State> {
  YourBloc() : super(StateInitial()) {

    on<PriceProposalEvent>(_onPriceProposalEvent,
        transformer: debounce(const Duration(milliseconds: 200)));
  }
}
//Debounce query requests
EventTransformer<E> debounce<E>(Duration duration) {
  return (events, mapper) {
    return events.debounce(duration).switchMap(mapper);
  };
}

Hope, it may help ya!

Upvotes: 3

cas
cas

Reputation: 817

using the rxdart library for the stream methods that I see you're already using:

static Stream<T> debounce<T>(
    Stream<T> events,
    Stream<T> Function(T) transitionFn,
) {
    return events
        .debounceTime(const Duration(milliseconds: 200))
        .switchMap(transitionFn);
}

Upvotes: 0

Related Questions