7c148a54c6a739fc503a711807421e3150ac13b3773e89a44dada3716349692843d386bb91c5e19ec0fd7e706855b8dd5b31f0ecd54444909c79e7bbd2db77 3.9 KB

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