Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Created October 23, 2019 12:56
Show Gist options
  • Save mbrandonw/1c4ca76f1927420921b3871dd2153497 to your computer and use it in GitHub Desktop.
Save mbrandonw/1c4ca76f1927420921b3871dd2153497 to your computer and use it in GitHub Desktop.
extension Publisher {
func cancellable<Id: Hashable>(id: Id) -> AnyPublisher<Output, Failure> {
return Deferred { () -> PassthroughSubject<Output, Failure> in
cancellables[id]?.cancel()
let subject = PassthroughSubject<Output, Failure>()
cancellables[id] = self.subscribe(subject)
return subject
}
.eraseToAnyPublisher()
}
}
private var cancellables: [AnyHashable: AnyCancellable] = [:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment