Reactive Extensions (RxJS)
While learning Angular 2, I discovered that Angular Team introduces Reactive Programming based on observables for asynchronous processing. This means you have to learn RxJS before jumping into Angular 2.
Letâs start explaining RxJS!Â
RxJS is a library for composing async and event-based programs using observable sequence, and you can query them using Array#extras such as filter, map, find etc.Â
RxJs is represent by the Observable pattern of Observable / Observer. The Observable will notify all the observers automatically of any state of changes as push-based.
RxJs does not aim at replacing existing async models such as promises and callbacks. However Observable provide key features like laziness and the ability to cancel subscription and compose events. RxJs will provide you with tools that canât be found in those models, and assist you better in async programming.
I will describe the different key types in RxJs.
Steam is a sequence of values in time, data is a stream of 4 sequence.
const data = [{"id": 1, "name": âGrapeQLâ}, {"id": 2, "name": âRelayâ}, {"id": 3, "name": âBacon.jsâ}, {"id": 4, "name": âd3.jsâ}];
Sequence can be understood as events that are triggered when the user interacts with the page, or data arriving from the server, like our data array.
Observable represent data source that can be observed, meaning it can send data to anyone who is interested in the data source. The way you do it, is by using subscribe method of the Observable to hand it an Observer object.
Hot (active) and cold (passive).
There is two types of Observable, hot and cold. The difference is that when you have default/cold Observable it doesnât emit new values - only if you subscribe to it using âsubscribeâ method! Whereas hot Observable start emitting new values even if there is no subscriber.
var observable = Rx.Observable.interval(1000) observable.subscribe({next: i => console.log('a: ' + i)}); setTimeout(function () { observable.subscribe({next: i => console.log(' b: ' + i)}); }, 4500);
var observable = Rx.Observable.interval(1000).publish(); observable.subscribe({next: i => console.log('a: ' + i)}); setTimeout(function () { Â observable.subscribe({next: i => console.log(' Â b: ' + i)}); }, 4500); observable.connect(); //start live streaming
Observer is the object that register an interest through subscription by using the subscribe method of Observable. Then the items are subsequently handed to the observer from the Observable.
Create Observer object and subscribe:
const observer = { Â next: x => console.log('Observer got a next value: ' + x), Â error: err => console.error('Observer got an error: ' + err), Â complete: () => console.log('Observer got a complete notification'), }; const subscription = observable.subscribe(observer);
Subject acts like a Observable and Observer, you can subscribe to it like Observable, and you can use it as a Observer to listen on a Observable.
var subject = new Subject(); subject.subscribe({ Â next: (v) => console.log('observer: ' + v) }); subject.next("Hello world");
Subscription can be used to unsubscribe from the Observable.
subscribe method will return an Subscription object whenever a subscription is made, and you can use that object to remove the current observer from listning to the Observable by calling unsubscribe method.
const subscription = observable.subscribe(observer); subscription.unsubscribe();
Angular 2âââIntroduction to new HTTP module
Reactive Programming in JavaScript With RxJS