Debian Codegraph @

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

raw
1 import { Observable } from '../Observable';
2 import { Subject } from '../Subject';
3 import { multicast } from './multicast';
4 import { ConnectableObservable } from '../observable/ConnectableObservable';
5 import { MonoTypeOperatorFunction, OperatorFunction, UnaryFunction, ObservableInput, ObservedValueOf } from '../types';
6 import { connect } from './connect';
7
8 /**
9 * Returns a connectable observable that, when connected, will multicast
10 * all values through a single underlying {@link Subject} instance.
11 *
12 * @deprecated Will be removed in v8. To create a connectable observable, use {@link connectable}.
13 * `source.pipe(publish())` is equivalent to
14 * `connectable(source, { connector: () => new Subject(), resetOnDisconnect: false })`.
15 * If you're using {@link refCount} after `publish`, use {@link share} operator instead.
16 * `source.pipe(publish(), refCount())` is equivalent to
17 * `source.pipe(share({ resetOnError: false, resetOnComplete: false, resetOnRefCountZero: false }))`.
18 * Details: https://rxjs.dev/deprecations/multicasting
19 */
20 export function publish<T>(): UnaryFunction<Observable<T>, ConnectableObservable<T>>;
21
22 /**
23 * Returns an observable, that when subscribed to, creates an underlying {@link Subject},
24 * provides an observable view of it to a `selector` function, takes the observable result of
25 * that selector function and subscribes to it, sending its values to the consumer, _then_ connects
26 * the subject to the original source.
27 *
28 * @param selector A function used to setup multicasting prior to automatic connection.
29 *
30 * @deprecated Will be removed in v8. Use the {@link connect} operator instead.
31 * `publish(selector)` is equivalent to `connect(selector)`.
32 * Details: https://rxjs.dev/deprecations/multicasting
33 */
34 export function publish<T, O extends ObservableInput<any>>(selector: (shared: Observable<T>) => O): OperatorFunction<T, ObservedValueOf<O>>;
35
36 /**
37 * Returns a ConnectableObservable, which is a variety of Observable that waits until its connect method is called
38 * before it begins emitting items to those Observers that have subscribed to it.
39 *
40 * <span class="informal">Makes a cold Observable hot</span>
41 *
42 * ![](publish.png)
43 *
44 * ## Examples
45 *
46 * Make `source$` hot by applying `publish` operator, then merge each inner observable into a single one
47 * and subscribe
48 *
49 * ```ts
50 * import { zip, interval, of, map, publish, merge, tap } from 'rxjs';
51 *
52 * const source$ = zip(interval(2000), of(1, 2, 3, 4, 5, 6, 7, 8, 9))
53 * .pipe(map(([, number]) => number));
54 *
55 * source$
56 * .pipe(
57 * publish(multicasted$ =>
58 * merge(
59 * multicasted$.pipe(tap(x => console.log('Stream 1:', x))),
60 * multicasted$.pipe(tap(x => console.log('Stream 2:', x))),
61 * multicasted$.pipe(tap(x => console.log('Stream 3:', x)))
62 * )
63 * )
64 * )
65 * .subscribe();
66 *
67 * // Results every two seconds
68 * // Stream 1: 1
69 * // Stream 2: 1
70 * // Stream 3: 1
71 * // ...
72 * // Stream 1: 9
73 * // Stream 2: 9
74 * // Stream 3: 9
75 * ```
76 *
77 * @see {@link publishLast}
78 * @see {@link publishReplay}
79 * @see {@link publishBehavior}
80 *
81 * @param selector Optional selector function which can use the multicasted source sequence as many times
82 * as needed, without causing multiple subscriptions to the source sequence.
83 * Subscribers to the given source will receive all notifications of the source from the time of the subscription on.
84 * @return A function that returns a ConnectableObservable that upon connection
85 * causes the source Observable to emit items to its Observers.
86 * @deprecated Will be removed in v8. Use the {@link connectable} observable, the {@link connect} operator or the
87 * {@link share} operator instead. See the overloads below for equivalent replacement examples of this operator's
88 * behaviors.
89 * Details: https://rxjs.dev/deprecations/multicasting
90 */
91 export function publish<T, R>(selector?: OperatorFunction<T, R>): MonoTypeOperatorFunction<T> | OperatorFunction<T, R> {
92 return selector ? (source) => connect(selector)(source) : (source) => multicast(new Subject<T>())(source);
93 }