RxJS 观察者和订阅者是一回事吗?
RxJS Is Observer and Subscriber the same thing?
从这个documentation:
RxJS introduces Observables, a new Push system for JavaScript. An Observable is a Producer of multiple values, "pushing" them to Observers (Consumers).
Subscribing to an Observable is analogous to calling a Function.
要调用 Observable,我们应该从 Observable 对象本身调用 subscribe()
函数,并将观察者作为由 observable 传递的数据的消费者传递,例如:
observable.subscribe( { /*this is an observer*/ } );
还有这个 documentation 说:
What is an Observer? An Observer is a consumer of values delivered by an Observable. Observers are simply a set of callbacks, one for each type of notification delivered by the Observable: next, error, and complete. The following is an example of a typical Observer object:
另一方面,第一份文件说:
The Observable constructor takes one argument: the subscribe function.
The following example creates an Observable to emit the string 'hi' every second to a subscriber.
import { Observable } from 'rxjs';
const observable = new Observable(function subscribe(subscriber) {
const id = setInterval(() => {
subscriber.next('hi')
}, 1000);
});
When calling observable.subscribe with an Observer, the function subscribe in new Observable(function subscribe(subscriber) {...}) is run for that given subscriber. Each call to observable.subscribe triggers its own independent setup for that given subscriber.
所以实体 Subscriber
只是在创建一个新的 Observable 时传递给订阅函数的参数?如果不是,订阅者是谁?
观察者和订阅者是同一个实体吗?如本文所述documentation
为什么调用 observable.subscribe({observer as call backs})
的代码不是可观察对象的订阅者?就像函数的 return 值的消费者是进行函数调用的代码。
观察者
const observer = {
next: v => /* code for next callback*/,
error: err => /* code for error callback*/,
complete: () => /* code for completion callback*/
}
订阅
const subscription = {
unsubscribe: () => /* code for unsubscribe callback */
}
可观察
const observable1 = from([1,2,3,4,5]);
const observable2 = of(1,2,3,4,5);
const observable3 = new Observable(observer => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.next(4);
observer.next(5);
observable.complete();
return { // return a subscription
unsubscribe: () => /* code for unsubscribe callback */
};
});
订阅并使用返回的订阅
// Store a subscription
const subscription = observable3.subscribe(observer);
// Invoke the unsubscribe callback defined by the observable.
subscription.unsubscribe();
好的。那什么是订户?
[Subscriber] Implements the Observer interface and extends the Subscription class. While the Observer is the public API for consuming the values of an Observable, all Observers get converted to a Subscriber... Subscriber is a common type in RxJS, and crucial for implementing operators, but it is rarely used as a public API.
观察者和订阅者是一回事吗?有点,是吗?取决于你问的具体程度。
考虑一下:
observable3.subscribe({
next: v => /* code for next callback */
});
obsevable3.subscribe(
v => /* code for next callback */
);
第一个是只定义了一个观察者 属性 的对象。第二个只是一个 lambda 函数。他们最终都产生了基本相同的订阅者。
从这个documentation:
RxJS introduces Observables, a new Push system for JavaScript. An Observable is a Producer of multiple values, "pushing" them to Observers (Consumers).
Subscribing to an Observable is analogous to calling a Function.
要调用 Observable,我们应该从 Observable 对象本身调用 subscribe()
函数,并将观察者作为由 observable 传递的数据的消费者传递,例如:
observable.subscribe( { /*this is an observer*/ } );
还有这个 documentation 说:
What is an Observer? An Observer is a consumer of values delivered by an Observable. Observers are simply a set of callbacks, one for each type of notification delivered by the Observable: next, error, and complete. The following is an example of a typical Observer object:
另一方面,第一份文件说:
The Observable constructor takes one argument: the subscribe function. The following example creates an Observable to emit the string 'hi' every second to a subscriber.
import { Observable } from 'rxjs';
const observable = new Observable(function subscribe(subscriber) {
const id = setInterval(() => {
subscriber.next('hi')
}, 1000);
});
When calling observable.subscribe with an Observer, the function subscribe in new Observable(function subscribe(subscriber) {...}) is run for that given subscriber. Each call to observable.subscribe triggers its own independent setup for that given subscriber.
所以实体 Subscriber
只是在创建一个新的 Observable 时传递给订阅函数的参数?如果不是,订阅者是谁?
观察者和订阅者是同一个实体吗?如本文所述documentation
为什么调用 observable.subscribe({observer as call backs})
的代码不是可观察对象的订阅者?就像函数的 return 值的消费者是进行函数调用的代码。
观察者
const observer = {
next: v => /* code for next callback*/,
error: err => /* code for error callback*/,
complete: () => /* code for completion callback*/
}
订阅
const subscription = {
unsubscribe: () => /* code for unsubscribe callback */
}
可观察
const observable1 = from([1,2,3,4,5]);
const observable2 = of(1,2,3,4,5);
const observable3 = new Observable(observer => {
observer.next(1);
observer.next(2);
observer.next(3);
observer.next(4);
observer.next(5);
observable.complete();
return { // return a subscription
unsubscribe: () => /* code for unsubscribe callback */
};
});
订阅并使用返回的订阅
// Store a subscription
const subscription = observable3.subscribe(observer);
// Invoke the unsubscribe callback defined by the observable.
subscription.unsubscribe();
好的。那什么是订户?
[Subscriber] Implements the Observer interface and extends the Subscription class. While the Observer is the public API for consuming the values of an Observable, all Observers get converted to a Subscriber... Subscriber is a common type in RxJS, and crucial for implementing operators, but it is rarely used as a public API.
观察者和订阅者是一回事吗?有点,是吗?取决于你问的具体程度。
考虑一下:
observable3.subscribe({
next: v => /* code for next callback */
});
obsevable3.subscribe(
v => /* code for next callback */
);
第一个是只定义了一个观察者 属性 的对象。第二个只是一个 lambda 函数。他们最终都产生了基本相同的订阅者。