Created
October 23, 2019 12:56
-
-
Save mbrandonw/1c4ca76f1927420921b3871dd2153497 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
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