Debian Codegraph @

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

raw
1 import { Subject } from '../Subject';
2 import { Observable } from '../Observable';
3 import { ConnectableObservable } from '../observable/ConnectableObservable';
4 import { OperatorFunction, UnaryFunction, ObservedValueOf, ObservableInput } from '../types';
5 import { isFunction } from '../util/isFunction';
6 import { connect } from './connect';
7
8 /**
9 * An operator that creates a {@link ConnectableObservable}, that when connected,
10 * with the `connect` method, will use the provided subject to multicast the values
11 * from the source to all consumers.
12 *
13 * @param subject The subject to multicast through.
14 * @return A function that returns a {@link ConnectableObservable}
15 * @deprecated Will be removed in v8. To create a connectable observable, use {@link connectable}.
16 * If you're using {@link refCount} after `multicast`, use the {@link share} operator instead.
17 * `multicast(subject), refCount()` is equivalent to
18 * `share({ connector: () => subject, resetOnError: false, resetOnComplete: false, resetOnRefCountZero: false })`.
19 * Details: https://rxjs.dev/deprecations/multicasting
20 */
21 export function multicast<T>(subject: Subject<T>): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
22
23 /**
24 * Because this is deprecated in favor of the {@link connect} operator, and was otherwise poorly documented,
25 * rather than duplicate the effort of documenting the same behavior, please see documentation for the
26 * {@link connect} operator.
27 *
28 * @param subject The subject used to multicast.
29 * @param selector A setup function to setup the multicast
30 * @return A function that returns an observable that mirrors the observable returned by the selector.
31 * @deprecated Will be removed in v8. Use the {@link connect} operator instead.
32 * `multicast(subject, selector)` is equivalent to
33 * `connect(selector, { connector: () => subject })`.
34 * Details: https://rxjs.dev/deprecations/multicasting
35 */
36 export function multicast<T, O extends ObservableInput<any>>(
37 subject: Subject<T>,
38 selector: (shared: Observable<T>) => O
39 ): OperatorFunction<T, ObservedValueOf<O>>;
40
41 /**
42 * An operator that creates a {@link ConnectableObservable}, that when connected,
43 * with the `connect` method, will use the provided subject to multicast the values
44 * from the source to all consumers.
45 *
46 * @param subjectFactory A factory that will be called to create the subject. Passing a function here
47 * will cause the underlying subject to be "reset" on error, completion, or refCounted unsubscription of
48 * the source.
49 * @return A function that returns a {@link ConnectableObservable}
50 * @deprecated Will be removed in v8. To create a connectable observable, use {@link connectable}.
51 * If you're using {@link refCount} after `multicast`, use the {@link share} operator instead.
52 * `multicast(() => new BehaviorSubject('test')), refCount()` is equivalent to
53 * `share({ connector: () => new BehaviorSubject('test') })`.
54 * Details: https://rxjs.dev/deprecations/multicasting
55 */
56 export function multicast<T>(subjectFactory: () => Subject<T>): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
57
58 /**
59 * Because this is deprecated in favor of the {@link connect} operator, and was otherwise poorly documented,
60 * rather than duplicate the effort of documenting the same behavior, please see documentation for the
61 * {@link connect} operator.
62 *
63 * @param subjectFactory A factory that creates the subject used to multicast.
64 * @param selector A function to setup the multicast and select the output.
65 * @return A function that returns an observable that mirrors the observable returned by the selector.
66 * @deprecated Will be removed in v8. Use the {@link connect} operator instead.
67 * `multicast(subjectFactory, selector)` is equivalent to
68 * `connect(selector, { connector: subjectFactory })`.
69 * Details: https://rxjs.dev/deprecations/multicasting
70 */
71 export function multicast<T, O extends ObservableInput<any>>(
72 subjectFactory: () => Subject<T>,
73 selector: (shared: Observable<T>) => O
74 ): OperatorFunction<T, ObservedValueOf<O>>;
75
76 /**
77 * @deprecated Will be removed in v8. Use the {@link connectable} observable, the {@link connect} operator or the
78 * {@link share} operator instead. See the overloads below for equivalent replacement examples of this operator's
79 * behaviors.
80 * Details: https://rxjs.dev/deprecations/multicasting
81 */
82 export function multicast<T, R>(
83 subjectOrSubjectFactory: Subject<T> | (() => Subject<T>),
84 selector?: (source: Observable<T>) => Observable<R>
85 ): OperatorFunction<T, R> {
86 const subjectFactory = isFunction(subjectOrSubjectFactory) ? subjectOrSubjectFactory : () => subjectOrSubjectFactory;
87
88 if (isFunction(selector)) {
89 // If a selector function is provided, then we're a "normal" operator that isn't
90 // going to return a ConnectableObservable. We can use `connect` to do what we
91 // need to do.
92 return connect(selector, {
93 connector: subjectFactory,
94 });
95 }
96
97 return (source: Observable<T>) => new ConnectableObservable<any>(source, subjectFactory);
98 }