BACK
Referenceshare
Reference · 37

share

Turns a cold Observable into a hot, multi-subscribable one.

Signature

share(config?: ShareConfig): MonoTypeOperatorFunction

What it does

Under the hood: a Subject + refCount. The first subscriber triggers the subscription to the source; later subscribers get the same stream without restarting the source. When the subscriber count hits zero, it unsubscribes from the source. The config object lets you tune what happens on reset/error/complete.

When to use

One HTTP request for several components, a shared event stream (a WebSocket, for example), any heavy computation you don't want to repeat per subscriber.

Gotcha

share() doesn't cache values — late subscribers don't get what was already emitted. If you need a "replay", use shareReplay.

See also

shareReplay, multicast, Subject

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...