76 lines
3.0 KiB
JavaScript
76 lines
3.0 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');
|
||
|
/**
|
||
|
* Converts an Observable that emits Observables into an Observable that emits the items emitted by the most recently
|
||
|
* emitted of those Observables.
|
||
|
*
|
||
|
* <img src="./img/switch.png" width="100%">
|
||
|
*
|
||
|
* Switch subscribes to an Observable that emits Observables. Each time it observes one of these emitted Observables,
|
||
|
* the Observable returned by switchOnNext begins emitting the items emitted by that Observable. When a new Observable
|
||
|
* is emitted, switchOnNext stops emitting items from the earlier-emitted Observable and begins emitting items from the
|
||
|
* new one.
|
||
|
*
|
||
|
* @param {Function} a predicate function to evaluate items emitted by the source Observable.
|
||
|
* @returns {Observable<T>} an Observable that emits the items emitted by the Observable most recently emitted by the
|
||
|
* source Observable.
|
||
|
*/
|
||
|
function _switch() {
|
||
|
return this.lift(new SwitchOperator());
|
||
|
}
|
||
|
exports._switch = _switch;
|
||
|
var SwitchOperator = (function () {
|
||
|
function SwitchOperator() {
|
||
|
}
|
||
|
SwitchOperator.prototype.call = function (subscriber) {
|
||
|
return new SwitchSubscriber(subscriber);
|
||
|
};
|
||
|
return SwitchOperator;
|
||
|
}());
|
||
|
var SwitchSubscriber = (function (_super) {
|
||
|
__extends(SwitchSubscriber, _super);
|
||
|
function SwitchSubscriber(destination) {
|
||
|
_super.call(this, destination);
|
||
|
this.active = 0;
|
||
|
this.hasCompleted = false;
|
||
|
}
|
||
|
SwitchSubscriber.prototype._next = function (value) {
|
||
|
this.unsubscribeInner();
|
||
|
this.active++;
|
||
|
this.add(this.innerSubscription = subscribeToResult_1.subscribeToResult(this, value));
|
||
|
};
|
||
|
SwitchSubscriber.prototype._complete = function () {
|
||
|
this.hasCompleted = true;
|
||
|
if (this.active === 0) {
|
||
|
this.destination.complete();
|
||
|
}
|
||
|
};
|
||
|
SwitchSubscriber.prototype.unsubscribeInner = function () {
|
||
|
this.active = this.active > 0 ? this.active - 1 : 0;
|
||
|
var innerSubscription = this.innerSubscription;
|
||
|
if (innerSubscription) {
|
||
|
innerSubscription.unsubscribe();
|
||
|
this.remove(innerSubscription);
|
||
|
}
|
||
|
};
|
||
|
SwitchSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
|
||
|
this.destination.next(innerValue);
|
||
|
};
|
||
|
SwitchSubscriber.prototype.notifyError = function (err) {
|
||
|
this.destination.error(err);
|
||
|
};
|
||
|
SwitchSubscriber.prototype.notifyComplete = function () {
|
||
|
this.unsubscribeInner();
|
||
|
if (this.hasCompleted && this.active === 0) {
|
||
|
this.destination.complete();
|
||
|
}
|
||
|
};
|
||
|
return SwitchSubscriber;
|
||
|
}(OuterSubscriber_1.OuterSubscriber));
|
||
|
//# sourceMappingURL=switch.js.map
|