Exploring the RxJS 7 recommended pattern to cache streams
A lot of work was done in version 7 to consolidate multicasting operators. The multicast
, publish
, publishReplay
, publishLast
, and refCount
operators were deprecated and will be removed in RxJS 8.
The only operators remaining are shareReplay
, share
, and connectable
. And the share
operator rules them all, meaning that it is highly recommended to use the share
operator instead of Connectable
and shareReplay
in most cases. The shareReplay
operator is too popular to deprecate but may be deprecated in future versions as there is an alternative to it, especially because shareReplay
, when not used carefully, can cause memory leaks, particularly with infinite streams.
The share
operator is enhanced in version 7 with an optional configuration object as an argument, which makes it more flexible and ready to do the job of other operators.
In this share
configuration object, you can choose the subject you're connecting through...