Cold and Hot Observables:
When the data is produced by the Observable itself, we call it a cold Observable. For example, observables created using the of, from, range, interval and timer operators will be cold.
import * as Rx from "rxjs";
const random = Math.random()
const observable = Rx.Observable.create((observer) => {
observer.next(random);
});
// subscription 1
observable.subscribe((data) => {
console.log(data); // 0.11208711666917925 (random number)
});
// subscription 2
observable.subscribe((data) => {
console.log(data); // 0.11208711666917925 (random number)
});
Hot observables, on other hand, have their data producer outside the observable itself. These observables are closing over external data producers. For example, observables created with the fromEvent operator for either user events (clicks, mouse moves,…) or WebSocket events are hot observables. The data is being produced regardless of if there’s a subscriber or not. If there’s no subscriber when the data is being produced, the data is simply lost.
import * as Rx from "rxjs";
const observable = Rx.Observable.fromEvent(document, 'click');
// subscription 1
observable.subscribe((event) => {
console.log(event.clientX); // x position of click
});
// subscription 2
observable.subscribe((event) => {
console.log(event.clientY); // y position of click
});
Arrow Functions
Arrow functions allows a short syntax for writing function expressions.
You don't need the
function keyword, the return keyword, and the curly brackets.
Let and Const are blocked scope variables in ES6.
No comments:
Post a Comment