why the 'refcount: true' of `ShareReplay` doesn't work like `share` when source completes

Viewed 176

I tried the example given by 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

I thought 'b' subscription would output 'A, B .... E' again. Because 'a' sub has completed in 5000ms, refCount should become 0. And then shared$ disconnects to the source. So when b subscribes shared$, the source would restart. But the console showed only 'C, D, E' replayed by shareReplay.

If I replace shareReplay({refCount: true}) with share() or add take(5) to 'a', b will ouput as I expect. What is the root cause? You can play with my code: https://stackblitz.com/edit/azerz8?devtoolsheight=50

1 Answers
Related