88f8925e5f53e6c6881cac27ff01e46bf1d566550095cab8f2935c4fe18671937da9b17329f97388f8ec968137e544777c5dd9e134642548deb95078563ab4 4.0 KB

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