Last active
April 10, 2021 21:18
-
-
Save jasdev/7e865df7cfd0ef98f7376dfb1968861a to your computer and use it in GitHub Desktop.
Initial, composed implementation of `Publisher.withLatestFrom`’s handling of error events.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// **Scenario one**: `withLatestFrom`’s argument erroring out first. | |
struct SomeError: Error {} | |
let first = PassthroughSubject<Int, SomeError>() | |
let second = PassthroughSubject<String, SomeError>() | |
var cancellables = Set<AnyCancellable>() | |
first | |
.withLatestFrom(second) // Our composed operator implementation of `withLatestFrom`. | |
.sink(receiveCompletion: { print($0) }, receiveValue: { print($0) }) | |
.store(in: &cancellables) | |
first.send(1) | |
second.send("one") | |
first.send(2) | |
second.send("two") | |
second.send(completion: .failure(.init())) | |
// Outputs: | |
// | |
// ``` | |
// one | |
// failure(SomeError()) | |
// ``` | |
// **Scenario two**: Upstream erroring out first. | |
// … | |
first.send(1) | |
second.send("one") | |
first.send(2) | |
second.send("two") | |
first.send(completion: .failure(.init())) // `first` is subbed in for `second` here. | |
// Outputs: | |
// | |
// ``` | |
// one | |
// failure(SomeError()) | |
// ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment