60 lines
2.4 KiB
JavaScript
60 lines
2.4 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 Subscriber_1 = require('../Subscriber');
|
|
var ArgumentOutOfRangeError_1 = require('../util/ArgumentOutOfRangeError');
|
|
/**
|
|
* Returns an Observable that emits the item at the specified index in the source Observable.
|
|
* If default is given, missing indices will output this value on next; otherwise, outputs error.
|
|
* @param {number} index the index of the value to be retrieved.
|
|
* @param {any} [defaultValue] the default value returned for missing indices.
|
|
* @returns {Observable} an Observable that emits a single item, if it is found. Otherwise, will emit the default value if given.
|
|
*/
|
|
function elementAt(index, defaultValue) {
|
|
return this.lift(new ElementAtOperator(index, defaultValue));
|
|
}
|
|
exports.elementAt = elementAt;
|
|
var ElementAtOperator = (function () {
|
|
function ElementAtOperator(index, defaultValue) {
|
|
this.index = index;
|
|
this.defaultValue = defaultValue;
|
|
if (index < 0) {
|
|
throw new ArgumentOutOfRangeError_1.ArgumentOutOfRangeError;
|
|
}
|
|
}
|
|
ElementAtOperator.prototype.call = function (subscriber) {
|
|
return new ElementAtSubscriber(subscriber, this.index, this.defaultValue);
|
|
};
|
|
return ElementAtOperator;
|
|
}());
|
|
var ElementAtSubscriber = (function (_super) {
|
|
__extends(ElementAtSubscriber, _super);
|
|
function ElementAtSubscriber(destination, index, defaultValue) {
|
|
_super.call(this, destination);
|
|
this.index = index;
|
|
this.defaultValue = defaultValue;
|
|
}
|
|
ElementAtSubscriber.prototype._next = function (x) {
|
|
if (this.index-- === 0) {
|
|
this.destination.next(x);
|
|
this.destination.complete();
|
|
}
|
|
};
|
|
ElementAtSubscriber.prototype._complete = function () {
|
|
var destination = this.destination;
|
|
if (this.index >= 0) {
|
|
if (typeof this.defaultValue !== 'undefined') {
|
|
destination.next(this.defaultValue);
|
|
}
|
|
else {
|
|
destination.error(new ArgumentOutOfRangeError_1.ArgumentOutOfRangeError);
|
|
}
|
|
}
|
|
destination.complete();
|
|
};
|
|
return ElementAtSubscriber;
|
|
}(Subscriber_1.Subscriber));
|
|
//# sourceMappingURL=elementAt.js.map
|