Skip to content

Instantly share code, notes, and snippets.

@lammertw
Created March 8, 2021 17:57
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 lammertw/1d9e10889da01f4819b405ce81d1e02e to your computer and use it in GitHub Desktop.
Save lammertw/1d9e10889da01f4819b405ce81d1e02e to your computer and use it in GitHub Desktop.
func asPublisher<T>(_ flow: CFlow<T>) -> AnyPublisher<T, Never> {
return Deferred<Publishers.HandleEvents<PassthroughSubject<T, Never>>> {
let subject = PassthroughSubject<T, Never>()
let closable = flow.watch { next in
if let next = next {
subject.send(next)
}
}
return subject.handleEvents(receiveCancel: {
closable.close()
})
}.eraseToAnyPublisher()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment