[英]What are the semantics of different RxJS subjects?
该主题的文档很少,很难在那里发现“入口点”。
Semantics differ according to the type of subjects. 语义根据主题的类型而不同。 I will divide them in two kinds : vanilla (
Rx.Subject
), and special-purpose subjects (the other three). 我将它们分为两种:香草(
Rx.Subject
)和特殊目的主题(其他三种)。 The special-purpose subjects share part of the semantics of the vanilla subject with a few caveats due to their specialization (for instance, completion/reconnection behaviour). 由于专业化(例如,完成/重新连接行为),特殊目的主题与香草主题的语义共享一些警告。
Vanilla Rx.Subject semantics Vanilla Rx.Subject语义
Key features 主要特点
dispose
handler on their prototype). dispose
处理程序)。 That means, among other things, they have: onNext
, onError
, onComplete
method onNext
, onError
, onComplete
方法 subscribe
method subscribe
方法 .asObserver()
, and .asObservable()
) if need be .asObserver()
和.asObservable()
),如果需要的话 dispose
ing a subject will unsubscribe all observers and release resources. dispose
主题将取消订阅所有观察者并释放资源。 I quote a key aspect of Rxjs contract and grammar : 我引用了Rxjs合同和语法的一个关键方面:
This grammar allows observable sequences to send any amount (0 or more) of onNext messages to the subscribed observer instance, optionally followed by a single success (onCompleted) or failure (onError) message.
该语法允许可观察序列将任何数量(0或更多)的onNext消息发送到订阅的观察者实例,可选地随后是单个成功(onCompleted)或失败(onError)消息。
a vanilla subject (created with new Rx.Subject()
) implements that grammar : when onCompleted
has been called once, all subsequent calls to onNext
are ignored. 一个vanilla主题(用
new Rx.Subject()
创建)实现了这个语法:当onCompleted
被调用一次后,对onNext
所有后续调用都将被忽略。 Second call of onCompleted
on the same observer is also ignored. onCompleted
对同一观察者的第二次调用也被忽略了。 If an observer subscribes to the observable side of the subject, its onComplete
callback will immediately be called ( http://jsfiddle.net/cLf6Lqsn/1/ ). 如果观察者订阅了主题的可观察方,则会立即调用其
onComplete
回调( http://jsfiddle.net/cLf6Lqsn/1/ )。
Creation 创建
new Rx.Subject()
Returns a subject which connects its observer to its observable ( jsfiddle ). 返回将其观察者连接到其observable( jsfiddle )的主题。 This example is taken from the official documentation and portrays how to use subjects as proxies.
此示例取自官方文档,并描述了如何将主题用作代理。 The subject is subscribed to a source (observer side), and is also listened on by observers (observable side).
主题订阅了一个来源(观察者一方),并且也被观察者(可观察的一方)收听。 Any call to
onNext
(observer side) results in the observable side calling onNext
with the same value for each of its observers. 对
onNext
(观察者端)的任何调用onNext
导致可观察的一方调用onNext
,并为每个观察者调用相同的值。
Rx.Subject.create(observer, observable)
Creates a subject from the specified observer and observable. 从指定的观察者和observable创建主题。 Those two are not necessarily connected.
这两者不一定相关。 A good example can be seen in the implementation of
Rx.Dom.fromWebSocket
which returns a subject used to send and receive data from a socket. 在
Rx.Dom.fromWebSocket
的实现中可以看到一个很好的例子,它返回一个用于从套接字发送和接收数据的主题。 The observer side of the subject sends data to the socket. 主体的观察者侧将数据发送到套接字。 The observable side is used to listen on incoming data from the socket.
可观察侧用于侦听来自套接字的传入数据。 Also, a subject created this way does NOT have a
dispose
method. 此外,以这种方式创建的主题没有
dispose
方法。
Specialized Rx.Subject semantics 专门的Rx.Subject语义
reactivex.io
documentation covers pretty well most of the semantics of the specialized subjects. reactivex.io
文档很好地涵盖了专业主题的大部分语义。 Hopefully I did not get too much wrong. 希望我没有太多错误。 I'll be happy to be corrected.
我很乐意得到纠正。 Last note, this should be valid for RxJS v4.
最后请注意,这应该对RxJS v4有效。
For a detailed explanation of the behaviour of cold/hot observables, one can refer to : Hot and Cold observables : are there 'hot' and 'cold' operators? 有关冷/热可观测量行为的详细解释,可以参考: 热和冷可观测量:是否存在“热”和“冷”运算符?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.