Reactive extensions

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

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

According to http://reactivex.io/, ReactiveX is an API for asynchronous programming with observable streams.

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

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:

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

  1. "ReactiveX - Intro". reactivex.io. Retrieved 2017-03-14.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.