Reactive programming - Observable
- 2. REACTIVE PROGRAMMING
WHAT IS REACTIVE PROGRAMMING?
There are plenty of bad explanation and definitions out there
on the internet:
▸ Wikipedia - is to generic and theoretical as usual.
▸ Stackoverflow’s - canonical answer is obviously not suitable for newcomers.
▸ Reactive Manifesto - sounds like the kind of … job position?
▸ Microsoft RX - is so heavy and Microsoftish that most of us are left confused.
- 3. REACTIVE PROGRAMMING
WHAT IS REACTIVE PROGRAMMING?
There are plenty of bad explanation and definitions out there
on the internet:
▸ Wikipedia - is to generic and theoretical as usual.
▸ Stackoverflow’s - canonical answer is obviously not suitable for newcomers.
▸ Reactive Manifesto - sounds like the kind of … job position?
▸ Microsoft RX - is so heavy and Microsoftish that most of us are left confused.
- 4. REACTIVE PROGRAMMING
WHAT IS REACTIVE PROGRAMMING?
My definition is:
▸ Reactive programming is programming with asynchronous
data streams.
In a way, this isn't anything new.
Event buses or your typical click events are really an asynchronous event stream, on which
you can observe and do some side effects.
Reactive is that idea on steroids. You are able to create data streams of anything, not just
from click and hover events. Streams are cheap and ubiquitous, can be a stream: variables,
user inputs, properties, caches, data structures, etc
- 6. REACTIVE PROGRAMMING
YOU CAN LISTEN TO THAT STREAM AND REACT ACCORDINGLY.
On top of that, you are given an amazing toolbox of functions to combine, create and filter any
of those streams. That's where the "functional" magic kicks in.
A stream can be used as an input to another one.
Even multiple streams can be used as inputs to another stream.
You can merge two streams.
You can filter a stream to get another one that has only those events you are interested in.
You can map data values from one stream to another new one.
- 7. REACTIVE PROGRAMMING
A STREAM
A stream is a sequence of ongoing events ordered in time. It can emit three different things: a
value (of some type), an error, or a "completed" signal. Consider that the "completed" takes
place, for instance, when the current window or view containing that button is closed.
We capture these emitted events only asynchronously, by defining a function that will execute
when a value is emitted, another function when an error is emitted, and another function when
'completed' is emitted.
The "listening" to the stream is called subscribing. The functions we are defining are observers.
The stream is the subject (or "observable") being observed.
- 9. REACTIVE PROGRAMMING
“WHY SHOULD I CONSIDER ADOPTING RP?”
Reactive Programming raises the level of abstraction of your code so you can focus on the
interdependence of events that define the business logic, rather than having to constantly
fiddle with a large amount of implementation details. Code in RP will likely be more concise.
The benefit is more evident in modern webapps and mobile apps that are highly interactive
with a multitude of UI events related to data events.
Apps nowadays have an abundancy of real-time events of every kind that enable a highly
interactive experience to the user. We need tools for properly dealing with that, and Reactive
Programming is an answer.
- 11. REACTIVE PROGRAMMING
“SO … DOES THIS MEAN THAT A PROMISE IS LIKE AN OBSERVABLE?”
Yes.
Observable is Promise++. In Rx you can easily convert a Promise to an Observable by doing
var stream = Rx.Observable.fromPromise(promise), so let's use that. The only difference is that
Observables are not Promises/A+ compliant, but conceptually there is no clash. A Promise is
simply an Observable with one single emitted value. Rx streams go beyond promises by
allowing many returned values.
This is pretty nice, and shows how Observables are at least as powerful as Promises.