swiftui-notes icon indicating copy to clipboard operation
swiftui-notes copied to clipboard

implementing your own subscriber

Open heckj opened this issue 6 years ago • 1 comments

I started to add a section on Implementing your own subscriber, but realized there's a lot more learning that I need to do before I can knock it out.

I'm going to kick that back in priority to getting other elements done first.

Here's the snippet that I was thinking to start with, in developingwith.adoc:

== Implementing your own Subscriber

Developing with combine can frequently leverage the two common existing subscribers <<reference.adoc#reference-assign>> and <<reference.adoc#reference-sink>>, or you are working with an Apple framework (such as https://developer.apple.com/documentation/swiftui[SwiftUI]) that is the the subscriber to which you are integrating.

In some cases, you may want to create your own subscriber.
While <<reference.adoc#reference-sink>> can handle most situations, if you want to control the demand that drives the pipelines, you need to provide your own implementation.

__YET TO BE DEVELOPED__

https://developer.apple.com/documentation/combine/subscriber[`Subscriber protocol`]

types:
* Input
* Failure

protocol required functions:
* `receive(Self.Input) -> Subscribers.Demand`
* `receive(subscription: Subscription)`
* `receive(completion: Subscribers.Completion<Self.Failure>)`

* receive() -> Subscribers.Demand (when input void)

maybe based on https://developer.apple.com/documentation/combine/anysubscriber[`AnySubscriber`] which can be initialized with 3 closures:

* receiveSubscription: `((Subscription) -> Void)?``
* receiveValue: `((Input) -> Subscribers.Demand)?``
* receiveCompletion: `((Subscribers.Completion<Failure>) -> Void)?)``

heckj avatar Aug 11 '19 16:08 heckj

The few notes in the docs from:

  • https://developer.apple.com/documentation/combine/receiving_and_handling_events_with_combine
  1. implement/conform to the Subscriber protocol
  2. handling cancel() (part of Subscriber protocol):

If you create a custom Subscriber, the publisher sends a Subscription object when you first subscribe to it. Store this subscription, and then call its cancel() method when you want to cancel publishing. When you create a custom subscriber, you should implement the Cancellable protocol, and have your cancel() implementation forward the call to the stored subscription.

heckj avatar Dec 26 '19 18:12 heckj