refCount() and share()
The refCount() operator on ConnectableObservable is similar to autoConnect(1), which fires after getting one subscription, with one important difference: after all its observers have been disposed of, it disposes of itself and starts over when a new subscription comes in. It does not persist the subscription to the source when it has no subscribers. When another subscription happens, it essentially starts over.
Look at the following example: we have Observable.interval() emitting every second, and it multicasts with refCount(). Observer 1 takes five emissions, and Observer 2 takes two emissions. We stagger their subscriptions with our sleep() function to put a three-second pause between subscriptions. Because these ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access