Front end of the Slack clone application.

multicast.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /** PURE_IMPORTS_START .._operators_multicast PURE_IMPORTS_END */
  2. import { multicast as higherOrder } from '../operators/multicast';
  3. /* tslint:enable:max-line-length */
  4. /**
  5. * Allows source Observable to be subscribed only once with a Subject of choice,
  6. * while still sharing its values between multiple subscribers.
  7. *
  8. * <span class="informal">Subscribe to Observable once, but send its values to multiple subscribers.</span>
  9. *
  10. * <img src="./img/multicast.png" width="100%">
  11. *
  12. * `multicast` is an operator that works in two modes.
  13. *
  14. * In the first mode you provide a single argument to it, which can be either an initialized Subject or a Subject
  15. * factory. As a result you will get a special kind of an Observable - a {@link ConnectableObservable}. It can be
  16. * subscribed multiple times, just as regular Observable, but it won't subscribe to the source Observable at that
  17. * moment. It will do it only if you call its `connect` method. This means you can essentially control by hand, when
  18. * source Observable will be actually subscribed. What is more, ConnectableObservable will share this one subscription
  19. * between all of its subscribers. This means that, for example, `ajax` Observable will only send a request once,
  20. * even though usually it would send a request per every subscriber. Since it sends a request at the moment of
  21. * subscription, here request would be sent when the `connect` method of a ConnectableObservable is called.
  22. *
  23. * The most common pattern of using ConnectableObservable is calling `connect` when the first consumer subscribes,
  24. * keeping the subscription alive while several consumers come and go and finally unsubscribing from the source
  25. * Observable, when the last consumer unsubscribes. To not implement that logic over and over again,
  26. * ConnectableObservable has a special operator, `refCount`. When called, it returns an Observable, which will count
  27. * the number of consumers subscribed to it and keep ConnectableObservable connected as long as there is at least
  28. * one consumer. So if you don't actually need to decide yourself when to connect and disconnect a
  29. * ConnectableObservable, use `refCount`.
  30. *
  31. * The second mode is invoked by calling `multicast` with an additional, second argument - selector function.
  32. * This function accepts an Observable - which basically mirrors the source Observable - and returns Observable
  33. * as well, which should be the input stream modified by any operators you want. Note that in this
  34. * mode you cannot provide initialized Subject as a first argument - it has to be a Subject factory. If
  35. * you provide selector function, `multicast` returns just a regular Observable, instead of ConnectableObservable.
  36. * Thus, as usual, each subscription to this stream triggers subscription to the source Observable. However,
  37. * if inside the selector function you subscribe to the input Observable multiple times, actual source stream
  38. * will be subscribed only once. So if you have a chain of operators that use some Observable many times,
  39. * but you want to subscribe to that Observable only once, this is the mode you would use.
  40. *
  41. * Subject provided as a first parameter of `multicast` is used as a proxy for the single subscription to the
  42. * source Observable. It means that all values from the source stream go through that Subject. Thus, if a Subject
  43. * has some special properties, Observable returned by `multicast` will have them as well. If you want to use
  44. * `multicast` with a Subject that is one of the ones included in RxJS by default - {@link Subject},
  45. * {@link AsyncSubject}, {@link BehaviorSubject}, or {@link ReplaySubject} - simply use {@link publish},
  46. * {@link publishLast}, {@link publishBehavior} or {@link publishReplay} respectively. These are actually
  47. * just wrappers around `multicast`, with a specific Subject hardcoded inside.
  48. *
  49. * Also, if you use {@link publish} or {@link publishReplay} with a ConnectableObservables `refCount` operator,
  50. * you can simply use {@link share} and {@link shareReplay} respectively, which chain these two.
  51. *
  52. * @example <caption>Use ConnectableObservable</caption>
  53. * const seconds = Rx.Observable.interval(1000);
  54. * const connectableSeconds = seconds.multicast(new Subject());
  55. *
  56. * connectableSeconds.subscribe(value => console.log('first: ' + value));
  57. * connectableSeconds.subscribe(value => console.log('second: ' + value));
  58. *
  59. * // At this point still nothing happens, even though we subscribed twice.
  60. *
  61. * connectableSeconds.connect();
  62. *
  63. * // From now on `seconds` are being logged to the console,
  64. * // twice per every second. `seconds` Observable was however only subscribed once,
  65. * // so under the hood Observable.interval had only one clock started.
  66. *
  67. * @example <caption>Use selector</caption>
  68. * const seconds = Rx.Observable.interval(1000);
  69. *
  70. * seconds
  71. * .multicast(
  72. * () => new Subject(),
  73. * seconds => seconds.zip(seconds) // Usually zip would subscribe to `seconds` twice.
  74. * // Because we are inside selector, `seconds` is subscribed once,
  75. * ) // thus starting only one clock used internally by Observable.interval.
  76. * .subscribe();
  77. *
  78. * @see {@link publish}
  79. * @see {@link publishLast}
  80. * @see {@link publishBehavior}
  81. * @see {@link publishReplay}
  82. * @see {@link share}
  83. * @see {@link shareReplay}
  84. *
  85. * @param {Function|Subject} subjectOrSubjectFactory - Factory function to create an intermediate Subject through
  86. * which the source sequence's elements will be multicast to the selector function input Observable or
  87. * ConnectableObservable returned by the operator.
  88. * @param {Function} [selector] - Optional selector function that can use the input stream
  89. * as many times as needed, without causing multiple subscriptions to the source stream.
  90. * Subscribers to the input source will receive all notifications of the source from the
  91. * time of the subscription forward.
  92. * @return {Observable<T>|ConnectableObservable<T>} An Observable that emits the results of invoking the selector
  93. * on the source stream or a special {@link ConnectableObservable}, if selector was not provided.
  94. *
  95. * @method multicast
  96. * @owner Observable
  97. */
  98. export function multicast(subjectOrSubjectFactory, selector) {
  99. return higherOrder(subjectOrSubjectFactory, selector)(this);
  100. }
  101. //# sourceMappingURL=multicast.js.map