Hot 和 Cold observables:是否有“hot"和“cold"运算符? [英] Hot and Cold observables: are there 'hot' and 'cold' operators?

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

问题描述

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

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

总结:

  • 当一个冷的 observable 有一个观察者来消费它们时,它会发出它的值,即观察者接收到的值的序列与订阅时间无关.所有观察者都将使用相同的值序列.
  • 热 observable 发出与其订阅无关的值,即观察者接收到的值是订阅时间的函数.

然而,我觉得热与冷仍然是混淆的根源.所以这是我的问题:

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

  • 默认情况下所有 rx observables 都是冷的吗(主体除外)?

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

我经常读到事件是热 observables 的典型比喻,但我也读到 Rx.fromEvent(input, 'click') 是一个冷 observable(?).

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 操作符可以将冷的 observable 转换为热的 observable(除了 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$ 成为一个冷的 observable,它在某个地方被订阅了.sth$.withLatestFrom(cold$,...) 会成为一个热门的 observable 吗?

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 会创建冷的 observables,但事实并非如此,如其中一个答案所述.但是,我仍然对这种行为感到困惑:https://codepen.io/anon/pen/NqQMJR?editors=101.不同的订阅从同一个 observable 获得不同的值.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("
");
};}

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(){});

正如其中一个答案中提到的,定义一个 observable 会导致一系列回调和参数注册.必须启动数据流,这是通过 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 默认是冷的.订阅 observable 将导致发生订阅的上游链.最后一个订阅导致执行一个函数,该函数将处理一个源并将其数据发送给它的观察者.

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.

该观察者依次向下一个观察者发送数据,从而产生下游数据流,直至下沉观察者.下面的简化图显示了当两个订阅者订阅同一个 observable 时的订阅和数据流.

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)来创建热 observable.

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,并且每个值都来自 hotOrColdcode>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. 根据用于多播的主题类型,有不同的派生多播操作符:

<代码>学科类型 |`发布`运营商|`共享`运算符------------------ |--------------------------- |-----------------接收主题 |Rx.Observable.publish |分享Rx.BehaviorSubject |Rx.Observable.publishValue |份额价值Rx.AsyncSubject |Rx.Observable.publishLast |不适用Rx.ReplaySubject |Rx.Observable.replay |分享重播

更新:另见以下文章,这里,还有a>) 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?

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

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