Skip to content

Instantly share code, notes, and snippets.

@wotjd
Last active December 27, 2022 13:10
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 wotjd/2ea79f03e4fdfa34b8db635c838755a3 to your computer and use it in GitHub Desktop.
Save wotjd/2ea79f03e4fdfa34b8db635c838755a3 to your computer and use it in GitHub Desktop.
import Combine
extension Publisher {
func makeHot() -> AnyPublisher<Output, Failure> {
let subject = PassthroughSubject<Void, Never>()
let publisher = map(Result.success)
.catch { Just(.failure($0)) }
.prefix(untilOutputFrom: subject)
.makeConnectable()
let connection = publisher.connect()
return publisher.flatMap(\.publisher)
.handleEvents(receiveCompletion: { _ in
subject.send(())
connection.cancel()
})
.eraseToAnyPublisher()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment