Bloc: How to use debounce timer with states

Created on 4 Jul 2020  路  2Comments  路  Source: felangel/bloc

Describe the bug
I have this situation, where users can update the cart by pressing the add/remove button, so I added a debounce timer on the event stream.
The problem I'm facing with this,

  • Customer adds product 1, then between the denounce timer s/he adds another product, hence I'm losing the event where the user added product 1. I want to make sure that the debounce timer added only when the products are different.

    • And when yielding the states, I want to add a denounce timer also. How can I do that?

Here's the debounce timer for events:

 @override
  Stream<Transition<CloudCartBlocEvent, CloudCartBlocState>> transformEvents(
      Stream<CloudCartBlocEvent> events, transitionFn) {
    return super.transformEvents(
        events.debounceTime(
          Duration(
            milliseconds: 250,
          ),
        ),
        transitionFn);
  }
question

Most helpful comment

Thanks as always @RollyPeres!
@adar2378 closing for now but feel free to comment with additional questions and I鈥檓 happy to continue the conversation 馃憤

All 2 comments

Hi @adar2378 馃憢

You probably want to throttleTime cart events instead of debounceTime.

As for debouncing states you can override transformTransitions:

 @override
  Stream<Transition<Event, State>> transformTransitions(
    Stream<Transition<Event, State>> transitions,
   ) {
    return transitions.debounceTime(Duration(seconds: 1));
  }

Hope that helps 馃憤

Thanks as always @RollyPeres!
@adar2378 closing for now but feel free to comment with additional questions and I鈥檓 happy to continue the conversation 馃憤

Was this page helpful?
0 / 5 - 0 ratings