Использование share() RxJS

Пример с сайта, который работает:

//emit value in 1s
const source = timer(1000);
//log side effect, emit result
const example = source.pipe(
  tap(() => console.log('***SIDE EFFECT***')),
  mapTo('***RESULT***')
);

/*
  ***NOT SHARED, SIDE EFFECT WILL BE EXECUTED TWICE***
  output:
  "***SIDE EFFECT***"
  "***RESULT***"
  "***SIDE EFFECT***"
  "***RESULT***"
*/
const subscribe = example.subscribe(val => console.log(val));
const subscribeTwo = example.subscribe(val => console.log(val));

//share observable among subscribers
const sharedExample = example.pipe(share());
/*
  ***SHARED, SIDE EFFECT EXECUTED ONCE***
  output:
  "***SIDE EFFECT***"
  "***RESULT***"
  "***RESULT***"
*/
const subscribeThree = sharedExample.subscribe(val => console.log(val));
const subscribeFour = sharedExample.subscribe(val => console.log(val));

Мой НЕ рабочий код...почему не так де работает?

function getValue() {
  return of({
    data: [{ id: 1, item: 'i1' }, { id: 2, item: 'i2' }, { id: 3, item: 'i3' }]
  });
}

const value = getValue().pipe(
  tap(() => console.log('***SIDE EFFECT***')),
  map(m => m.data)
);

const subscribe = value.subscribe(val => console.log(val));
const subscribeTwo = value.subscribe(val => console.log(val));

const sharedvalue = value.pipe(share());

const subscribe2 = sharedvalue.subscribe(val => console.log(val));
const subscribeTwo2 = sharedvalue.subscribe(val => console.log(val));

Результат:

введите сюда описание изображения


Ответы (0 шт):