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 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); })); })); };
23 return function (source) { return source.lift(new SwitchMapOperator(project)); };
25 exports.switchMap = switchMap;
26 var SwitchMapOperator = (function () {
27 function SwitchMapOperator(project) {
28 this.project = project;
30 SwitchMapOperator.prototype.call = function (subscriber, source) {
31 return source.subscribe(new SwitchMapSubscriber(subscriber, this.project));
33 return SwitchMapOperator;
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;
43 SwitchMapSubscriber.prototype._next = function (value) {
45 var index = this.index++;
47 result = this.project(value, index);
50 this.destination.error(error);
53 this._innerSub(result);
55 SwitchMapSubscriber.prototype._innerSub = function (result) {
56 var innerSubscription = this.innerSubscription;
57 if (innerSubscription) {
58 innerSubscription.unsubscribe();
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);
68 SwitchMapSubscriber.prototype._complete = function () {
69 var innerSubscription = this.innerSubscription;
70 if (!innerSubscription || innerSubscription.closed) {
71 _super.prototype._complete.call(this);
75 SwitchMapSubscriber.prototype._unsubscribe = function () {
76 this.innerSubscription = undefined;
78 SwitchMapSubscriber.prototype.notifyComplete = function () {
79 this.innerSubscription = undefined;
81 _super.prototype._complete.call(this);
84 SwitchMapSubscriber.prototype.notifyNext = function (innerValue) {
85 this.destination.next(innerValue);
87 return SwitchMapSubscriber;
88 }(innerSubscribe_1.SimpleOuterSubscriber));
89 //# sourceMappingURL=switchMap.js.map