Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active April 10, 2021 21:19
Show Gist options
  • Save jasdev/cef6a2abecb8da784288e5c12017c64d to your computer and use it in GitHub Desktop.
Save jasdev/cef6a2abecb8da784288e5c12017c64d to your computer and use it in GitHub Desktop.
`CombineExt.Publisher.withLatestFrom`’s handling of finished events.
import CombineExt
// Scenario one: `withLatestFrom`’s argument finishes first.
let first = PassthroughSubject<Int, Never>()
let second = PassthroughSubject<String, Never>()
var cancellables = Set<AnyCancellable>()
first
.withLatestFrom(second) // CombineExt’s implementation
.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) // `first` is subbed in for `second` here.
// Outputs:
//
// ```
// one
// finished
// ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment