不同RxJS科目的语义是什么? [英] What are the semantics of different RxJS subjects?

查看:151
本文介绍了不同RxJS科目的语义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该主题的文档稀少,很难在那里发现入口点。

Documentation for the topic is sparse and it's hard to discover an "entry-point" there.

推荐答案

语义因主题类型而异。我将它们分为两种:香草( Rx.Subject )和特殊目的主题(其他三种)。由于专业化(例如,完成/重新连接行为),特殊目的主题分享了香草主题语义的一部分,但有一些警告。

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


  1. 主要特点

  1. Key features


  • 主题实现观察者,可观察的接口(和一次性接口,因为他们的原型上有 dispose 处理程序)。这意味着,除其他外,他们有:


    • 观察者界面: onNext onError onComplete method

    • observable interface: subscribe 方法

    • subjects implement the observer, observable interface (and the disposable interface as they have a dispose handler on their prototype). That means, among other things, they have:
      • observer interface : onNext, onError, onComplete method
      • observable interface : subscribe method

      I引用 Rxjs合同和语法的一个关键方面


      此语法允许可观察序列将任何数量(0或更多)的onNext消息发送到订阅的观察者实例,可选地后跟一个成功(onCompleted)或失败(onError)消息。

      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.




      • a vanilla subject(用 new Rx.Subject()创建)实现了这个语法:当 onCompleted 被调用一次时,所有随后对 onNext 的调用将被忽略。在同一个观察者上第二次调用 onCompleted 也会被忽略。如果观察者订阅了主题的可观察方,则会立即调用其 onComplete 回调( http://jsfiddle.net/cLf6Lqsn/1/ )。

        • a vanilla subject (created with new Rx.Subject()) implements that grammar : when onCompleted has been called once, all subsequent calls to onNext are ignored. Second call of onCompleted on the same observer is also ignored. If an observer subscribes to the observable side of the subject, its onComplete callback will immediately be called (http://jsfiddle.net/cLf6Lqsn/1/).


          1. 创作

          1. Creation


          • new Rx.Subject()

          • new Rx.Subject()

          返回将其观察者连接到其观察者的主题( jsfiddle )。此示例取自官方文档和描绘了如何使用主题作为代理。主题订阅了一个来源(观察者一方),并且也被观察者(可观察的一方)收听。对 onNext (观察者方)的任何调用都会导致可观察方调用 onNext ,并为每个观察者提供相同的值。

          Returns a subject which connects its observer to its 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.


          • Rx.Subject.create(观察者,可观察)

          • Rx.Subject.create(observer, observable)

          从指定的观察者和observable创建一个主题。这两者不一定相关。在 <的实现中可以看到一个很好的例子。 code> Rx.Dom.fromWebSocket ,它返回用于从套接字发送和接收数据的主题。主体的观察者侧将数据发送到套接字。可观察侧用于侦听来自套接字的传入数据。此外,以这种方式创建的主题没有 dispose 方法。

          Creates a subject from the specified observer and 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. 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.

          专门的Rx.Subject语义

          • This reactivex.io documentation covers pretty well most of the semantics of the specialized subjects.
          • The other interesting points to mention concern behavior past completion.
          • Sample code illustrating the behaviour are here for async, behavior, replay

          希望我没有太多错误。我很乐意得到纠正。最后请注意,这应该对RxJS v4有效。

          Hopefully I did not get too much wrong. I'll be happy to be corrected. Last note, this should be valid for 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?

          这篇关于不同RxJS科目的语义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆