switchMap.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var OuterSubscriber_1 = require("../OuterSubscriber");
  17. var InnerSubscriber_1 = require("../InnerSubscriber");
  18. var subscribeToResult_1 = require("../util/subscribeToResult");
  19. var map_1 = require("./map");
  20. var from_1 = require("../observable/from");
  21. function switchMap(project, resultSelector) {
  22. if (typeof resultSelector === 'function') {
  23. return function (source) { return source.pipe(switchMap(function (a, i) { return from_1.from(project(a, i)).pipe(map_1.map(function (b, ii) { return resultSelector(a, b, i, ii); })); })); };
  24. }
  25. return function (source) { return source.lift(new SwitchMapOperator(project)); };
  26. }
  27. exports.switchMap = switchMap;
  28. var SwitchMapOperator = (function () {
  29. function SwitchMapOperator(project) {
  30. this.project = project;
  31. }
  32. SwitchMapOperator.prototype.call = function (subscriber, source) {
  33. return source.subscribe(new SwitchMapSubscriber(subscriber, this.project));
  34. };
  35. return SwitchMapOperator;
  36. }());
  37. var SwitchMapSubscriber = (function (_super) {
  38. __extends(SwitchMapSubscriber, _super);
  39. function SwitchMapSubscriber(destination, project) {
  40. var _this = _super.call(this, destination) || this;
  41. _this.project = project;
  42. _this.index = 0;
  43. return _this;
  44. }
  45. SwitchMapSubscriber.prototype._next = function (value) {
  46. var result;
  47. var index = this.index++;
  48. try {
  49. result = this.project(value, index);
  50. }
  51. catch (error) {
  52. this.destination.error(error);
  53. return;
  54. }
  55. this._innerSub(result, value, index);
  56. };
  57. SwitchMapSubscriber.prototype._innerSub = function (result, value, index) {
  58. var innerSubscription = this.innerSubscription;
  59. if (innerSubscription) {
  60. innerSubscription.unsubscribe();
  61. }
  62. var innerSubscriber = new InnerSubscriber_1.InnerSubscriber(this, value, index);
  63. var destination = this.destination;
  64. destination.add(innerSubscriber);
  65. this.innerSubscription = subscribeToResult_1.subscribeToResult(this, result, undefined, undefined, innerSubscriber);
  66. if (this.innerSubscription !== innerSubscriber) {
  67. destination.add(this.innerSubscription);
  68. }
  69. };
  70. SwitchMapSubscriber.prototype._complete = function () {
  71. var innerSubscription = this.innerSubscription;
  72. if (!innerSubscription || innerSubscription.closed) {
  73. _super.prototype._complete.call(this);
  74. }
  75. this.unsubscribe();
  76. };
  77. SwitchMapSubscriber.prototype._unsubscribe = function () {
  78. this.innerSubscription = null;
  79. };
  80. SwitchMapSubscriber.prototype.notifyComplete = function (innerSub) {
  81. var destination = this.destination;
  82. destination.remove(innerSub);
  83. this.innerSubscription = null;
  84. if (this.isStopped) {
  85. _super.prototype._complete.call(this);
  86. }
  87. };
  88. SwitchMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  89. this.destination.next(innerValue);
  90. };
  91. return SwitchMapSubscriber;
  92. }(OuterSubscriber_1.OuterSubscriber));
  93. //# sourceMappingURL=switchMap.js.map