简体   繁体   English

Rx JS将观察者订阅到多个Observable

[英]Rx JS Subscribe Observer to multiple Observables

Scratching the surface of Rx JS I ve ended up with the following snippet: 抓住Rx JS的表面我最后得到了以下片段:

    var observer1 = Rx.Observer.create(
         function (x) {
             console.log('Next: ' + x);
         },
         function (err) {
             console.log('Error: ' + err);
         },
         function () {
             console.log('Completed');
         }
     );  

     var observer2 = Rx.Observer.create(
         function (x) {
             console.log('Next: ' + x); 
         },  
         function (err) {
             console.log('Error: ' + err);   
         },  
         function () {
             console.log('Completed');   
         }   
     );  


     var source1 = Rx.Observable.return(1);
     var source2 = Rx.Observable.return(2);

     var subscription1 = source1.subscribe(observer1);
     var subscription2 = source2.subscribe(observer1);

OUTPUT: Next: 1 Completed 输出:下一个:1完成

JS BIN Code reference: http://goo.gl/DiHdWu JS BIN代码参考: http//goo.gl/DiHdWu

Subscribing the same observer to both streams only yields data from the first one. 将相同的观察者订阅到两个流仅产生来自第一个的数据。 However when subscribing the other observer things go as expected. 但是当订阅其他观察者时,事情会按预期进行。 Can someone please explain what is going on? 有人可以解释一下发生了什么吗?

     var subscription1 = source1.subscribe(observer1);
     var subscription2 = source2.subscribe(observer2);

OUTPUT: Next: 1 Completed Next: 2 Completed 输出:下一个:1完成下一个:2完成

Yes, an observer can listen to multiple Observables but not the way you are trying to use. 是的, 观察者可以收听多个Observable,但不能你想要使用的方式。 This can be achieved by using Merge , concat operators. 这可以通过使用Mergeconcat运算符来实现。 Code reference jsbin . 代码参考jsbin

Why your code didn't work? 为什么你的代码不起作用?

We get an IObserver for every call of Observer.create . 我们为Observer.create每次调用都得到一个IObserver It will ignore any future calls of OnNext once OnError or OnComplete is called. 一旦调用OnError或OnComplete,它将忽略OnNext的任何未来调用。

When we use one observer to subscribe multiple Observables, after the first observable terminates / completes (ie when it triggers OnError / OnCompleted) the observer will not work for any further subscribed observables. 当我们使用一个观察者来订阅多个Observable时,在第一个observable终止/完成后(即当它触发OnError / OnCompleted时),观察者将无法为任何进一步订阅的observable工作。 Because the termination message from the first observable will cause the observer to ignore messages from any further subscribed observables. 因为来自第一个observable的终止消息将导致观察者忽略来自任何进一步订阅的observable的消息。

For your problem to work, you need to use operators like merge , concat which will use multiple observers internally and do not pass completion messages(OnError/OnCompleted) from any observables except the last Observable to the outer observer. 为了使你的问题起作用,你需要使用像mergeconcat这样的运算符,它将在内部使用多个观察者,并且不会从除最后一个Observable之外的任何observable传递完成消息(OnError / OnCompleted)到外部观察者。

//Triggers observer1 for both observables(source1 & source2)
var subscription = source1.concat(source2).subscribe(observer1);

//Triggers observer2 for both observables(source1 & source2)
var subscription = source1.merge(source2).subscribe(observer2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM