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