Last active
April 10, 2021 21:19
-
-
Save jasdev/df46641f402eeef9a8a660dbb56c9e1b to your computer and use it in GitHub Desktop.
Initial, composed implementation of `Publisher.withLatestFrom`’s handling of finished events.
This file contains 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
// …back to using our implementation — | |
// **Scenario one**: `withLatestFrom`’s argument finishes first. | |
let first = PassthroughSubject<Int, Never>() | |
let second = PassthroughSubject<String, Never>() | |
var cancellables = Set<AnyCancellable>() | |
first | |
.withLatestFrom(second) | |
.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: .finished) | |
// Outputs: | |
// | |
// ``` | |
// one | |
// ``` | |
// **Scenario two**: Upstream finishes first. | |
// … | |
first.send(1) | |
second.send("one") | |
first.send(2) | |
second.send("two") | |
first.send(completion: .finished) | |
// Outputs: | |
// | |
// ``` | |
// one | |
// ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment