Caches the last value emitted to give to new subscribers (without running any upstream pipe operators)
Manages all subscribers directly, without passing subscriptions up the stream.
This is very similar to shareReplay(1), except that once all subscribers unsubscribe this also unsubscribes from the upstream observable.
constsource = newBehaviorSubject(1000); constresult = source.pipe(expensiveComputation(), cache()); source.subscribe(); // expensiveComputation(1000) runs source.subscribe(); // the cached result is used source.next(2000); // expensiveComputation(2000) runs once, emitted to both subscribers
This is very similar to
shareReplay(1)
, except that once all subscribers unsubscribe this also unsubscribes from the upstream observable.