b81d2f7ce69afe4252b9d8f7f0ac16256d6d3c3f217eac95a98b6692b90a8f865efa8797b4e21de22cf52b724e45e4a80c65654dc1b29bf36437bd82ca80ce 1.0 KB

1234567891011121314151617181920212223242526
  1. import { Observable } from '../Observable';
  2. import { multicast } from './multicast';
  3. import { refCount } from './refCount';
  4. import { Subject } from '../Subject';
  5. import { MonoTypeOperatorFunction } from '../types';
  6. function shareSubjectFactory() {
  7. return new Subject();
  8. }
  9. /**
  10. * Returns a new Observable that multicasts (shares) the original Observable. As long as there is at least one
  11. * Subscriber this Observable will be subscribed and emitting data. When all subscribers have unsubscribed it will
  12. * unsubscribe from the source Observable. Because the Observable is multicasting it makes the stream `hot`.
  13. * This is an alias for `multicast(() => new Subject()), refCount()`.
  14. *
  15. * ![](share.png)
  16. *
  17. * @return {Observable<T>} An Observable that upon connection causes the source Observable to emit items to its Observers.
  18. * @method share
  19. * @owner Observable
  20. */
  21. export function share<T>(): MonoTypeOperatorFunction<T> {
  22. return (source: Observable<T>) => refCount()(multicast(shareSubjectFactory)(source)) as Observable<T>;
  23. }