Group Group Group Group Group Group Group Group Group

Chapter 2: Custom Subscriber

I don’t understand why the output of the following code:

example(of: "Custom Subscriber") {
    let publisher = (1...10).publisher

    final class IntSubscriber: Subscriber {
        typealias Input = Int
        typealias Failure = Never

        func receive(subscription: Subscription) {
            subscription.request(.max(3))
        }

        func receive(_ input: Int) -> Subscribers.Demand {
            print("Received value", input)
            return .max(1)
        }

        func receive(completion: Subscribers.Completion<Never>) {
            print("Received completion", completion)
        }
    }

    let subscriber = IntSubscriber()
    publisher.subscribe(subscriber)
}

is the following code:

**——— Example of: Custom Subscriber ———**

**Received value 1**

**Received value 2**

**Received value 3**

**Received value 4**

**Received value 5**

**Received value 6**

**Received value 7**

**Received value 8**

**Received value 9**

**Received value 10**

**Received completion finished**

We initially requested for 3 items, and each time we increased the request by one, so totally we should receive 6 items and shouldn’t have reached the completion.