shareReplay({ refCount: true })
has different behavior with share()
#6731
-
Describe the bugHello, 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: A, B, C, D, E
setTimeout(() => shared$.subscribe(b => console.log('b: ', b), 6000);
// (after ~6s, all at once)b: 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. Expected behavior
Reproduction codeNo response Reproduction URLhttps://stackblitz.com/edit/azerz8?devtoolsheight=50 Version7.1.0 EnvironmentNo response Additional contextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I think it's as per design. Note that the medium article says how there's no need for The docs for
And this explains why if you add in a
If you replace it with share() I don't think it will work as you wanted, right?
I commented out the configs that by default are true (they already have the value you are looking for, so you can just omit them). On the other hand,
|
Beta Was this translation helpful? Give feedback.
-
Thank you very much. It really solves my problem. I think I need to be more careful when I use |
Beta Was this translation helpful? Give feedback.
-
I moved this into discussions and appreciate @voliva for the detailed explanations. |
Beta Was this translation helpful? Give feedback.
I think it's as per design. Note that the medium article says how there's no need for
shareReplay
anymore: i.e. all/most of the overloads with shareReplay you should be able to do with share. But doesn't say thatshareReplay({ refCount: true })
would have to behave the same way as share.The docs for
shareReplay
say:And this explains why if you add in a
take(3)
to the first subscription it works as y…