Combine has two related functions that support "demand", where Subscribers inform Publishers on the desired number of elements passed to them in a "receive" function. The below ignores infinite demand.
- func request(_ demand: Subscribers.Demand)
Subscriptions provide this function, and as the Apple Docs say:
"Tells a publisher that it may send more values to the subscriber."
Matt Gallagher supposes in his excellent 22 Combine Tests article that each of these demands should be additive, and when the Subscription sends elements to the Subscriber, it decrements the count it maintains.
- func receive(_ input: Self.Input) -> Subscribers.Demand
When a Subscriber receives data, it returns another demand, which the Apple docs state is:
"A Subscribers.Demand instance indicating how many more elements the subscriber expects to receive."
I have seen various interpretations on how these numbers relate, and I of course have my own that I'll postulate here.
A Publisher has a one element, and it gets a 'request(.max(10))' When it sends the element to the Subscriber, then the return demand should be '.max(9)', a reminder to the Subscription that it's expecting 9 more elements.
If for some reason the Subscriber decides to send in another request for .max(10), and the Subscription gets one more element, then messages the Subscriber with that one element, the return should then be .max(18), meaning, Subscriber wanted 10, then it wanted 10 more, but it has only received 2 and still wants 18 more.
Alternate interpretations seem to be that the return from receive is additive to the running demand. In this scenario any number other than 0 will increase what the Subscription can send.
Would be super if anyone in the know could help clarify!!!