Debian Codegraph @

node/rxjs/src/internal/operators/refCount.ts

raw
1 import { ConnectableObservable } from '../observable/ConnectableObservable';
2 import { Subscription } from '../Subscription';
3 import { MonoTypeOperatorFunction } from '../types';
4 import { operate } from '../util/lift';
5 import { createOperatorSubscriber } from './OperatorSubscriber';
6
7 /**
8 * Make a {@link ConnectableObservable} behave like a ordinary observable and automates the way
9 * you can connect to it.
10 *
11 * Internally it counts the subscriptions to the observable and subscribes (only once) to the source if
12 * the number of subscriptions is larger than 0. If the number of subscriptions is smaller than 1, it
13 * unsubscribes from the source. This way you can make sure that everything before the *published*
14 * refCount has only a single subscription independently of the number of subscribers to the target
15 * observable.
16 *
17 * Note that using the {@link share} operator is exactly the same as using the `multicast(() => new Subject())` operator
18 * (making the observable hot) and the *refCount* operator in a sequence.
19 *
20 * ![](refCount.png)
21 *
22 * ## Example
23 *
24 * In the following example there are two intervals turned into connectable observables
25 * by using the *publish* operator. The first one uses the *refCount* operator, the
26 * second one does not use it. You will notice that a connectable observable does nothing
27 * until you call its connect function.
28 *
29 * ```ts
30 * import { interval, tap, publish, refCount } from 'rxjs';
31 *
32 * // Turn the interval observable into a ConnectableObservable (hot)
33 * const refCountInterval = interval(400).pipe(
34 * tap(num => console.log(`refCount ${ num }`)),
35 * publish(),
36 * refCount()
37 * );
38 *
39 * const publishedInterval = interval(400).pipe(
40 * tap(num => console.log(`publish ${ num }`)),
41 * publish()
42 * );
43 *
44 * refCountInterval.subscribe();
45 * refCountInterval.subscribe();
46 * // 'refCount 0' -----> 'refCount 1' -----> etc
47 * // All subscriptions will receive the same value and the tap (and
48 * // every other operator) before the `publish` operator will be executed
49 * // only once per event independently of the number of subscriptions.
50 *
51 * publishedInterval.subscribe();
52 * // Nothing happens until you call .connect() on the observable.
53 * ```
54 *
55 * @return A function that returns an Observable that automates the connection
56 * to ConnectableObservable.
57 * @see {@link ConnectableObservable}
58 * @see {@link share}
59 * @see {@link publish}
60 * @deprecated Replaced with the {@link share} operator. How `share` is used
61 * will depend on the connectable observable you created just prior to the
62 * `refCount` operator.
63 * Details: https://rxjs.dev/deprecations/multicasting
64 */
65 export function refCount<T>(): MonoTypeOperatorFunction<T> {
66 return operate((source, subscriber) => {
67 let connection: Subscription | null = null;
68
69 (source as any)._refCount++;
70
71 const refCounter = createOperatorSubscriber(subscriber, undefined, undefined, undefined, () => {
72 if (!source || (source as any)._refCount <= 0 || 0 < --(source as any)._refCount) {
73 connection = null;
74 return;
75 }
76
77 ///
78 // Compare the local RefCountSubscriber's connection Subscription to the
79 // connection Subscription on the shared ConnectableObservable. In cases
80 // where the ConnectableObservable source synchronously emits values, and
81 // the RefCountSubscriber's downstream Observers synchronously unsubscribe,
82 // execution continues to here before the RefCountOperator has a chance to
83 // supply the RefCountSubscriber with the shared connection Subscription.
84 // For example:
85 // ```
86 // range(0, 10).pipe(
87 // publish(),
88 // refCount(),
89 // take(5),
90 // )
91 // .subscribe();
92 // ```
93 // In order to account for this case, RefCountSubscriber should only dispose
94 // the ConnectableObservable's shared connection Subscription if the
95 // connection Subscription exists, *and* either:
96 // a. RefCountSubscriber doesn't have a reference to the shared connection
97 // Subscription yet, or,
98 // b. RefCountSubscriber's connection Subscription reference is identical
99 // to the shared connection Subscription
100 ///
101
102 const sharedConnection = (source as any)._connection;
103 const conn = connection;
104 connection = null;
105
106 if (sharedConnection && (!conn || sharedConnection === conn)) {
107 sharedConnection.unsubscribe();
108 }
109
110 subscriber.unsubscribe();
111 });
112
113 source.subscribe(refCounter);
114
115 if (!refCounter.closed) {
116 connection = (source as ConnectableObservable<T>).connect();
117 }
118 });
119 }