Rxjs基础

来源:互联网 发布:oracle查看数据库 编辑:程序博客网 时间:2024/04/27 16:51

Observable:对可调用的将来value或event集合的做法
Observer:是一个回调函数集合,知道怎么监听来自Observable发送的值
Subscription:代表一个Observable的执行,最基本用涂是取消执行
Subject:事件发出者,可以传递一个value或event到observer
Observables are lazy Push collections of multiple values.

Pull: comsumer decide when it recieves data from producer but producer unaware

Push:Producer determines when to send data to the Consumer.The Consumer is unaware of when it will receive that data.
An Observable is a Producer of multiple values, “pushing” them to Observers (Consumers).

iterator.next() is the Consumer, “pulling” out multiple values from the iterator (the Producer).

A Promise is a computation that may (or may not) eventually return a single value.
An Observable is a lazily evaluated computation that can synchronously or asynchronously return zero to (potentially) infinite values from the time it’s invoked onwards.
Subscribing to an Observable is analogous to calling a Function.
Observables are able to deliver values either synchronously or asynchronously.Observables can “return” multiple values over time

Observables are created using Rx.Observable.create or a creation operator, are subscribed to with an Observer, executeto deliver next / error / complete notifications to the Observer, and their execution may be disposed. Observables can be created with create, but usually we use the so-called creation operators, like of, from,interval, etc.

Each call to observable.subscribe triggers its own independent setup for that given Observer.Subscribing to an Observable is like calling a function, providing callbacks where the data will be delivered to.

There are three types of values an Observable Execution can deliver:
Next:sends a value
Error:sends a JavaScript Error or exception.
Complete:does not send a value.
Error and Complete notifications may happen only once during the Observable Execution, and there can only be either one of them.

With subscription.unsubscribe() you can cancel the ongoing execution

Each Observable must define how to dispose resources of that execution when we create the Observable using create(). You can do that by returning a custom unsubscribe function from within function subscribe().

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.To use the Observer, provide it to the subscribe of an Observable

箭头函数导致this总是指向函数定义生效时所在的对象,就是定义时所在的对象,而不是使用时所在的对象。箭头函数里面根本没有自己的this,而是引用外层的this。

Subscriptions can also be put together, so that a call to an unsubscribe() of one Subscription may unsubscribe multiple Subscriptions. You can do this by “adding” one subscription into another,eg:
subscription.add(otherSubscription);
subscription.unsubscribe();
Subscriptions also have a remove(otherSubscription) method, in order to undo the addition of a child Subscription.

subject makes any Observable execution be shared to multiple Observers.
var subject = new Rx.Subject();
Every Subject is an Observable.Given a Subject, you can subscribe to it, providing an Observer, which will start receiving values normally.
Every Subject is an Observer.It is an object with the methods next(v), error(e), and complete().

multicast returns an Observable that looks like a normal Observable, but works like a Subject when it comes to subscribing.multicast returns a ConnectableObservable, which is simply an Observable with the connect() method.
The connect() method is important to determine exactly when the shared Observable execution will start. Becauseconnect() does source.subscribe(subject) under the hood, connect() returns a Subscription, which you can unsubscribe from in order to cancel the shared Observable execution.

refCount makes the multicasted Observable automatically start executing when the first subscriber arrives, and stop executing when the last subscriber leaves.
The refCount() method only exists on ConnectableObservable, and it returns an Observable

BehaviorSubject, which has a notion of “the current value”. It stores the latest value emitted to its consumers, and whenever a new Observer subscribes, it will immediately receive the “current value” from theBehaviorSubject.

A ReplaySubject is similar to a BehaviorSubject in that it can send old values to new subscribers, but it can also recorda part of the Observable execution.A ReplaySubject records multiple values from the Observable execution and replays them to new subscribers.buffer values for new subscribers

The AsyncSubject is a variant where only the last value of the Observable execution is sent to its observers, and only when the execution completes.

Operators are methods on the Observable type.they do not change the existing Observable instance. Instead, they return a new Observable, whose subscription logic is based on the first Observable.An Operator is essentially a pure function which takes one Observable as input and generates another Observable as output.

Instance operators are functions that use the this keyword to infer what is the input Observable.
A static operator uses no this keyword internally, but instead relies entirely on its arguments.and usually are used to create Observables from scratch.

A scheduler controls when a subscription starts and when notifications are delivered.

0 0
原创粉丝点击