Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform Example #19

Closed
rsnider19 opened this issue Nov 24, 2018 · 3 comments
Closed

Transform Example #19

rsnider19 opened this issue Nov 24, 2018 · 3 comments
Assignees
Labels
question Further information is requested
Projects

Comments

@rsnider19
Copy link

Can you please provide an example of overriding transform? Specifically debouncing a certain event (e.g. Search), but not other events in the bloc? Thanks!

@felangel felangel self-assigned this Nov 24, 2018
@felangel felangel added the question Further information is requested label Nov 24, 2018
@felangel
Copy link
Owner

felangel commented Nov 24, 2018

Hi @rsnider19 if we use the CounterBloc as an example and introduce a Search event which we want to debounce you can do something like:

@override
Stream<CounterState> transform(
  Stream<CounterEvent> events,
  Stream<CounterState> Function(CounterEvent event) next,
) {
    final observableStream = events as Observable<CounterEvent>;
    final nonDebounceStream = observableStream.where((event) {
      return (event is! Search);
    });
    final debounceStream = observableStream.where((event) {
      return (event is Search);
    }).debounce(Duration(milliseconds: 300));
    return super.transform(nonDebounceStream.mergeWith([debounceStream]), next);
}

Essentially, in the transform override you have access to the raw Stream<Event> and you can do whatever manipulation/transformation and return the new Stream<Event> which will be used by mapEventToState.

In a simple case where you want to debounce all events you can simply do:

@override
Stream<CounterState> transform(
  Stream<CounterEvent> events,
  Stream<CounterState> Function(CounterEvent event) next,
) {
   return super.transform(
      (events as Observable<CounterEvent>).debounce(
        Duration(milliseconds: 300),
      ),
      next,
    );
}

You can checkout the github search example app for an example of debouncing all events.

Let me know if that makes sense or if you have other questions.

@felangel felangel added the waiting for response Waiting for follow up label Nov 24, 2018
@felangel felangel added this to In progress in bloc Nov 24, 2018
@felangel
Copy link
Owner

Closing this for now but feel free to comment with any additional questions and I'll reopen it 👍

@felangel felangel removed the waiting for response Waiting for follow up label Nov 25, 2018
@rsnider19
Copy link
Author

Worked perfect! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
bloc
  
Done
Development

No branches or pull requests

2 participants