multicast.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /** PURE_IMPORTS_START .._observable_ConnectableObservable PURE_IMPORTS_END */
  2. import { connectableObservableDescriptor } from '../observable/ConnectableObservable';
  3. /* tslint:enable:max-line-length */
  4. /**
  5. * Returns an Observable that emits the results of invoking a specified selector on items
  6. * emitted by a ConnectableObservable that shares a single subscription to the underlying stream.
  7. *
  8. * <img src="./img/multicast.png" width="100%">
  9. *
  10. * @param {Function|Subject} subjectOrSubjectFactory - Factory function to create an intermediate subject through
  11. * which the source sequence's elements will be multicast to the selector function
  12. * or Subject to push source elements into.
  13. * @param {Function} [selector] - Optional selector function that can use the multicasted source stream
  14. * as many times as needed, without causing multiple subscriptions to the source stream.
  15. * Subscribers to the given source will receive all notifications of the source from the
  16. * time of the subscription forward.
  17. * @return {Observable} An Observable that emits the results of invoking the selector
  18. * on the items emitted by a `ConnectableObservable` that shares a single subscription to
  19. * the underlying stream.
  20. * @method multicast
  21. * @owner Observable
  22. */
  23. export function multicast(subjectOrSubjectFactory, selector) {
  24. return function multicastOperatorFunction(source) {
  25. var subjectFactory;
  26. if (typeof subjectOrSubjectFactory === 'function') {
  27. subjectFactory = subjectOrSubjectFactory;
  28. }
  29. else {
  30. subjectFactory = function subjectFactory() {
  31. return subjectOrSubjectFactory;
  32. };
  33. }
  34. if (typeof selector === 'function') {
  35. return source.lift(new MulticastOperator(subjectFactory, selector));
  36. }
  37. var connectable = Object.create(source, connectableObservableDescriptor);
  38. connectable.source = source;
  39. connectable.subjectFactory = subjectFactory;
  40. return connectable;
  41. };
  42. }
  43. export var MulticastOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  44. function MulticastOperator(subjectFactory, selector) {
  45. this.subjectFactory = subjectFactory;
  46. this.selector = selector;
  47. }
  48. MulticastOperator.prototype.call = function (subscriber, source) {
  49. var selector = this.selector;
  50. var subject = this.subjectFactory();
  51. var subscription = selector(subject).subscribe(subscriber);
  52. subscription.add(source.subscribe(subject));
  53. return subscription;
  54. };
  55. return MulticastOperator;
  56. }());
  57. //# sourceMappingURL=multicast.js.map