Search code examples
system.reactivereactive-extensions-js

Requesting a clear, picturesque explanation of Reactive Extensions (RX)?


For a long time now I am trying to wrap my head around RX. And, to be true, I am never sure if I got it - or not.

Today, I found an explanation on http://reactive-extensions.github.com/RxJS/ which - in my opinion - is horrible. It says:

RxJS is to events as promises are to async.

Great. This is a sentence so full of complexity that if you do not have the slightest idea of what RX is about, after that sentence you are quite as dumb as before.

And this is basically my problem: All the explanations in the usual places you find about RX make (at least me) feel dumb. They explain RX as a highly sophisticated concept with lots of highly complicated words and terms and whatsoever, and I am never quite sure what it is about.

So my question is: How would you explain RX to someone who is five years old? I'd like a clear, picturesque explanation of what it is, what it is good for, and what its main concepts are?


Solution

  • So, LINQ (in JavaScript, these are high-level array methods like map, filter, reduce, etc - if you're not a C# dev, just replace that whenever I mention 'LINQ') gives you a bunch of tools that you can apply to Sequences ("Lists" in a crude sense), in order to filter and transform an input into an output (aka "A list that's actually interesting to me"). But what is a list?

    What is a List?

    A List, is some elements, in a particular order. I can take any list and transform it into a better list with LINQ.

    (Not necessarily sorted order, but an order).

    An Event is a List

    But what about an Event? Let's subscribe to an event:

    OnKeyUp += (o,e) => Console.WriteLine(e.Key)
    >>> 'H'
    >>> 'e'
    >>> 'l'
    >>> 'l'
    >>> 'o'
    

    Hm. That looks like some things, in a particular order. It now suddenly dawns upon you, a list and an event are the same thing!

    If Lists and Events are the Same....

    ...then why can't I transform and filter input events into more interesting events. That's what Rx is. It's taking everything you know about dealing with sequences, including all of the LINQ operators like Select and Where and Aggregate, and applies them to events.

    Easy peasy.

    A Callback is a Sequence Too

    Isn't a Callback just basically an Event that only happens once? Isn't it basically just like a List with one item? Turns out it is, and one of the interesting things about Rx is that it lets us treat Events and Callbacks (and things like Geolocation requests) with the same language (i.e. we can combine the two, or wait for ether one or the other, etc etc).