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);
9 return function (d, b) {
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
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); })); })); };
25 return function (source) { return source.lift(new SwitchMapOperator(project)); };
27 exports.switchMap = switchMap;
28 var SwitchMapOperator = (function () {
29 function SwitchMapOperator(project) {
30 this.project = project;
32 SwitchMapOperator.prototype.call = function (subscriber, source) {
33 return source.subscribe(new SwitchMapSubscriber(subscriber, this.project));
35 return SwitchMapOperator;
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;
45 SwitchMapSubscriber.prototype._next = function (value) {
47 var index = this.index++;
49 result = this.project(value, index);
52 this.destination.error(error);
55 this._innerSub(result, value, index);
57 SwitchMapSubscriber.prototype._innerSub = function (result, value, index) {
58 var innerSubscription = this.innerSubscription;
59 if (innerSubscription) {
60 innerSubscription.unsubscribe();
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);
70 SwitchMapSubscriber.prototype._complete = function () {
71 var innerSubscription = this.innerSubscription;
72 if (!innerSubscription || innerSubscription.closed) {
73 _super.prototype._complete.call(this);
77 SwitchMapSubscriber.prototype._unsubscribe = function () {
78 this.innerSubscription = null;
80 SwitchMapSubscriber.prototype.notifyComplete = function (innerSub) {
81 var destination = this.destination;
82 destination.remove(innerSub);
83 this.innerSubscription = null;
85 _super.prototype._complete.call(this);
88 SwitchMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
89 this.destination.next(innerValue);
91 return SwitchMapSubscriber;
92 }(OuterSubscriber_1.OuterSubscriber));
93 //# sourceMappingURL=switchMap.js.map