1 | import { OperatorFunction, ObservableInput, ObservedValueOf, SubjectLike } from '../types';
|
2 | import { Observable } from '../Observable';
|
3 | /**
|
4 | * An object used to configure {@link connect} operator.
|
5 | */
|
6 | export interface ConnectConfig<T> {
|
7 | /**
|
8 | * A factory function used to create the Subject through which the source
|
9 | * is multicast. By default, this creates a {@link Subject}.
|
10 | */
|
11 | connector: () => SubjectLike<T>;
|
12 | }
|
13 | /**
|
14 | * Creates an observable by multicasting the source within a function that
|
15 | * allows the developer to define the usage of the multicast prior to connection.
|
16 | *
|
17 | * This is particularly useful if the observable source you wish to multicast could
|
18 | * be synchronous or asynchronous. This sets it apart from {@link share}, which, in the
|
19 | * case of totally synchronous sources will fail to share a single subscription with
|
20 | * multiple consumers, as by the time the subscription to the result of {@link share}
|
21 | * has returned, if the source is synchronous its internal reference count will jump from
|
22 | * 0 to 1 back to 0 and reset.
|
23 | *
|
24 | * To use `connect`, you provide a `selector` function that will give you
|
25 | * a multicast observable that is not yet connected. You then use that multicast observable
|
26 | * to create a resulting observable that, when subscribed, will set up your multicast. This is
|
27 | * generally, but not always, accomplished with {@link merge}.
|
28 | *
|
29 | * Note that using a {@link takeUntil} inside of `connect`'s `selector` _might_ mean you were looking
|
30 | * to use the {@link takeWhile} operator instead.
|
31 | *
|
32 | * When you subscribe to the result of `connect`, the `selector` function will be called. After
|
33 | * the `selector` function returns, the observable it returns will be subscribed to, _then_ the
|
34 | * multicast will be connected to the source.
|
35 | *
|
36 | * ## Example
|
37 | *
|
38 | * Sharing a totally synchronous observable
|
39 | *
|
40 | * ```ts
|
41 | * import { of, tap, connect, merge, map, filter } from 'rxjs';
|
42 | *
|
43 | * const source$ = of(1, 2, 3, 4, 5).pipe(
|
44 | * tap({
|
45 | * subscribe: () => console.log('subscription started'),
|
46 | * next: n => console.log(`source emitted ${ n }`)
|
47 | * })
|
48 | * );
|
49 | *
|
50 | * source$.pipe(
|
51 | * // Notice in here we're merging 3 subscriptions to `shared$`.
|
52 | * connect(shared$ => merge(
|
53 | * shared$.pipe(map(n => `all ${ n }`)),
|
54 | * shared$.pipe(filter(n => n % 2 === 0), map(n => `even ${ n }`)),
|
55 | * shared$.pipe(filter(n => n % 2 === 1), map(n => `odd ${ n }`))
|
56 | * ))
|
57 | * )
|
58 | * .subscribe(console.log);
|
59 | *
|
60 | * // Expected output: (notice only one subscription)
|
61 | * 'subscription started'
|
62 | * 'source emitted 1'
|
63 | * 'all 1'
|
64 | * 'odd 1'
|
65 | * 'source emitted 2'
|
66 | * 'all 2'
|
67 | * 'even 2'
|
68 | * 'source emitted 3'
|
69 | * 'all 3'
|
70 | * 'odd 3'
|
71 | * 'source emitted 4'
|
72 | * 'all 4'
|
73 | * 'even 4'
|
74 | * 'source emitted 5'
|
75 | * 'all 5'
|
76 | * 'odd 5'
|
77 | * ```
|
78 | *
|
79 | * @param selector A function used to set up the multicast. Gives you a multicast observable
|
80 | * that is not yet connected. With that, you're expected to create and return
|
81 | * and Observable, that when subscribed to, will utilize the multicast observable.
|
82 | * After this function is executed -- and its return value subscribed to -- the
|
83 | * operator will subscribe to the source, and the connection will be made.
|
84 | * @param config The configuration object for `connect`.
|
85 | */
|
86 | export declare function connect<T, O extends ObservableInput<unknown>>(selector: (shared: Observable<T>) => O, config?: ConnectConfig<T>): OperatorFunction<T, ObservedValueOf<O>>;
|
87 | //# sourceMappingURL=connect.d.ts.map |
\ | No newline at end of file |