冷热观测:是否存在“热"和“冷"运算符? [英] Hot and Cold observables: are there 'hot' and 'cold' operators?

查看:86
本文介绍了冷热观测:是否存在“热"和“冷"运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了以下SO问题: 什么是冷热观测值?

I reviewed the following SO question: What are the Hot and Cold observables?

总结:

    当一个可观测的冷观测器有一个观察者来使用它们时,它发出它的值,即,观察者接收到的值的顺序与订阅时间无关.所有观察者将使用相同的值序列.
  • 热的可观察对象发出的值与其订阅无关,即观察者收到的值是订阅时间的函数.
  • a cold observable emits its values when it has an observer to consume them, i.e. the sequence of values received by observers is independent of time of subscription. All observers will consume the same sequence of values.
  • a hot observable emits value independently of its subscriptions, i.e. the values received by observers are a function of the time of subscription.

但是,我仍然觉得热还是冷仍然是造成混乱的原因.所以这是我的问题:

Yet, I feel like hot vs. cold is still a source of confusion. So here are my questions:

  • 默认情况下,所有rx观测值是否都是冷的(对象除外)?

  • Are all rx observables cold by default (with the exception of subjects)?

我经常读到事件是热可观察物的典型隐喻,但我也读到Rx.fromEvent(input, 'click')是冷可观察物(?).

I often read that events are the typical metaphor for hot observables, but I also read that Rx.fromEvent(input, 'click') is a cold observable(?).

是否存在/哪些Rx运算符将冷的可观测对象转换为热的可观测对象(除了publishshare之外)?

Are there/what are the Rx operators which turn a cold observables into a hot observable (apart from publish, and share)?

例如,它如何与Rx运算符withLatestFrom一起使用?让cold$是一个冷的可观察到的东西,它已经在某处被订阅了. sth$.withLatestFrom(cold$,...)会成为热点吗?

For instance, how does it work with Rx operator withLatestFrom? Let cold$ be a cold observable which has somewhere been subscribed to. Will sth$.withLatestFrom(cold$,...) be a hot observable?

或者如果我执行sth1$.withLatestFrom(cold$,...), sth2$.withLatestFrom(cold$,...)并订阅sth1sth2,我是否总是看到两个sth相同的值?

Or if I do sth1$.withLatestFrom(cold$,...), sth2$.withLatestFrom(cold$,...) and subscribe to sth1 and sth2, will I always see the same value for both sth?

我以为Rx.fromEvent会创建冷的可观察物,但事实并非如此,如答案之一所述.但是,我仍然对此行为感到困惑: https://codepen.io/anon/pen/NqQMJR?editors = 101 .不同的订阅从相同的可观察值中获得不同的值. click事件不是共享的吗?

I thought Rx.fromEvent creates cold observables but that is not the case, as mentioned in one of the answers. However, I am still baffled by this behaviour: https://codepen.io/anon/pen/NqQMJR?editors=101. Different subscriptions get different values from the same observable. Wasn't the click event shared?

推荐答案

几个月后,我又回到了最初的问题,并希望与此同时分享所获得的知识. 我将使用以下代码作为说明支持( jsfiddle ):

I am coming back a few months later to my original question and wanted to share the gained knowledge in the meanwhile. I will use the following code as an explanation support (jsfiddle):

var ta_count = document.getElementById('ta_count');
var ta_result = document.getElementById('ta_result');
var threshold = 3;

function emits ( who, who_ ) {return function ( x ) {
  who.innerHTML = [who.innerHTML, who_ + " emits " + JSON.stringify(x)].join("\n");
};}

var messages$ = Rx.Observable.create(function (observer){
  var count= 0;
  setInterval(function(){
    observer.onNext(++count);
  }, 1000)
})
.do(emits(ta_count, 'count'))
.map(function(count){return count < threshold})
.do(emits(ta_result, 'result'))

messages$.subscribe(function(){});

如答案之一所述,定义一个可观察的对象会导致一系列回调和参数注册.数据流必须加入,并且可以通过subscribe函数完成. 此后可以找到(为简化起见)详细的流程.

As mentioned in one of the answers, defining an observable leads to a series of callback and parameter registration. The data flow has to be kicked in, and that is done via the subscribe function. A (simplified for illustration) detailed flow can be find thereafter.

可观察物默认是冷的.订阅可观察者将导致订阅的上游链发生.最后的订阅导致执行一个函数,该函数将处理源并将其数据发送给其观察者.

Observables are cold by default. Subscribing to an observable will result in an upstream chain of subscriptions taking place. The last subscription leads to the execution of a function which will handle a source and emit its data to its observer.

该观察者又向下一个观察者发出,导致下游数据流向下到接收器观察者.下图简化了两个订阅者订阅同一可观察对象的订阅和数据流.

That observer in its turn emits to the next observer, resulting in a downstream flow of data, down to the sink observer. The following simplified illustration shows subscription and data flows when two subscribers subscribe to the same observable.

可以通过使用主题或通过multicast运算符(及其派生类,请参见下面的注释3)来创建热可观测对象.

Hot observables can be created either by using a subject, or through the multicast operator (and its derivatives, see Note 3 below).

引擎盖下的multicast运算符利用对象并返回可观察的可连接对象.对操作员的所有订阅都将是对内部主题的订阅.调用connect时,内部主题订阅上游的可观察对象,并且数据向下游流动. 主题在内部操作一个订阅者观察者列表,并将传入的数据多播到所有订阅者观察者.

The multicast operator under the hood makes use of a subject and returns a connectable observable. All subscriptions to the operator will be subscriptions to the inner subject. When connect is called, the inner subject subscribes to the upstream observable, and data flows downstream. Subjects manipulate internally a list of subscribed observers and multicast incoming data to all subscribed observers.

下图总结了这种情况.

最后,更重要的是要了解由观察者模式和运算符的实现引起的数据流.

In the end, it matters more to understand the flow of data caused by the observer pattern and the implementation of the operators.

例如,如果obs很热,那么hotOrCold = obs.op1是冷还是热?不管答案是什么:

For instance, if obs is hot, is hotOrCold = obs.op1 cold or hot? Whatever the answer is :

  • 如果obs.op1没有订阅者,则没有数据将流过op1.如果有热门obs的订户,这意味着obs.op1可能会丢失数据
  • 假定op1不是类多播运算符,两次订阅hotOrCold将会订阅两次op1,并且obs中的每个值都将两次流过op1.
  • if there are no subscribers to obs.op1, no data will flow through op1. If there were subscribers to hot obs, that means obs.op1 will have possibly lost pieces of data
  • supposing that op1 is not a multicast-like operator, subscribing twice to hotOrCold will subscribe twice to op1, and every value from obs will flow twice through op1.

注意:

  1. 此信息对于Rxjs v4应该有效.虽然版本5消失了 经过相当大的更改,大部分内容仍保持逐字记录.
  2. 取消订阅,错误和完成流程未表示为 它们不在问题范围内.调度程序也不是 考虑在内.除其他因素外,它们会影响 数据流,但不是先验的方向和内容.
  3. 根据用于多播的主题类型,有 不同的派生多播运算符:
  1. This information should be valid for Rxjs v4. While the version 5 has gone through considerable changes, most of it still applies verbatim.
  2. Unsubscription, error and completion flows are not represented, as they are not in the scope of the question. Schedulers are also not taken into account. Among other things, they influence the timing of the data flow, but a priori not its direction and content.
  3. According to the type of subject used for multicasting, there are different derived multicasting operators:

Subject type | `Publish` Operator | `Share` operator ------------------ | --------------------------- | ----------------- Rx.Subject | Rx.Observable.publish | share Rx.BehaviorSubject | Rx.Observable.publishValue | shareValue Rx.AsyncSubject | Rx.Observable.publishLast | N/A Rx.ReplaySubject | Rx.Observable.replay | shareReplay

Subject type | `Publish` Operator | `Share` operator ------------------ | --------------------------- | ----------------- Rx.Subject | Rx.Observable.publish | share Rx.BehaviorSubject | Rx.Observable.publishValue | shareValue Rx.AsyncSubject | Rx.Observable.publishLast | N/A Rx.ReplaySubject | Rx.Observable.replay | shareReplay

更新:另请参见以下文章, ,并在那里)关于本·莱什(Ben Lesh)的问题.

Update : See also the following articles, here, and there) on that subject by Ben Lesh.

有关此主题的更多详细信息,可以在另一个SO问题中找到:不同RxJS主题的​​语义是什么?

Further details on subjects can be found in this other SO question : What are the semantics of different RxJS subjects?

这篇关于冷热观测:是否存在“热"和“冷"运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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