Replies: 1 comment 4 replies
-
Hi @rhysm94, from my perspective what you've done seems completely fine and I like the |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've been working on a project where multiple dependencies use
Void
Publishers to indicate when something has completed - i.e. with our database dependency (GRDB), when we persist something in the database, thewritePublisher
's success type isVoid
. Similarly, with some of our API requests, we don't have any body in the response to decode, just we just care that has succeeded. However, in these instances, we do care if they have failed.One of the problems I was having with this is that turning an
AnyPublisher<Void, Failure>
into something that can easily be consumed by a reducer can be a bit verbose. My naïve approach was something along these lines:This approach works, but is verbose to add manually for every Void Publisher/Effect.
It also adds a requirement to use
Result<Never, Failure>
in the action which handles the response, due to the need to map from thecatchToEffect
. Thankfully, you don't have to handle the.success
case at all, as it cannot be created, but you still have to add theI'm fully aware I could just be missing something, and there's already a way to do this in TCA - if there is, please let me know, I'd be very grateful! However, since I can't currently find anything, after a discussion on the Swift Forums, I've implemented something very similar to
fireAndForget
that I callfireAndCatch
:This allows you to ignore any output from a
Publisher
other than that it has succeeded, and pushing theFailure
into theOutput
case if aFailure
does occur. This changes the above example to this:It's a small change on the whole, but I feel it makes dealing with Publishers which don't feed data back into the system nicer to work with. There's no handling an unnecessary
Result
type, there's no explicitflatMap
of thePublisher
toEmpty
, as it's all handled for you.I was originally going to scope this to an extension only when the
Output == Void
but I noticed that thefireAndForget
has become more permissive recently, allowing allowing it to be called on anyPublisher
, regardless ofOutput
orFailure
type, so I brought this idea intofireAndCatch
too.Is this a problem anyone else has encountered, and solved in other ways?
Beta Was this translation helpful? Give feedback.
All reactions