Skip to content

Instantly share code, notes, and snippets.

@aydin-emre
Created December 21, 2021 18:11
Show Gist options
  • Select an option

  • Save aydin-emre/f895fcf807783a1bf92eeb2c7316f6fb to your computer and use it in GitHub Desktop.

Select an option

Save aydin-emre/f895fcf807783a1bf92eeb2c7316f6fb to your computer and use it in GitHub Desktop.
Custom PassthroughSubject, Publisher and Subscriber example
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