Reactive extensions


In software programming, Reactive Extensions is a set of tools allowing imperative programming languages to operate on sequences of data regardless of whether the data is synchronous or asynchronous. It provides a set of sequence operators that operate on each item in the sequence. It is an implementation of reactive programming and provides a blueprint for the tools to be implemented in multiple programming languages.

Summary

ReactiveX is an API for asynchronous programming with observable streams.
Asynchronous programming allows programmers to call functions and then have the functions "callback" when they are done, usually by giving the function the address of another function to execute when it is done. Programs designed in this way often avoid the overhead of having many threads constantly starting and stopping.
Observable streams in the context of Reactive Extensions are like event emitters that emit 3 events: next, error, and complete. An observable emits next events until it either emits an error event or a complete event. However, at that point it will not emit any more events, unless it is subscribed to again.

Motivation

For sequences of data, it combines the advantages of iterators with the flexibility of event-based asynchronous programming. It also works as a simple promise, eliminating the pyramid of doom that results from multiple layers of callbacks.

Observables and observers

ReactiveX is a combination of ideas from the observer and the iterator patterns and from functional programming.
An observer subscribes to an observable sequence. The sequence then sends the items to the observer one at a time, usually by calling the provided callback function. The observer handles each one before processing the next one. If many events come in asynchronously, they must be stored in a queue or dropped. In ReactiveX, an observer will never be called with an item out of order or called before the callback has returned for the previous item. Asynchronous calls remain asynchronous and may be handled by returning an observable.
It is similar to the iterators pattern in that if a fatal error occurs, it notifies the observer separately. When all the items have been sent, it completes. The Reactive Extensions API also borrows many of its operators from iterator operators in other programming languages.
Reactive Extensions is different from functional reactive programming as the Introduction to Reactive Extensions explains:

It is sometimes called "functional reactive programming" but this is a misnomer. ReactiveX may be functional, and it may be reactive, but "functional reactive programming" is a different animal. One main point of difference is that functional reactive programming operates on values that change continuously over time, while ReactiveX operates on discrete values that are emitted over time.

Reactive operators

An operator is a function that takes one observable as its first argument and returns another observable. Then for every item that the source observable emits, it will apply a function to that item, and then emit it on the destination Observable. It can even emit another Observable on the destination observable. This is called an inner observable.
An operator that emits inner observables can be followed by another operator that in some way combines the items emitted by all the inner observables and emits the item on its outer observable. Examples include:
Operators can be chained together to create complex data flows that filter events based on certain criteria. Multiple operators can be applied to the same observable.
Some of the operators that can be used in Reactive Extensions may be familiar to programmers who use functional programming language, such as map, reduce, group, and zip. There are many other operators available in Reactive Extensions, though the operators available in a particular implementation for a programming language may vary.

Reactive operator examples

Here is an example of using the map and reduce operators. We create an observable from a list of numbers. The map operator will then multiply each number by two and return an observable. The reduce operator will then sum up all the numbers provided to it. Calling subscribe will register an observer that will observe the values from the observable produced by the chain of operators. With the subscribe method, we are able to pass in an error-handling function, called whenever an error is emitted in the observable, and a completion function when the observable has finished emitting items.

sourceObservable = Rx.Observable.from;
sourceObservable
.map
.reduce
.subscribe, function, function )

The above example uses the of Reactive Extensions for the JavaScript programming language.