work.suroh.tk/node_modules/rxjs/operators/elementAt.js

100 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-12-02 12:22:45 +00:00
"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');
/**
* Emits the single value at the specified `index` in a sequence of emissions
* from the source Observable.
*
* <span class="informal">Emits only the i-th value, then completes.</span>
*
* <img src="./img/elementAt.png" width="100%">
*
* `elementAt` returns an Observable that emits the item at the specified
* `index` in the source Observable, or a default value if that `index` is out
* of range and the `default` argument is provided. If the `default` argument is
* not given and the `index` is out of range, the output Observable will emit an
* `ArgumentOutOfRangeError` error.
*
* @example <caption>Emit only the third click event</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var result = clicks.elementAt(2);
* result.subscribe(x => console.log(x));
*
* // Results in:
* // click 1 = nothing
* // click 2 = nothing
* // click 3 = MouseEvent object logged to console
*
* @see {@link first}
* @see {@link last}
* @see {@link skip}
* @see {@link single}
* @see {@link take}
*
* @throws {ArgumentOutOfRangeError} When using `elementAt(i)`, it delivers an
* ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0` or the
* Observable has completed before emitting the i-th `next` notification.
*
* @param {number} index Is the number `i` for the i-th source emission that has
* happened since the subscription, starting from the number `0`.
* @param {T} [defaultValue] The default value returned for missing indices.
* @return {Observable} An Observable that emits a single item, if it is found.
* Otherwise, will emit the default value if given. If not, then emits an error.
* @method elementAt
* @owner Observable
*/
function elementAt(index, defaultValue) {
return function (source) { return source.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, source) {
return source.subscribe(new ElementAtSubscriber(subscriber, this.index, this.defaultValue));
};
return ElementAtOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
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