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
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 andyield*
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.
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 insideasync*
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
can be rewritten as:
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 themapEventToState
implementation.Hope that helps and you can check out the Dart Docs for more information 馃憤