Debian Codegraph @

node/rxjs/src/internal/observable/ConnectableObservable.ts

raw
1 import { Subject } from '../Subject';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { Subscription } from '../Subscription';
5 import { refCount as higherOrderRefCount } from '../operators/refCount';
6 import { createOperatorSubscriber } from '../operators/OperatorSubscriber';
7 import { hasLift } from '../util/lift';
8
9 /**
10 * @class ConnectableObservable<T>
11 * @deprecated Will be removed in v8. Use {@link connectable} to create a connectable observable.
12 * If you are using the `refCount` method of `ConnectableObservable`, use the {@link share} operator
13 * instead.
14 * Details: https://rxjs.dev/deprecations/multicasting
15 */
16 export class ConnectableObservable<T> extends Observable<T> {
17 protected _subject: Subject<T> | null = null;
18 protected _refCount: number = 0;
19 protected _connection: Subscription | null = null;
20
21 /**
22 * @param source The source observable
23 * @param subjectFactory The factory that creates the subject used internally.
24 * @deprecated Will be removed in v8. Use {@link connectable} to create a connectable observable.
25 * `new ConnectableObservable(source, factory)` is equivalent to
26 * `connectable(source, { connector: factory })`.
27 * When the `refCount()` method is needed, the {@link share} operator should be used instead:
28 * `new ConnectableObservable(source, factory).refCount()` is equivalent to
29 * `source.pipe(share({ connector: factory }))`.
30 * Details: https://rxjs.dev/deprecations/multicasting
31 */
32 constructor(public source: Observable<T>, protected subjectFactory: () => Subject<T>) {
33 super();
34 // If we have lift, monkey patch that here. This is done so custom observable
35 // types will compose through multicast. Otherwise the resulting observable would
36 // simply be an instance of `ConnectableObservable`.
37 if (hasLift(source)) {
38 this.lift = source.lift;
39 }
40 }
41
42 /** @internal */
43 protected _subscribe(subscriber: Subscriber<T>) {
44 return this.getSubject().subscribe(subscriber);
45 }
46
47 protected getSubject(): Subject<T> {
48 const subject = this._subject;
49 if (!subject || subject.isStopped) {
50 this._subject = this.subjectFactory();
51 }
52 return this._subject!;
53 }
54
55 protected _teardown() {
56 this._refCount = 0;
57 const { _connection } = this;
58 this._subject = this._connection = null;
59 _connection?.unsubscribe();
60 }
61
62 /**
63 * @deprecated {@link ConnectableObservable} will be removed in v8. Use {@link connectable} instead.
64 * Details: https://rxjs.dev/deprecations/multicasting
65 */
66 connect(): Subscription {
67 let connection = this._connection;
68 if (!connection) {
69 connection = this._connection = new Subscription();
70 const subject = this.getSubject();
71 connection.add(
72 this.source.subscribe(
73 createOperatorSubscriber(
74 subject as any,
75 undefined,
76 () => {
77 this._teardown();
78 subject.complete();
79 },
80 (err) => {
81 this._teardown();
82 subject.error(err);
83 },
84 () => this._teardown()
85 )
86 )
87 );
88
89 if (connection.closed) {
90 this._connection = null;
91 connection = Subscription.EMPTY;
92 }
93 }
94 return connection;
95 }
96
97 /**
98 * @deprecated {@link ConnectableObservable} will be removed in v8. Use the {@link share} operator instead.
99 * Details: https://rxjs.dev/deprecations/multicasting
100 */
101 refCount(): Observable<T> {
102 return higherOrderRefCount()(this) as Observable<T>;
103 }
104 }