1 /** PURE_IMPORTS_START tslib,_OuterSubscriber,_InnerSubscriber,_util_subscribeToResult,_map,_observable_from PURE_IMPORTS_END */
2 import * as tslib_1 from "tslib";
3 import { OuterSubscriber } from '../OuterSubscriber';
4 import { InnerSubscriber } from '../InnerSubscriber';
5 import { subscribeToResult } from '../util/subscribeToResult';
6 import { map } from './map';
7 import { from } from '../observable/from';
8 export function switchMap(project, resultSelector) {
9 if (typeof resultSelector === 'function') {
10 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); })); })); };
12 return function (source) { return source.lift(new SwitchMapOperator(project)); };
14 var SwitchMapOperator = /*@__PURE__*/ (function () {
15 function SwitchMapOperator(project) {
16 this.project = project;
18 SwitchMapOperator.prototype.call = function (subscriber, source) {
19 return source.subscribe(new SwitchMapSubscriber(subscriber, this.project));
21 return SwitchMapOperator;
23 var SwitchMapSubscriber = /*@__PURE__*/ (function (_super) {
24 tslib_1.__extends(SwitchMapSubscriber, _super);
25 function SwitchMapSubscriber(destination, project) {
26 var _this = _super.call(this, destination) || this;
27 _this.project = project;
31 SwitchMapSubscriber.prototype._next = function (value) {
33 var index = this.index++;
35 result = this.project(value, index);
38 this.destination.error(error);
41 this._innerSub(result, value, index);
43 SwitchMapSubscriber.prototype._innerSub = function (result, value, index) {
44 var innerSubscription = this.innerSubscription;
45 if (innerSubscription) {
46 innerSubscription.unsubscribe();
48 var innerSubscriber = new InnerSubscriber(this, value, index);
49 var destination = this.destination;
50 destination.add(innerSubscriber);
51 this.innerSubscription = subscribeToResult(this, result, undefined, undefined, innerSubscriber);
52 if (this.innerSubscription !== innerSubscriber) {
53 destination.add(this.innerSubscription);
56 SwitchMapSubscriber.prototype._complete = function () {
57 var innerSubscription = this.innerSubscription;
58 if (!innerSubscription || innerSubscription.closed) {
59 _super.prototype._complete.call(this);
63 SwitchMapSubscriber.prototype._unsubscribe = function () {
64 this.innerSubscription = null;
66 SwitchMapSubscriber.prototype.notifyComplete = function (innerSub) {
67 var destination = this.destination;
68 destination.remove(innerSub);
69 this.innerSubscription = null;
71 _super.prototype._complete.call(this);
74 SwitchMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
75 this.destination.next(innerValue);
77 return SwitchMapSubscriber;
79 //# sourceMappingURL=switchMap.js.map