[6a3a178] | 1 | import { MonoTypeOperatorFunction } from '../types';
|
---|
| 2 | /**
|
---|
| 3 | * Make a {@link ConnectableObservable} behave like a ordinary observable and automates the way
|
---|
| 4 | * you can connect to it.
|
---|
| 5 | *
|
---|
| 6 | * Internally it counts the subscriptions to the observable and subscribes (only once) to the source if
|
---|
| 7 | * the number of subscriptions is larger than 0. If the number of subscriptions is smaller than 1, it
|
---|
| 8 | * unsubscribes from the source. This way you can make sure that everything before the *published*
|
---|
| 9 | * refCount has only a single subscription independently of the number of subscribers to the target
|
---|
| 10 | * observable.
|
---|
| 11 | *
|
---|
| 12 | * Note that using the {@link share} operator is exactly the same as using the *publish* operator
|
---|
| 13 | * (making the observable hot) and the *refCount* operator in a sequence.
|
---|
| 14 | *
|
---|
| 15 | * ![](refCount.png)
|
---|
| 16 | *
|
---|
| 17 | * ## Example
|
---|
| 18 | *
|
---|
| 19 | * In the following example there are two intervals turned into connectable observables
|
---|
| 20 | * by using the *publish* operator. The first one uses the *refCount* operator, the
|
---|
| 21 | * second one does not use it. You will notice that a connectable observable does nothing
|
---|
| 22 | * until you call its connect function.
|
---|
| 23 | *
|
---|
| 24 | * ```ts
|
---|
| 25 | * import { interval } from 'rxjs';
|
---|
| 26 | * import { tap, publish, refCount } from 'rxjs/operators';
|
---|
| 27 | *
|
---|
| 28 | * // Turn the interval observable into a ConnectableObservable (hot)
|
---|
| 29 | * const refCountInterval = interval(400).pipe(
|
---|
| 30 | * tap((num) => console.log(`refCount ${num}`)),
|
---|
| 31 | * publish(),
|
---|
| 32 | * refCount()
|
---|
| 33 | * );
|
---|
| 34 | *
|
---|
| 35 | * const publishedInterval = interval(400).pipe(
|
---|
| 36 | * tap((num) => console.log(`publish ${num}`)),
|
---|
| 37 | * publish()
|
---|
| 38 | * );
|
---|
| 39 | *
|
---|
| 40 | * refCountInterval.subscribe();
|
---|
| 41 | * refCountInterval.subscribe();
|
---|
| 42 | * // 'refCount 0' -----> 'refCount 1' -----> etc
|
---|
| 43 | * // All subscriptions will receive the same value and the tap (and
|
---|
| 44 | * // every other operator) before the publish operator will be executed
|
---|
| 45 | * // only once per event independently of the number of subscriptions.
|
---|
| 46 | *
|
---|
| 47 | * publishedInterval.subscribe();
|
---|
| 48 | * // Nothing happens until you call .connect() on the observable.
|
---|
| 49 | * ```
|
---|
| 50 | *
|
---|
| 51 | * @see {@link ConnectableObservable}
|
---|
| 52 | * @see {@link share}
|
---|
| 53 | * @see {@link publish}
|
---|
| 54 | */
|
---|
| 55 | export declare function refCount<T>(): MonoTypeOperatorFunction<T>;
|
---|