switchMap.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /** PURE_IMPORTS_START tslib,_map,_observable_from,_innerSubscribe PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { map } from './map';
  4. import { from } from '../observable/from';
  5. import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
  6. export function switchMap(project, resultSelector) {
  7. if (typeof resultSelector === 'function') {
  8. return function (source) { return source.pipe(switchMap(function (a, i) { return from(project(a, i)).pipe(map(function (b, ii) { return resultSelector(a, b, i, ii); })); })); };
  9. }
  10. return function (source) { return source.lift(new SwitchMapOperator(project)); };
  11. }
  12. var SwitchMapOperator = /*@__PURE__*/ (function () {
  13. function SwitchMapOperator(project) {
  14. this.project = project;
  15. }
  16. SwitchMapOperator.prototype.call = function (subscriber, source) {
  17. return source.subscribe(new SwitchMapSubscriber(subscriber, this.project));
  18. };
  19. return SwitchMapOperator;
  20. }());
  21. var SwitchMapSubscriber = /*@__PURE__*/ (function (_super) {
  22. tslib_1.__extends(SwitchMapSubscriber, _super);
  23. function SwitchMapSubscriber(destination, project) {
  24. var _this = _super.call(this, destination) || this;
  25. _this.project = project;
  26. _this.index = 0;
  27. return _this;
  28. }
  29. SwitchMapSubscriber.prototype._next = function (value) {
  30. var result;
  31. var index = this.index++;
  32. try {
  33. result = this.project(value, index);
  34. }
  35. catch (error) {
  36. this.destination.error(error);
  37. return;
  38. }
  39. this._innerSub(result);
  40. };
  41. SwitchMapSubscriber.prototype._innerSub = function (result) {
  42. var innerSubscription = this.innerSubscription;
  43. if (innerSubscription) {
  44. innerSubscription.unsubscribe();
  45. }
  46. var innerSubscriber = new SimpleInnerSubscriber(this);
  47. var destination = this.destination;
  48. destination.add(innerSubscriber);
  49. this.innerSubscription = innerSubscribe(result, innerSubscriber);
  50. if (this.innerSubscription !== innerSubscriber) {
  51. destination.add(this.innerSubscription);
  52. }
  53. };
  54. SwitchMapSubscriber.prototype._complete = function () {
  55. var innerSubscription = this.innerSubscription;
  56. if (!innerSubscription || innerSubscription.closed) {
  57. _super.prototype._complete.call(this);
  58. }
  59. this.unsubscribe();
  60. };
  61. SwitchMapSubscriber.prototype._unsubscribe = function () {
  62. this.innerSubscription = undefined;
  63. };
  64. SwitchMapSubscriber.prototype.notifyComplete = function () {
  65. this.innerSubscription = undefined;
  66. if (this.isStopped) {
  67. _super.prototype._complete.call(this);
  68. }
  69. };
  70. SwitchMapSubscriber.prototype.notifyNext = function (innerValue) {
  71. this.destination.next(innerValue);
  72. };
  73. return SwitchMapSubscriber;
  74. }(SimpleOuterSubscriber));
  75. //# sourceMappingURL=switchMap.js.map