Reactive extensions

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In software programming, Reactive Extensions (also known as ReactiveX) 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.

Why Reactive Extensions[edit]

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 multiple layers of callbacks.

What is Reactive Extensions[edit]

According to http://reactivex.io/, 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 (i.e. streams that can be observed) 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.

ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming.[1]

  • 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 (in a multi-threaded context) called before the callback has returned for the previous item. Asynchronous calls are still asynchronous and may be handled by returning an observable.[citation needed]
  • It is like Iterators in that if a fatal error occurs, it notifies the observer separately (by calling a second function), and when all the items have been sent, it completes (and notifies the observer by calling a third function). It also borrows many of its operators from Iterators.

From http://reactivex.io/intro.html

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. (See Conal Elliott’s work for more-precise information on functional reactive programming).

An operator is a function that takes one observable (the source) as its first argument and returns another Observable (the destination, or outer 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:

  • switchAll - subscribes to each new inner observable as soon as it is emitted and unsubscribes from the previous one.
  • mergeAll - subscribes to all inner observables as they are emitted and outputs their values in whatever order it receives them.
  • concatAll - subscribes to each inner observable in order and waits for it to complete before subscribing to the next observable.

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. In the example below, sourceObservable could be another chain of operators that does not end with the subscribe operator.

sourceObservable //[1, 2, 3, 4, 5]
//multiply each value by 2
.map((e) => e * 2)
//the reduce operator returns a new sequence that will
//emit the final value and immediately complete.
.reduce((n,e) => n + e, 0) 
//subscribe to the observable chain
.subscribe((e) => {
    console.log(e); //e = 2 + 4 + 6 + 8 + 10
}, (error) => {
    console.error(error); //handle errors
}, () => {
    console.log('done');
})

References[edit]

  1. ^ "ReactiveX - Intro". reactivex.io. Retrieved 2017-03-14.