SubjectReactive X中可以看作是观察者和可观察序列的桥或代理。因为它本身就是一个观察者,可以订阅到一个或多个可观察序列,而同时也是一个可观察序列,可以向其观察者重复发送数据,也可以发现新数据。

因为Subject订阅到可观察序列,可观察序列发送数据时将会被触发(如果可观察序列是冷的,将等待订阅才会发送数据)。这样可以具有将热观察者序列转变为冷观察者序列的效果。

A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by reemitting them, and it can also emit new items.

Because a Subject subscribes to an Observable, it will trigger that Observable to begin emitting items (if that Observable is “cold” — that is, if it waits for a subscription before it begins to emit items). This can have the effect of making the resulting Subject a “hot” Observable variant of the original “cold” Observable.

更多查看

各种Subject

针对不同用例提供了四种Subject。在所有实现中不是都可用,有些实现使用其他命名规范(例如,在RxScala中,将这里的Subject命名为PublishSubject

There are four varieties of Subject that are designed for particular use cases. Not all of these are available in all implementations, and some implementations use other naming conventions (for example, in RxScala, what is called a “PublishSubject” here is known simply as a “Subject”):

AsyncSubject

Subject

AsyncSubject只发送源Observable发送的最后一个数据,而且是在源Observable结束的时候发送。如果源Observable没有发送任何数据,AsyncSubject也不会发送数据并结束。

An AsyncSubject emits the last value (and only the last value) emitted by the source Observable, and only after that source Observable completes. (If the source Observable does not emit any values, the AsyncSubject also completes without emitting any values.)

Subject

对后续的观察者也会发送同样的最后的数据。如果源Observable发生错误终止,AsyncSubject不会在发送任何数据,但会将源Observable发送的错误通知发送出去。

It will also emit this same final value to any subsequent observers. However, if the source Observable terminates with an error, the AsyncSubject will not emit any items, but will simply pass along the error notification from the source Observable.

更多见

BehaviorSubject

Subject

当观察者订阅BehaviorSubject,将会立即发送源Observable当前时刻的前一个发送的数据(如果源Observable没有发送过数据,则发送一个默认值),而后继续发送源Observable对象发送的数据。

When an observer subscribes to a BehaviorSubject, it begins by emitting the item most recently emitted by the source Observable (or a seed/default value if none has yet been emitted) and then continues to emit any other items emitted later by the source Observable(s).

Subject

如果源Observable发送错误终止,BehaviorSubject将不会在向观察者发送任何数据,但会将源Observable对象发送的错误通知发送出去。

However, if the source Observable terminates with an error, the BehaviorSubject will not emit any items to subsequent observers, but will simply pass along the error notification from the source Observable.

更多见

PublishSubject

Subject

PublishSubject只向观察者发送订阅后源Observable发送的数据项。

注意PublishSubject可能创建后立即发送数据项(除非采用措施避免这种情况),因此在Subject创建和观察者订阅期间,存在丢失数据项的风险。如果需要确保源Observable的数据项全部发送给观察者,需要使用Observable.Create方法手动引入一个与冷Observable同样的行为的Observable(检查所有观察者都进行了订阅才会发送数据项),或使用ReplaySubject。

PublishSubject emits to an observer only those items that are emitted by the source Observable(s) subsequent to the time of the subscription.

Note that a PublishSubject may begin emitting items immediately upon creation (unless you have taken steps to prevent this), and so there is a risk that one or more items may be lost between the time the Subject is created and the observer subscribes to it. If you need to guarantee delivery of all items from the source Observable, you’ll need either to form that Observable with Create so that you can manually reintroduce “cold” Observable behavior (checking to see that all observers have subscribed before beginning to emit items), or switch to using a ReplaySubject instead.

Subject

如果源Observable发送错误,PublishSubject将不会向观察者发送任何数据,而是将源Observable发送的错误通知发送出去。

If the source Observable terminates with an error, the PublishSubject will not emit any items to subsequent observers, but will simply pass along the error notification from the source Observable.

ReplaySubject

Subject

ReplaySubject向观察者发送源Observable发送的所有数据项,无论观察者何时进行订阅。

ReplaySubject的一个版本会在缓冲区大小达到阈值时丢弃最老的数据项,或丢弃超过生命周期的数据项。

如果将ReplaySubject当作观察者,注意不要在多线程中调用oNext方法(或其他on方法),这会引起并发调用(非顺序调用),违法Observable约定,Subject结果不明确,可能会最先接收到通知或发送的数据项。

ReplaySubject emits to any observer all of the items that were emitted by the source Observable(s), regardless of when the observer subscribes.

There are also versions of ReplaySubject that will throw away old items once the replay buffer threatens to grow beyond a certain size, or when a specified timespan has passed since the items were originally emitted.

If you use a ReplaySubject as an observer, take care not to call its onNext method (or its other on methods) from multiple threads, as this could lead to coincident (non-sequential) calls, which violates the Observable contract and creates an ambiguity in the resulting Subject as to which item or notification should be replayed first.

更多见

特定语言实现信息Language-Specific Information:

RxClojure

RxCpp

RxGroovy

RxJava 1․x

RxJS

RxKotlin

RxNET

RxPY

Rxrb

RxScala

 

相关文章: