Effects and Publishers : how to return async Effect #2420
Closed
JeromeTonnelierOgury
started this conversation in
General
Replies: 1 comment 7 replies
-
Hi @JeromeTonnelierOgury, there is a much simpler way to achieve what you want. It is possible to create an effect from a Combine publisher using the So, instead of this (which btw, where does return .run { send in
videoManager
.videoLoaded
.sink { _ in } receiveValue: { _ in
Task {
await send(.changeState(.loaded))
}
}
.store(in: &storage.cancellablles) …you can simply do this: return .publisher {
videoManager.videoLoaded
.map { _ in .changeState(.loaded) }
} And if you need to run multiple of these publishers you can merge them together, either as publishers: return .publisher {
videoManager.videoLoaded
.map { _ in .changeState(.loaded) }
.merge(
with: videoManager.loadError
.map(Action.error)
)
} …or you can even merge them as effects: return .merge(
videoManager.videoLoaded
.map { _ in .changeState(.loaded) },
videoManager.loadError
.map(Action.error)
) |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone!
We finally had our subscription running since yesterday, and I'm stuck on a particular issue (Don't know if I made the right choise though). I'll keep looking at the videos, but if in the meantime, someone has a clue, I'm all ears :D
So basically, I have a reducer with a dependency that has various PassthroughSubjects (they are here to wrap an actual delegate into a more TCA friendly flow).
I'll keep my example of the Video framework.
So I have a VideoManager as follows (drop the DependecyKey part to ease the understanding)
Yes, it seems a bit weird to have some passthroughSubject for the errors, but the errors can come from various source, and I guess it was easier that way. I'll try to tackle that in the next refinement.
Now my Reducer would have everything to handle UI (Spinners, Buttons, ...).. One of the actions would be
loadButtonTapped
and here is where it all goes south.but the
await send
has no effect inside thesink
methods (which I guess makes sense)What I wanted to achieve was, once the
loadButtonTapped
was fired :videoLoaded
and when it succeeds, inform the reducer with a.changeState(.loaded)
loadError
and when it succeeds, inform the reducer with a.error(.error)
videoManager.load()
If someone can point me out some code that would use a Subject "return" value inside an Effect, it would be great.
Maybe AsyncStreams would be better than pure Combine stuff, but I guess it's doable to use a PassthroughSubject inside an Effect right ?
Thanks a lot for your help 🙏
Beta Was this translation helpful? Give feedback.
All reactions