Rxjs display operator evaluated for each subscriber

Why is the card operator evaluated for each subscriber, and not once?

const obs1 = Rx.Observable.interval(1000).take(1).map((x, i) => {
  console.log(i+1 + ':1 map')
  return 'obs1';
})

const obs2 = Rx.Observable.interval(1300).take(1).map((x, i) => {
  console.log(i+1 + ':2 map')
  return 'obs2';
})

const obs3 = Rx.Observable.interval(1700).take(2).map((x, i) => {
  console.log(i+1 + ':3 map')
  return 'obs3';
})

const view = obs1.combineLatest(obs2, obs3, (obs1, obs2, obs3) => {                 return obs1 + ', ' + obs2 + ', ' + obs3; });

// Every subscriber adds more calls to map - why is it called multiple     times at the same time ?

view.subscribe((value) => {
  console.log('sub1: ' + value)
});

view.subscribe((value) => {
  console.log('sub2: ' + value)
});

view.subscribe((value) => {
  console.log('sub3: ' + value)
});

I created a test file here: http://jsbin.com/jubinuf/3/edit?js,console

Can I write this test test differently to avoid this behavior?

+4
source share
2 answers

Kyle answered correctly. publish().refCount()applied to all three observables will result in the selector function mapnot being re-executed.

, Rxjs. obsX , "" . combineLatest 3, map . here . , .

+4

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


All Articles