Created
January 30, 2023 16:58
-
-
Save joshuajhomann/5bcdebb22d41e9e92744173faa148f4f to your computer and use it in GitHub Desktop.
This file contains 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 | |
func pipeReplaySubject<Output>( | |
of type: Output.Type | |
) -> ( | |
input: (Output) -> Void, | |
output: some Publisher<Output, Never> | |
) { | |
let subject = CurrentValueSubject<Optional<Output>, Never>(nil) | |
return ( | |
input: subject.send, | |
output: subject.compactMap { $0 } | |
) | |
} | |
let (input, output) = pipeReplaySubject(of: Int.self) | |
var subscriptions: Set<AnyCancellable> = [] | |
output.sink { print("Subscribe before first send \($0)") }.store(in: &subscriptions) | |
input(1) | |
output.sink { print("Subscribe after first send \($0)")}.store(in: &subscriptions) | |
input(2) | |
output.sink { print("Subscribe after second send \($0)")}.store(in: &subscriptions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment