Rx
Module Rx is the ixfx signal pattern. The basic principle is similar to event listeners, where we subscribe to values being emitted from a source. This is also known as a push or stream idiom, since we can’t pull values on-demand (although see pinging). Read more about push/pull semantics.
Here’s how the classic event listener looks. We subscribe to ‘pointermove’ event on ‘document’ source, with an anonymous function handling the data:
In classic events we choose to subscribe to a particular event. That is, the one event source (eg. a HTML element) can have several streams of events which can be selectively subscribed to. In contrast, reactive types really consist of a single stream of events.
Like generators, a reactive can work with finite sets of data (eg a list of things), or continuous streams of data which potentially have no end, such as a stream of pointermove
events.
Besides subscribing to a source, there is a notion of operators. Operators are reusable functions that process values one-by-one emitted by a stream. ixfx has in-built operators for common needs.
Why?
The reactive pattern can be useful for a few reasons.
- It allows you to compose data processing flows succinctly and legibly. You avoid the need to create and name variables to keep track of things, and less ‘plumbing’ to handle event-based sources
- It’s a good match for the event-driven nature of interaction on the web.
Here’s an example of a reactive in action: