Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active April 4, 2021 00:32
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/5ffeafb41b933aba8bb0740384715db5 to your computer and use it in GitHub Desktop.
Save jasdev/5ffeafb41b933aba8bb0740384715db5 to your computer and use it in GitHub Desktop.
`AnyObject`-constrained `withLatestFrom` overload
extension Publisher where Self: AnyObject {
func withLatestFrom<Other: Publisher>(
_ other: Other
) -> AnyPublisher<Other.Output, Other.Failure>
where Failure == Other.Failure {
other
.map { [weak self] second in
self?.map { _ in second }.eraseToAnyPublisher() ??
Empty().eraseToAnyPublisher()
}
.switchToLatest()
.eraseToAnyPublisher()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment