简体   繁体   English

不同RxJS科目的语义是什么?

[英]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语义

  1. Key features 主要特点

    • subjects implement the observer, observable interface (and the disposable interface as they have a dispose handler on their prototype). 主体实现观察者,可观察的界面(和一次性界面,因为他们在他们的原型上有一个dispose处理程序)。 That means, among other things, they have: 这意味着,除其他外,他们有:
      • observer interface : onNext , onError , onComplete method observer接口: onNextonErroronComplete方法
      • observable interface : subscribe method 可观察的接口: subscribe方法
    • you can cast a subject to an observer or to an observable, hiding the implementation of the extra interfaces (cf. .asObserver() , and .asObservable() ) if need be 您可以将主题转换为观察者或观察者,隐藏额外接口的实现(参见.asObserver().asObservable() ),如果需要的话
    • the subject being an observable, you can subscribe several observers to it. 作为一个可观察的主题,你可以订阅几个观察者。 That observable will then broadcast its data to all its observers. 然后,该观察者将向其所有观察员广播其数据。 Internally the subject maintains an array of observers. 在内部,主体维持着一系列观察者。
    • the subject being an observer, you can subscribe it to any observable 作为观察者的主体,您可以将其订阅给任何可观察者
    • the observer and the observable which compose the subject being two distinct entities, you can use them independently of the other if that's your use case. 构成主体的观察者和观察者是两个不同的实体,如果这是你的用例,你可以独立使用它们。
    • dispose ing a subject will unsubscribe all observers and release resources. dispose主题将取消订阅所有观察者并释放资源。
    • Subjects do not take a scheduler but rather assume that all serialization and grammatical correctness are handled by the caller of the subject. 主题不采用调度程序,而是假设所有序列化和语法正确性由主题的调用者处理。
    • The default behaviour of subjects is to emit synchronously their values to the observers, starting with the first subscribed observer to the last . 主体的默认行为是将它们的值同步地发送给观察者, 从第一个订阅的观察者开始到最后一个 In most cases, order will not matter, in others it will. 在大多数情况下,订单无关紧要,在其他情况下也会如此。

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/ )。

    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语义

  • This reactivex.io documentation covers pretty well most of the semantics of the specialized subjects. 这个reactivex.io文档很好地涵盖了专业主题的大部分语义。
  • The other interesting points to mention concern behavior past completion. 其他有趣的观点提到关注过去完成的行为。
  • Sample code illustrating the behaviour are here for async , behavior , replay 示例行为的示例代码用于异步行为重放

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM