68 lines
2.7 KiB
JavaScript
68 lines
2.7 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');
|
|
/**
|
|
* Returns an Observable that searches for the first item in the source Observable that
|
|
* matches the specified condition, and returns the first occurence in the source.
|
|
* @param {function} predicate function called with each item to test for condition matching.
|
|
* @returns {Observable} an Observable of the first item that matches the condition.
|
|
*/
|
|
function find(predicate, thisArg) {
|
|
if (typeof predicate !== 'function') {
|
|
throw new TypeError('predicate is not a function');
|
|
}
|
|
return this.lift(new FindValueOperator(predicate, this, false, thisArg));
|
|
}
|
|
exports.find = find;
|
|
var FindValueOperator = (function () {
|
|
function FindValueOperator(predicate, source, yieldIndex, thisArg) {
|
|
this.predicate = predicate;
|
|
this.source = source;
|
|
this.yieldIndex = yieldIndex;
|
|
this.thisArg = thisArg;
|
|
}
|
|
FindValueOperator.prototype.call = function (observer) {
|
|
return new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg);
|
|
};
|
|
return FindValueOperator;
|
|
}());
|
|
exports.FindValueOperator = FindValueOperator;
|
|
var FindValueSubscriber = (function (_super) {
|
|
__extends(FindValueSubscriber, _super);
|
|
function FindValueSubscriber(destination, predicate, source, yieldIndex, thisArg) {
|
|
_super.call(this, destination);
|
|
this.predicate = predicate;
|
|
this.source = source;
|
|
this.yieldIndex = yieldIndex;
|
|
this.thisArg = thisArg;
|
|
this.index = 0;
|
|
}
|
|
FindValueSubscriber.prototype.notifyComplete = function (value) {
|
|
var destination = this.destination;
|
|
destination.next(value);
|
|
destination.complete();
|
|
};
|
|
FindValueSubscriber.prototype._next = function (value) {
|
|
var _a = this, predicate = _a.predicate, thisArg = _a.thisArg;
|
|
var index = this.index++;
|
|
try {
|
|
var result = predicate.call(thisArg || this, value, index, this.source);
|
|
if (result) {
|
|
this.notifyComplete(this.yieldIndex ? index : value);
|
|
}
|
|
}
|
|
catch (err) {
|
|
this.destination.error(err);
|
|
}
|
|
};
|
|
FindValueSubscriber.prototype._complete = function () {
|
|
this.notifyComplete(this.yieldIndex ? -1 : undefined);
|
|
};
|
|
return FindValueSubscriber;
|
|
}(Subscriber_1.Subscriber));
|
|
exports.FindValueSubscriber = FindValueSubscriber;
|
|
//# sourceMappingURL=find.js.map
|