Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Non-`AnyObject` constrained forms of `Publisher.withLatestFrom`.
import Combine
extension Publisher {
func withLatestFrom<Other: Publisher>(
_ other: Other
) -> AnyPublisher<Other.Output, Other.Failure>
where Failure == Other.Failure {
other
.map { second in map { _ in second } }
.switchToLatest()
.eraseToAnyPublisher()
}
}
extension Publisher {
func withLatestFrom<Other: Publisher, Transformed>(
_ other: Other,
_ selector: @escaping (Output, Other.Output) -> Transformed
) -> AnyPublisher<Transformed, Other.Failure>
where Failure == Other.Failure {
other
.map { second in map { first in selector(first, second) } }
.switchToLatest()
.eraseToAnyPublisher()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment