RxJs 5 share() 操作符是如何工作的? [英] How does the RxJs 5 share() operator work?

查看:21
本文介绍了RxJs 5 share() 操作符是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是 100% 清楚 RxJs 5 share() 操作符是如何工作的,请看这里 最新文档.用于问题的 Jsbin 此处.

Its not 100% clear for me how the RxJs 5 share() operator works, see here the latest docs. Jsbin for the question here.

如果我创建一个 0 到 2 系列的 observable,每个值间隔一秒:

If I create an observable with a series of 0 to 2, each value separated by one second:

var source = Rx.Observable.interval(1000)
.take(5)
.do(function (x) {
    console.log('some side effect');
});

如果我为这个 observable 创建两个订阅者:

And if I create two subscribers to this observable:

source.subscribe((n) => console.log("subscriptor 1 = " + n));
source.subscribe((n) => console.log("subscriptor 2 = " + n));

我在控制台中得到这个:

I get this in the console:

"some side effect ..."
"subscriptor 1 = 0"
"some side effect ..."
"subscriptor 2 = 0"
"some side effect ..."
"subscriptor 1 = 1"
"some side effect ..."
"subscriptor 2 = 1"
"some side effect ..."
"subscriptor 1 = 2"
"some side effect ..."
"subscriptor 2 = 2"

我以为每个订阅都会订阅同一个 Observable,但似乎并非如此!就像订阅行为创建了一个完全独立的 Observable!

I thought each subscription would subscribe to the same Observable, but it does not seem to be the case! Its like the act of subscribing creates a completely separate Observable!

但是如果将 share() 操作符添加到源 observable 中:

But if the share() operator is added to the source observable:

var source = Rx.Observable.interval(1000)
.take(3)
.do(function (x) {
    console.log('some side effect ...');
})
.share();

然后我们得到这个:

"some side effect ..."
"subscriptor 1 = 0"
"subscriptor 2 = 0"
"some side effect ..."
"subscriptor 1 = 1"
"subscriptor 2 = 1"
"some side effect ..."
"subscriptor 1 = 2"
"subscriptor 2 = 2"

如果没有 share(),这是我所期望的.

Which is what I would expect without the share().

这是怎么回事,share() 操作符是如何工作的?每个订阅都会创建一个新的 Observable 链吗?

Whats going on here, how does the share()operator work ? Does each subscription create a new Observable chain?

推荐答案

小心你使用的是 RxJS v5,而你的文档链接似乎是 RxJS v4.我不记得具体细节,但我认为 share 运营商经历了一些变化,特别是在完成和重新订阅方面,但不要相信我的话.

Be careful that you are using RxJS v5 while your documentation link seem to be RxJS v4. I don't remember specifics but I think that the share operator went through some changes, in particular when it comes to completion and resubscription, but don't take my word for it.

回到您的问题,正如您在研究中所表明的,您的期望与图书馆设计不符.Observable 懒惰地实例化它们的数据流,具体地在订阅者订阅时启动数据流.当第二个订阅者订阅同一个 observable 时,另一个新的数据流就会开始,就好像它是第一个订阅者一样(所以是的,每个订阅都会创建一个新的 observable 链).这就是 RxJS 术语中创造的冷 observable,这是 RxJS observable 的默认行为.如果你想要一个 observable 在数据到达时将其数据发送给它拥有的订阅者,这被称为热 observable,获得热 observable 的一种方法是使用 share 运算符.

Back to your question, as you have shown in your study, your expectations do not correspond to the library design. Observables lazily instantiate their data flow, concretely initiating the dataflow when a subscriber subscribes. When a second subscriber subscribes to the same observable, another new dataflow is started as if it is was the first subscriber (so yes, each subscription creates a new chain of observables as you said). This is what is coined in RxJS terminology as a cold observable and that's the default behaviour for RxJS observable. If you want an observable which sends its data to the subscribers it has at the moment the data arrives, this is coined a hot observable, and one way to get a hot observable is to use the share operator.

您可以在这里找到图解订阅和数据流:Hot 和 Cold observables :是否有hot"和cold"操作符?(这对 RxJS v4 有效,但大部分对 v5 有效.

You can find illustrated subscription and data flows here : Hot and Cold observables : are there 'hot' and 'cold' operators? (this is valid for RxJS v4, but most of it is valid for v5).

这篇关于RxJs 5 share() 操作符是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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