Skip to content

Instantly share code, notes, and snippets.

@jasdev
Created March 24, 2021 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/9503435250951e051b7dd0cca471b2e1 to your computer and use it in GitHub Desktop.
Save jasdev/9503435250951e051b7dd0cca471b2e1 to your computer and use it in GitHub Desktop.
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