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

查看:19
本文介绍了不同 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 处理程序).这意味着,除其他外,他们有:
    • 观察者接口:onNextonErroronComplete方法
    • observable 接口: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

      我引用了 的一个关键方面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.

      • 一个普通的主题(用 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. 创作

          • 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(observer, 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 语义

          希望我没有做错太多.我很乐意得到纠正.最后注意,这应该对 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.

          关于冷/热 observables 行为的详细解释,可以参考:Hot 和 Cold observables:是否有hot"和cold"操作符?

          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天全站免登陆