Skip to content

Instantly share code, notes, and snippets.

@dhoerl
Last active March 31, 2020 19:48
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 dhoerl/ec6c3688ed42f95989e52d007a66ed5c to your computer and use it in GitHub Desktop.
Save dhoerl/ec6c3688ed42f95989e52d007a66ed5c to your computer and use it in GitHub Desktop.
Combine Subscriber for Medium
final class StringSubscriber: Subscriber {
typealias Input = [Character]
typealias Failure = Error
var subscription: Subscription?
var count = 0
func receive(subscription: Subscription) {
self.subscription = subscription
self.subscription?.request(.max(1))
}
func receive(_ input: Input) -> Subscribers.Demand {
input.forEach({ print(count == 0 ? "Chars:" : " ", $0, terminator: ""); self.count += 1 })
subscription?.request(.max(1))
return .max(1)
}
func receive(completion: Subscribers.Completion<Failure>) {
print("\nSubscriber completion: \(completion)")
self.subscription = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment