javascript - RxJS combineLatest operator weird behaviour -


i have query regarding rxjs combinelatest operator. have modified example given in

https://www.learnrxjs.io/operators/combination/combinelatest.html

as follows:

//timerone emits first value @ 1s, once every 4s const timerone = rx.observable.timer(1000, 4000); //timertwo emits first value @ 2s, once every 4s const timertwo = rx.observable.timer(2000, 4000) //timerthree emits first value @ 3s, once every 4s const timerthree = rx.observable.of(false);  //when 1 timer emits, emit latest values each timer array const combined = rx.observable .combinelatest(     timerone,     timertwo,     timerthree );  const subscribe = combined.subscribe(latestvalues => {     //grab latest emitted values timers one, two, , 3     const [timervalone, timervaltwo, timervalthree] = latestvalues;     if(latestvalues[0] === 3) {         this.timerthree = rx.observable.of(true);   }    console.log(     `timer 1 latest: ${timervalone},       timer 2 latest: ${timervaltwo},       timer 3 latest: ${timervalthree}`    ); }); 

i expect value of timerthree change true bit keep on printing false shown in output snippet:

"timer 1 latest: 3,   timer 2 latest: 2,   timer 3 latest: false" "timer 1 latest: 3,   timer 2 latest: 3,   timer 3 latest: false" "timer 1 latest: 4,   timer 2 latest: 3,   timer 3 latest: false" 

any idea why happening? there way fix this? thanks

the important thing aware of here timerthree isn't observable in itself, rather reference observable object. when use combinelatest, combining object, not variable that's referencing it. when assign timerthree new observable, it's pointing new object, combined still using old one.

if want able change value of timerthree, try using subject instead. can push new values timerthree.next.


Comments