Creating custom publisher and operators. 👨🏿‍🔧

By Denis Poifol

Elevator Pitch

Combine is a great tool because of its declarative syntax. Which is enabled by the use of operators. But sometimes the built-in operators of Combine fall short or just feel awkward in our use cases. We will see in this talk how we can create our own custom operators to feel this gap.

Description

Combine relies on three major type of objects :

  • Publisher (that produce events)
  • Subscriber (that subscribe to a stream of events)
  • Subscription (that enable the formers to work together)

Each of the above are only defined by protocols, this is a great news : this means Combine was build open for extensions. We are invited by the framework to create for ourselves whatever might be missing to our use cases. Unfortunately there are many wrong way to create a publisher and Combine does not come with a documentation on how to create your own publishers (hell, it did not come with a documentation at all when it was first released).

In order to avoid taking a wrong turn in the implementation we need to fully grasp how does combine components work behind the scene and how the rely on each other.

During this talk we will first look a recreating one of the simplest Publisher out there : Just, once our implementation is complete and we have learned from implementing something simple, we will look at creating something a bit more complex : an enumerated operator

Through this talk we will look at the relationship between Publisher Subscriber and Subscription and what is almost always forgotten back pressure.

Provided there is enough time I would like to also talk about the relation between the object from a memory point of view to understand the magic combine uses to prevent retain cycles.

Notes

I will try to explain everything from the ground up as much as possible. But I doubt that someone that does not know a thing about combine can keep up with this subject. If you want to get an idea of what the narrative of the talk will look like, I will probably build from the playground I have already created about combine that go through this subjects : https://github.com/denisPoifol/CombinePlaygrounds/tree/CombineInDepth