es6/operator/find.js
import { Subscriber } from '../Subscriber';
/* tslint:disable:max-line-length */
export 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));
}
export 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, source) {
return source._subscribe(new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg));
};
return FindValueOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
export 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));
//# sourceMappingURL=find.js.map