Recommended Pattern to Cache Streams
Learn about the patterns recommended for caching streams.
We'll cover the following
The share
operator
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
. The share
operator rules them all, meaning that it’s highly recommended to use the share
operator instead of Connectable
and shareReplay
in most cases. The shareReplay
operator is too popular to deprecate but might be deprecated in future versions since there’s 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 and define your reset behavior. This is how we can achieve the behavior of the shareReplay
operator using the share
operator:
Get hands-on with 1200+ tech skills courses.