es6/operator/every.js
import { Subscriber } from '../Subscriber';
/**
* 返回的 Observable 发出是否源 Observable 的每项都满足指定的条件。
*
* @example <caption>一个简单示例:如果所有元素都小于5就发出 `true`,反之 `false`</caption>
* Observable.of(1, 2, 3, 4, 5, 6)
* .every(x => x < 5)
* .subscribe(x => console.log(x)); // -> false
*
* @param {function} predicate 用来确定每一项是否满足指定条件的函数。
* @param {any} [thisArg] 可选对象,作为回调函数中的 `this` 使用。
* @return {Observable} 布尔值的 Observable,用来确定是否源 Observable 的所有项都满足指定条件。
* @method every
* @owner Observable
*/
export function every(predicate, thisArg) {
return this.lift(new EveryOperator(predicate, thisArg, this));
}
class EveryOperator {
constructor(predicate, thisArg, source) {
this.predicate = predicate;
this.thisArg = thisArg;
this.source = source;
}
call(observer, source) {
return source.subscribe(new EverySubscriber(observer, this.predicate, this.thisArg, this.source));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class EverySubscriber extends Subscriber {
constructor(destination, predicate, thisArg, source) {
super(destination);
this.predicate = predicate;
this.thisArg = thisArg;
this.source = source;
this.index = 0;
this.thisArg = thisArg || this;
}
notifyComplete(everyValueMatch) {
this.destination.next(everyValueMatch);
this.destination.complete();
}
_next(value) {
let result = false;
try {
result = this.predicate.call(this.thisArg, value, this.index++, this.source);
}
catch (err) {
this.destination.error(err);
return;
}
if (!result) {
this.notifyComplete(false);
}
}
_complete() {
this.notifyComplete(true);
}
}
//# sourceMappingURL=every.js.map