97 lines
3.8 KiB
JavaScript
97 lines
3.8 KiB
JavaScript
"use strict";
|
|
var __extends = (this && this.__extends) || function (d, b) {
|
|
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
|
function __() { this.constructor = d; }
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
};
|
|
var OuterSubscriber_1 = require('../OuterSubscriber');
|
|
var subscribeToResult_1 = require('../util/subscribeToResult');
|
|
/**
|
|
* Returns a new Observable by applying a function that you supply to each item emitted by the source Observable that
|
|
* returns an Observable, and then emitting the items emitted by the most recently emitted of these Observables.
|
|
*
|
|
* <img src="./img/switchMap.png" width="100%">
|
|
*
|
|
* @param {Observable} a function that, when applied to an item emitted by the source Observable, returns an Observable.
|
|
* @returns {Observable} an Observable that emits the items emitted by the Observable returned from applying func to
|
|
* the most recently emitted item emitted by the source Observable.
|
|
*/
|
|
function switchMap(project, resultSelector) {
|
|
return this.lift(new SwitchMapOperator(project, resultSelector));
|
|
}
|
|
exports.switchMap = switchMap;
|
|
var SwitchMapOperator = (function () {
|
|
function SwitchMapOperator(project, resultSelector) {
|
|
this.project = project;
|
|
this.resultSelector = resultSelector;
|
|
}
|
|
SwitchMapOperator.prototype.call = function (subscriber) {
|
|
return new SwitchMapSubscriber(subscriber, this.project, this.resultSelector);
|
|
};
|
|
return SwitchMapOperator;
|
|
}());
|
|
var SwitchMapSubscriber = (function (_super) {
|
|
__extends(SwitchMapSubscriber, _super);
|
|
function SwitchMapSubscriber(destination, project, resultSelector) {
|
|
_super.call(this, destination);
|
|
this.project = project;
|
|
this.resultSelector = resultSelector;
|
|
this.index = 0;
|
|
}
|
|
SwitchMapSubscriber.prototype._next = function (value) {
|
|
var result;
|
|
var index = this.index++;
|
|
try {
|
|
result = this.project(value, index);
|
|
}
|
|
catch (error) {
|
|
this.destination.error(error);
|
|
return;
|
|
}
|
|
this._innerSub(result, value, index);
|
|
};
|
|
SwitchMapSubscriber.prototype._innerSub = function (result, value, index) {
|
|
var innerSubscription = this.innerSubscription;
|
|
if (innerSubscription) {
|
|
innerSubscription.unsubscribe();
|
|
}
|
|
this.add(this.innerSubscription = subscribeToResult_1.subscribeToResult(this, result, value, index));
|
|
};
|
|
SwitchMapSubscriber.prototype._complete = function () {
|
|
var innerSubscription = this.innerSubscription;
|
|
if (!innerSubscription || innerSubscription.isUnsubscribed) {
|
|
_super.prototype._complete.call(this);
|
|
}
|
|
};
|
|
SwitchMapSubscriber.prototype._unsubscribe = function () {
|
|
this.innerSubscription = null;
|
|
};
|
|
SwitchMapSubscriber.prototype.notifyComplete = function (innerSub) {
|
|
this.remove(innerSub);
|
|
this.innerSubscription = null;
|
|
if (this.isStopped) {
|
|
_super.prototype._complete.call(this);
|
|
}
|
|
};
|
|
SwitchMapSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
|
|
if (this.resultSelector) {
|
|
this._tryNotifyNext(outerValue, innerValue, outerIndex, innerIndex);
|
|
}
|
|
else {
|
|
this.destination.next(innerValue);
|
|
}
|
|
};
|
|
SwitchMapSubscriber.prototype._tryNotifyNext = function (outerValue, innerValue, outerIndex, innerIndex) {
|
|
var result;
|
|
try {
|
|
result = this.resultSelector(outerValue, innerValue, outerIndex, innerIndex);
|
|
}
|
|
catch (err) {
|
|
this.destination.error(err);
|
|
return;
|
|
}
|
|
this.destination.next(result);
|
|
};
|
|
return SwitchMapSubscriber;
|
|
}(OuterSubscriber_1.OuterSubscriber));
|
|
//# sourceMappingURL=switchMap.js.map
|