I’m trying to figure out how Apple’s own
sink subscriber (more specifically, the sink extension on
Publisher) is able to subscribe and then immediately return its subscription. Combine does offer a form of the
subscribe<S>() function, but that form is only available to Subject subclasses.
receive<S>(Subscriber) protocol method of the publisher doesn’t return anything, nor does the subscriber’s
receive(subscription: Subscription) method. Rather, the subscription is passed directly from the publisher to the subscriber. If the subscriber holds a strong reference to the subscription, there is potential for a memory leak if the subscription doesn’t complete.
The only thing I can think of that Apple might be doing for
sink is using an intermediary object that conforms to both Subscriber and Publisher and inserts itself into the hookup flow. Does anyone know how this actually works under the hood, or how to implement something similar?