ec074baa0d1c9507ab9b89d7d58c609aa87a4aef97f4370024401dd310c55215b7e971860dfa57d2902e08e2a24a1de15e50a30423d472ddaf27bc71dbef11 1.5 KB

1234567891011121314151617181920212223242526
  1. import { Observable } from '../Observable';
  2. import { BehaviorSubject } from '../BehaviorSubject';
  3. import { ConnectableObservable } from '../observable/ConnectableObservable';
  4. import { UnaryFunction } from '../types';
  5. /**
  6. * Creates a {@link ConnectableObservable} that utilizes a {@link BehaviorSubject}.
  7. *
  8. * @param initialValue The initial value passed to the {@link BehaviorSubject}.
  9. * @return A function that returns a {@link ConnectableObservable}
  10. * @deprecated Will be removed in v8. To create a connectable observable that uses a
  11. * {@link BehaviorSubject} under the hood, use {@link connectable}.
  12. * `source.pipe(publishBehavior(initValue))` is equivalent to
  13. * `connectable(source, { connector: () => new BehaviorSubject(initValue), resetOnDisconnect: false })`.
  14. * If you're using {@link refCount} after `publishBehavior`, use the {@link share} operator instead.
  15. * `source.pipe(publishBehavior(initValue), refCount())` is equivalent to
  16. * `source.pipe(share({ connector: () => new BehaviorSubject(initValue), resetOnError: false, resetOnComplete: false, resetOnRefCountZero: false }))`.
  17. * Details: https://rxjs.dev/deprecations/multicasting
  18. */
  19. export function publishBehavior<T>(initialValue: T): UnaryFunction<Observable<T>, ConnectableObservable<T>> {
  20. // Note that this has *never* supported the selector function.
  21. return (source) => {
  22. const subject = new BehaviorSubject<T>(initialValue);
  23. return new ConnectableObservable(source, () => subject);
  24. };
  25. }