import { interval } from 'rxjs';
import { tap, publish, refCount } from 'rxjs/operators';
// Turn the interval observable into a ConnectableObservable (hot)
const refCountInterval = interval(400).pipe(
tap((num) => console.log(`refCount ${num}`)),
publish(),
refCount()
);
const publishedInterval = interval(400).pipe(
tap((num) => console.log(`publish ${num}`)),
publish()
);
refCountInterval.subscribe();
refCountInterval.subscribe();
// 'refCount 0' -----> 'refCount 1' -----> etc
// All subscriptions will receive the same value and the tap (and
// every other operator) before the publish operator will be executed
// only once per event independently of the number of subscriptions.
publishedInterval.subscribe();
// Nothing happens until you call .connect() on the observable.
更多建議: