Skip to content

Instantly share code, notes, and snippets.

@IanKeen
Created June 3, 2021 19:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanKeen/72b84680ab2577878a41c9267e86a2e5 to your computer and use it in GitHub Desktop.
Save IanKeen/72b84680ab2577878a41c9267e86a2e5 to your computer and use it in GitHub Desktop.
Combine: ShareReplay - Shares the underlying resource and replays the last emitted value (if there was one) to new subscribers
extension Publisher {
func shareReplay() -> AnyPublisher<Output, Failure> {
let subject = CurrentValueSubject<Output?, Failure>(nil)
return map { $0 }
.multicast(subject: subject)
.autoconnect()
.compactMap { $0 }
.eraseToAnyPublisher()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment