Last active
December 27, 2022 13:10
-
-
Save wotjd/2ea79f03e4fdfa34b8db635c838755a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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