Created
December 21, 2021 18:11
-
-
Save aydin-emre/f895fcf807783a1bf92eeb2c7316f6fb to your computer and use it in GitHub Desktop.
Custom PassthroughSubject, Publisher and Subscriber example
This file contains hidden or 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 | |
| enum MyError: Error { | |
| case subscriberError | |
| } | |
| class StringSubscriber: Subscriber { | |
| func receive(subscription: Subscription) { | |
| subscription.request(.max(2)) | |
| } | |
| func receive(_ input: String) -> Subscribers.Demand { | |
| print(input) | |
| return .max(1) | |
| } | |
| func receive(completion: Subscribers.Completion<MyError>) { | |
| print("Completion") | |
| } | |
| typealias Input = String | |
| typealias Failure = MyError | |
| } | |
| let subscriber = StringSubscriber() | |
| let subject = PassthroughSubject<String, MyError>() | |
| subject.subscribe(subscriber) | |
| let subscription = subject.sink(receiveCompletion: { (completion) in | |
| print("Received Completion from sink") | |
| }) { value in | |
| print("Received Value from sink") | |
| } | |
| subject.send("A") | |
| subject.send("B") | |
| subscription.cancel() | |
| subject.send("C") | |
| subject.send("D") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment