RxJS combinationLatest: how to get emit after changing only one value?

I am trying to learn the RxJS library. One of the cases that I don’t quite understand is described in this jsfiddle (the code is also below).

var A= new Rx.Subject(); var B= new Rx.Subject(); A.onNext(0); // '.combineLatest' needs all the dependency Observables to get emitted, before its combined signal is emitted. // // How to have a combined signal emitted when any of the dependencies change (using earlier given values for the rest)? // A.combineLatest( B, function (a,b) { return a+b; } ) .subscribe( function (v) { console.log( "AB: "+ v ); } ); B.onNext("a"); A.onNext(1); 

I would like to get two sounds in AB magazine. One of B changes to "a" (A already has a value of 0). The other is from change A to 1.

However, only the changes that occur after signing seem important (even if A matters, and therefore the combined result can be calculated).

Should I use hot observables for this or some other method than .combineLatest ?

My problem in the actual code (bigger than this sample) is that after the subscription I need to do separate initializations, which cuts the material in two separate places, instead of the original values ​​being clearly ahead.

thanks

+5
source share
1 answer

I think you misunderstood how Subjects works. Subjects are hot watchers. They do not hold on to values, so if they get onNext without subscribers, that value will be lost to the world.

What you're looking for is either a BehaviorSubject or a ReplaySubject , both of which preserve past values, which are retransmitted to new subscribers. In the first case, you always build it with an initial value

 //All subscribers will receive 0 var subject = new Rx.BehaviorSubject(0); //All subscribers will receive 1 //Including all future subscribers subject.onNext(1); 

in the last you set the number of values ​​that will be played for each subscription

 var subject = new Rx.ReplaySubject(1); //All new subscribers will receive 0 until the subject receives its //next onNext call subject.onNext(0); 

Rewriting your example, it could be:

 var A= new Rx.BehaviorSubject(0); var B= new Rx.Subject(); // '.combineLatest' needs all the dependency Observables to get emitted, before its combined signal is emitted. // // How to have a combined signal emitted when any of the dependencies change (using earlier given values for the rest)? // A.combineLatest( B, function (a,b) { return a+b; } ) .subscribe( function (v) { console.log( "AB: "+ v ); } ); B.onNext("a"); A.onNext(1); //AB: 0a //AB: 1a 

In another note, understanding, of course, that this is not new to you at all, in most cases you do not need to use Subject directly, since this usually means that you are trying to argue Rx with the security of your well-known paradigms, you should ask yourself where your data? How is it created? If you ask these questions enough, following the chain of events returning to the source, 9 out of 10 times you will find that there may be an Observable wrapper for it.

+9
source

Source: https://habr.com/ru/post/1232414/


All Articles