【发布时间】:2021-08-08 14:20:51
【问题描述】:
以下代码按预期工作:
const source = interval(1000).pipe(
take(5),
share()
);
source.subscribe(x => console.log('c1', x));
setTimeout(() => {
source.subscribe(x => console.log('c2', x));
}, 2000);
产生以下输出: c1 0 c1 1 c1 2 c2 2 c1 3 c2 3 c1 4 c2 4
但是当我将第二个订阅更改为使用 delay(2000) 而不是 setTimeout() 我收到了另一个未共享的流。
const source = interval(1000).pipe(
take(5),
share()
);
source.subscribe(x => console.log('c1', x));
source.pipe(delay(2000)).subscribe(x => console.log('c2', x));
产生这个输出:
c1 0 c1 1 c1 2 c2 0 c1 3 c2 1 c1 4 c2 2 c2 3 c2 4
如何让第二个订阅者使用共享流? 我显然不完全了解 RX 操作员是如何在幕后工作的。
【问题讨论】:
标签: rxjs rxjs-observables