Comments (4)
Hey @mathieutozer, good question! And it's definitely a bit of an oversight right now.
The way to handle this is to construct an Effect<Result<[Video], Error>>
, and then you would have an action that takes that payload:
enum AppAction {
...
case refreshVideosResponse(Result<[Video], Error>)
}
However, the current eraseToEffect
only works when the Failure
of the Publisher
is Never
. We should have another overload that works on unconstrained Publisher
and it puts the failure in a Result
.
extension Publisher {
public func eraseToEffect() -> Effect<Result<Output, Failure>> {
self.map(Result.success)
.catch { Just(Result.failure($0)) }
.eraseToEffect()
}
}
If you use that method in your example you should get a proper Effect<Result<_, _>>
that will probably be useful to you.
Does that make sense?
from episode-code-samples.
Thanks @mbrandonw! I was able to conceive of the first step, but didn't have the chops to figure out the second.
I am now having trouble holding onto a long running cancellable effect (I think that is my issue). I tried implementing
@available(iOS 13.0, *)
public extension Publisher {
func cancellable<Id: Hashable>(id: Id) -> Effect<Result<Output, Failure>> {
return Deferred { () -> PassthroughSubject<Output, Failure> in
cancellables[id]?.cancel()
let subject = PassthroughSubject<Output, Failure>()
cancellables[id] = self.subscribe(subject)
return subject
}
.eraseToEffect()
}
static func cancel<Id: Hashable>(id: Id) -> Effect<Result<Output, Failure>> {
.fireAndForget {
cancellables[id]?.cancel()
}
}
}
As per other advice found in the issues here, but the other implementation where Failure == Never get used instead
@available(iOS 13.0, *)
extension Publisher where Failure == Never {
public func cancellable<Id: Hashable>(id: Id) -> Effect<Output> {
return Deferred { () -> PassthroughSubject<Output, Failure> in
cancellables[id]?.cancel()
let subject = PassthroughSubject<Output, Failure>()
cancellables[id] = self.subscribe(subject)
return subject
}
.eraseToEffect()
}
public static func cancel<Id: Hashable>(id: Id) -> Effect<Output> {
.fireAndForget {
cancellables[id]?.cancel()
}
}
}
from episode-code-samples.
I believe there shouldn't be a need to define overloads on for cancellation
. You should be able to get away with the one definition, as seen here: https://gist.github.com/mbrandonw/cb4848c9896680af6d250d9ae5f15cdd
Did you need the overload for something in particular?
from episode-code-samples.
You are correct - my issue was that I hadn't set up a publisher correctly so the subscription closed once the first effect was received. Still a new to these patterns!
from episode-code-samples.
Related Issues (20)
- @FocusState not working with new @BindableState API HOT 5
- UIApplication.shared.windows deprecated in iOS 15 HOT 1
- 0167 Navigation is weird on iPad HOT 1
- Please fix the error Episode #76 ~ HOT 2
- [0171-modularization-pt1] Setting Up HOT 3
- modularization folders architecture HOT 1
- Missing episode 187: URL Route? HOT 1
- Cannot convert value of type - Examples will not compile using Xcode 13.3 HOT 2
- Request: Re-Record Episode 100 to 103 and Update Source Code HOT 3
- Episode 196: Runtime warning work only in half of the cases HOT 1
- minor bug in episode 198 sample HOT 1
- Code sample for episode 220 is missing HOT 1
- Strange navigation on episode 220 HOT 1
- Episode 2 -> Fish playground -> Ambiguous use of 'init(utf8String:)'
- Episode 0228 - Composable Navigation: Destinations sample code not found HOT 1
- navigationDestination not triggered HOT 3
- Modern SwiftUI: Standups App HOT 1
- Episode 241 code not published HOT 2
- Episode 109 sheet show twice when item name changed HOT 1
- Typo in Navigation Example code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from episode-code-samples.