Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

switchMap.js 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 map_1 = require("./map");
  17. var from_1 = require("../observable/from");
  18. var innerSubscribe_1 = require("../innerSubscribe");
  19. function switchMap(project, resultSelector) {
  20. if (typeof resultSelector === 'function') {
  21. 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); })); })); };
  22. }
  23. return function (source) { return source.lift(new SwitchMapOperator(project)); };
  24. }
  25. exports.switchMap = switchMap;
  26. var SwitchMapOperator = (function () {
  27. function SwitchMapOperator(project) {
  28. this.project = project;
  29. }
  30. SwitchMapOperator.prototype.call = function (subscriber, source) {
  31. return source.subscribe(new SwitchMapSubscriber(subscriber, this.project));
  32. };
  33. return SwitchMapOperator;
  34. }());
  35. var SwitchMapSubscriber = (function (_super) {
  36. __extends(SwitchMapSubscriber, _super);
  37. function SwitchMapSubscriber(destination, project) {
  38. var _this = _super.call(this, destination) || this;
  39. _this.project = project;
  40. _this.index = 0;
  41. return _this;
  42. }
  43. SwitchMapSubscriber.prototype._next = function (value) {
  44. var result;
  45. var index = this.index++;
  46. try {
  47. result = this.project(value, index);
  48. }
  49. catch (error) {
  50. this.destination.error(error);
  51. return;
  52. }
  53. this._innerSub(result);
  54. };
  55. SwitchMapSubscriber.prototype._innerSub = function (result) {
  56. var innerSubscription = this.innerSubscription;
  57. if (innerSubscription) {
  58. innerSubscription.unsubscribe();
  59. }
  60. var innerSubscriber = new innerSubscribe_1.SimpleInnerSubscriber(this);
  61. var destination = this.destination;
  62. destination.add(innerSubscriber);
  63. this.innerSubscription = innerSubscribe_1.innerSubscribe(result, innerSubscriber);
  64. if (this.innerSubscription !== innerSubscriber) {
  65. destination.add(this.innerSubscription);
  66. }
  67. };
  68. SwitchMapSubscriber.prototype._complete = function () {
  69. var innerSubscription = this.innerSubscription;
  70. if (!innerSubscription || innerSubscription.closed) {
  71. _super.prototype._complete.call(this);
  72. }
  73. this.unsubscribe();
  74. };
  75. SwitchMapSubscriber.prototype._unsubscribe = function () {
  76. this.innerSubscription = undefined;
  77. };
  78. SwitchMapSubscriber.prototype.notifyComplete = function () {
  79. this.innerSubscription = undefined;
  80. if (this.isStopped) {
  81. _super.prototype._complete.call(this);
  82. }
  83. };
  84. SwitchMapSubscriber.prototype.notifyNext = function (innerValue) {
  85. this.destination.next(innerValue);
  86. };
  87. return SwitchMapSubscriber;
  88. }(innerSubscribe_1.SimpleOuterSubscriber));
  89. //# sourceMappingURL=switchMap.js.map