Learn RxJS (02) : Introduces RxJS and Observables
The last article gave you a general explanation of what is responsive programming, this article we will understand RxJS, RxJS is a specific implementation of responsive programming; There are other libraries that also implement responsive programming, such as Cyc.js, bacon.js, most. Why should we study RxJS and not those other libraries? The main reasons are as follows:
- Conform to the standard:
RxJS
The implementation is currentObservables proposal(Some reactive programming features that may be implemented later in a new version of JavaScript) - popular:
RxJS
isRx
JavaScript version of the implementation, but also some other language implementation; Such as:RxJava.RxSwiftAnd so on; These libraries have the same API, so if you learn how to use one library, you learn how to use many different languages at the same time. For front-end developers, if you want to learn responsive programming; Choose to useRxJS
Would be a good choice. Just to make it intuitive,RxJS
In the makingstar
There are far more libraries than there are for other JavaScript implementations of responsive programming, and there are now three dominant front-end frameworksAngular
.React
.Vue
Have withRxJS
A combination of whichAngular
The deepest combination of (Angular – The RxJS library), so learnAngular
Developers, it is more necessary to putRxJS
Learn it well. So let’s go inRxJS
Study.RxJS
There is one core and three focuses, and one core refers toObservable
The three key points areObserver
.Subject
.Scheduler
; I’m going to try all of these things out with you step by step. Let’s start withObservable
Start learning. In the introductionObservable
Let’s get familiar with two concepts: there are two roles in the process of data from production to usage; One is the producer of data (Producer
), the other is the consumer of data (Consumer
); There needs to be a way of “communicating” between consumers and producers so that data can be passed from producer to consumer. Now there are two ways: - Pull: This means that
Consumer
Directly from theProducer
There to get the data; In this way,Consumer
It’s active. It decides when to goProducer
Where you get the data,Producer
I don’t knowConsumer
When will you come to pick up the data? - Push: This means that
Producer
Take the initiative to pass data toConsumer
In this way,Producer
It’s active. When does he decide to send the data toConsumer
.Consumer
Don’t knowProducer
When to pass the data to yourself.PullandPushThese are the two concepts we want to understand, and we can represent them in the following table:
Being a Producer of data | A Consumer of data | |
---|---|---|
Pull | passive: Data is produced when consumers request it | active: Decide when you need data |
Push | active: Produce data at your own pace | passive: Receives data and reacts |
In JavaScript, the Pull types are Function and Iterator, where Function can return only one result at a time, while Generator(which complies with both the iterable and Iterator protocols) can return multiple results at a time. There are promises of Push type. Promises can return one result asynchronously, but not multiple results asynchronously. So in RxJS, there is an Observable that can return multiple results synchronously or asynchronously, which is equivalent to adding a condition that can return multiple values in Push mode. We can also use the following table to represent the above:
SINGLE(returns a SINGLE value) | MULTIPLE(return MULTIPLE values) | |
---|---|---|
Pull | Function | Iterator |
Push | Promise | Observable |
One more thing to note: Functions, iterators, and Observables are all deferred, that is, functions only perform operations when called and iterators only perform operations when iterated. An Observable only performs an operation when it’s subscribed. Let’s start by creating an Observable and subscribing to it; The code is as follows:
import { Observable } from "rxjs";
const observable = Observable.create(observer => {
observer.next(1);
observer.next(2);
setTimeout(() => {
observer.next(3);
}, 100);
});
console.log("before subscribe");
const subscription = observable.subscribe(val => {
console.log(`val: ${val}`);
});
console.log("after subscribe");Copy the code
The console output is as follows:
before subscribe
val: 1
val: 2
after subscribe
val: 3Copy the code
From the console results, we can see that the Observable we created synchronously sends 1 and 2 after subscribing, and then sends 3 after 100ms. And we added extra code before and after the Observable subscription to see how the Observable sends its values. Observables can send values asynchronously as well as synchronously. This article gives a brief introduction to RxJS and Observables. In the next article, we will take a closer look at Observables.
Reference: Observable
Copyright Notice: Share – Keep attribution – Non-commercial use – No deduction