Skip to content

Instantly share code, notes, and snippets.

@joshuajhomann
Created January 30, 2023 16:58
Show Gist options
  • Save joshuajhomann/5bcdebb22d41e9e92744173faa148f4f to your computer and use it in GitHub Desktop.
Save joshuajhomann/5bcdebb22d41e9e92744173faa148f4f to your computer and use it in GitHub Desktop.
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