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