Bloc: Async*, Yield* and MapEventToState Question

Created on 20 Apr 2019  路  3Comments  路  Source: felangel/bloc

Newbie alert.

Really helpful package! I'm trying to understand how MapEventToState works exactly, and I noticed that in some cases you use yield and in others you use yield*. It looks like you are using yield* when you are calling a function that uses async*, but I'm sure there's more to it. I looked up yield* and it addresses recursion, from what I can tell.

Can you tell me me when to use which? TIA

2019-04-20_15h50_38

question

Most helpful comment

Hey @nhwilly 馃憢
Thanks for the positive feedback and there's no such thing as a newbie question haha!

yield in Dart can be used inside async* functions (async generators) in order to add the result to the resulting stream that is returned by the function.

yield* (yield-each) inserts all the elements of the subsequence into the sequence currently being constructed, as if we had an individual yield for each element.

In the above example

yield* _mapLogoutToState()

can be rewritten as:

await _userRepository.signOut();
yield Unauthenticated();

directly within mapEventToState.

The advantage of using yield* is that you can break out the complex logic for mapping each event to a stream of states into separate private helper functions (like _mapLogoutToState) in order to avoid cluttering the mapEventToState implementation.

Hope that helps and you can check out the Dart Docs for more information 馃憤

All 3 comments

Hey @nhwilly 馃憢
Thanks for the positive feedback and there's no such thing as a newbie question haha!

yield in Dart can be used inside async* functions (async generators) in order to add the result to the resulting stream that is returned by the function.

yield* (yield-each) inserts all the elements of the subsequence into the sequence currently being constructed, as if we had an individual yield for each element.

In the above example

yield* _mapLogoutToState()

can be rewritten as:

await _userRepository.signOut();
yield Unauthenticated();

directly within mapEventToState.

The advantage of using yield* is that you can break out the complex logic for mapping each event to a stream of states into separate private helper functions (like _mapLogoutToState) in order to avoid cluttering the mapEventToState implementation.

Hope that helps and you can check out the Dart Docs for more information 馃憤

I think you should write all the Dart documentation. I started out reading it and got confused.

This is the clearest explanation I've seen.

It makes sense. yield is used to add a single instance to the stream and yield* adds a (potential) stream of instances to the stream.

Thanks!

I think you should write all the Dart documentation. I started out reading it and got confused.

This is the clearest explanation I've seen.

It makes sense. yield is used to add a single instance to the stream and yield* adds a (potential) stream of instances to the stream.

Thanks!

Yes, and keep in mind that the yield*ed function doesn't return until it's completed. It's not like a future that allows continued execution. Correct me if I'm wrong @felangel, but yield* someFunction can be continued synchronous asynchrony.

Was this page helpful?
0 / 5 - 0 ratings