multicast.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. var ConnectableObservable_1 = require('../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. 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, ConnectableObservable_1.connectableObservableDescriptor);
  38. connectable.source = source;
  39. connectable.subjectFactory = subjectFactory;
  40. return connectable;
  41. };
  42. }
  43. exports.multicast = multicast;
  44. var MulticastOperator = (function () {
  45. function MulticastOperator(subjectFactory, selector) {
  46. this.subjectFactory = subjectFactory;
  47. this.selector = selector;
  48. }
  49. MulticastOperator.prototype.call = function (subscriber, source) {
  50. var selector = this.selector;
  51. var subject = this.subjectFactory();
  52. var subscription = selector(subject).subscribe(subscriber);
  53. subscription.add(source.subscribe(subject));
  54. return subscription;
  55. };
  56. return MulticastOperator;
  57. }());
  58. exports.MulticastOperator = MulticastOperator;
  59. //# sourceMappingURL=multicast.js.map