【发布时间】:2021-12-15 07:55:56
【问题描述】:
我试过https://medium.com/volosoft/whats-new-in-rxjs-7-a11cc564c6c0给出的例子
import { interval, of, zip } from "rxjs";
import { map, shareReplay } from "rxjs/operators";
const shared$ = zip(interval(1000), of("A", "B", "C", "D", "E")).pipe(
map(([, char]) => char),
shareReplay({ refCount: true, bufferSize: 3 })
);
shared$.subscribe(a => console.log('a: ', a);
// (~1s apart) A, B, C, D, E
setTimeout(() => shared$.subscribe(b => console.log('b: ', b), 6000);
// (after ~6s, all at once) C, D, E
我认为 'b' 订阅会再次输出 'A, B .... E'。因为 'a' sub 已在 5000 毫秒内完成,所以 refCount 应该变为 0。然后 shared$ 断开与源的连接。因此,当 b 订阅 shared$ 时,源将重新启动。但控制台只显示由 shareReplay 重播的“C、D、E”。
如果我用share() 替换shareReplay({refCount: true}) 或将take(5) 添加到'a',b 将按我的预期输出。根本原因是什么?
你可以玩我的代码:https://stackblitz.com/edit/azerz8?devtoolsheight=50
【问题讨论】:
标签: rxjs