Introduction to Reactive programming: part 2
Hi there! How are you? I hope you are well!
In our first part of the series of posts (I really have no idea how many there will be eventually…) we stopped when we reached the notion of Push systems! Let’s resume from there and see where this will lead us.
Push technologies
A push technology works like this:
The server or publisher of the content or data is responsible for providing updates automatically to subscribed clients. So, the client does not request explicitly each time a resource from the server, but it is already subscribed to a feed of data once and thereafter is receiving latest changes and updates automatically. I think that with this description a lot of applications you use everyday work like this, for example a Slack channel or some Facebook group/page etc. All good so far. But I mentioned Observer pattern in the end of the previous post. Why?
The Observer pattern
Which are the similarities of this pattern with the push protocol? As you probably remember, we started this inquisition from the RxJs library where the Observable is, sort of speaking, the first-class citizen there! It is more than obvious that the Observer pattern and the Observable must have some kind of connection, right? Well, let us investigate a little bit what the pattern is first and what it does and what problems is trying to solve!
The Observer pattern is described by using an object, called Subject which keeps inside it a list of Subscriber objects and whenever there is an update in it, it automatically informs all of the Subscribers of its state changes. That is a simple description of the pattern and once more you can think of some application where you have some kind of subscription to a topic, subject, post, article, channel, chat etc. and when there is some kind of activity going on from the other peers of yours (friends, colleagues, fans etc, you name it) you usually get an push notification. And this happens, live!
The main problem this pattern is trying to solve is how to communicate internal state changes between one to many objects that are automatically notified each time by the one object which calls for the many objects and updates them.
So, reading the RxJs docs we see that they combine the Observer with another pattern called Iterator pattern, which we will examine in the following section especially the JavaScript implementation of this.
Iterator pattern
JavaScript supports the Iterator pattern with any object that provides a next() method, which returns an object with two specific properties: done and value. We will give some code example to understand better this pattern which is super important to grasp if you want to be a good modern JavaScript developer!
But this is a story for the next post. This one is getting long enough.
Cheers!
Please follow https://status.npmjs.org/incidents/41zfb8qpvrdj for updates.
While I am delighted to be part of such an epic thread, for the sake of the good people at Github I’m locking this now.