RxJs 5 share()运算符如何工作? [英] How does the RxJs 5 share() operator work?

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

问题描述

对我来说,对于RxJs 5 share()运算符的工作原理并非100%清楚,请参阅最新文档。 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');
});

如果我为这个观察者创建了两个订阅者:

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()运算符添加到源可观察量中:

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()运算符是如何工作的?每个订阅是否都会创建一个新的Observable链?

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

推荐答案

当您的文档链接似乎是RxJS时,请注意您使用的是RxJS v5 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.

回到你的问题,正如你在研究中所展示的那样,你的期望与图书馆设计不符。 Observables懒惰地实例化他们的数据流,具体地在订阅者订阅时启动数据流。当第二个订阅者订阅相同的observable时,另一个新的数据流被启动,就像它是第一个订阅者一样(所以是的,每个订阅都会像你所说的那样创建一个新的可观察链)。这就是RxJS术语中创造的冷可观察性,这是RxJS可观察的默认行为。如果你想要一个observable将数据发送给它在数据到达时的订阅者,那么这就是一个热的可观察性,而获得热点可观察性的一种方法是使用 share operator。

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.

您可以在此处找到插图的订阅和数据流:热和冷可观测量:有热和冷运算符吗?(这对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天全站免登陆