Home Manual Reference Source Test Repository

es6/operator/skipWhile.js

import { Subscriber } from '../Subscriber';
/**
 * 返回一个 Observable, 该 Observable 会跳过由源 Observable 发出的所有满足指定条件的数据项,
 * 但是一旦出现了不满足条件的项,则发出在此之后的所有项。
 *
 * <img src="./img/skipWhile.png" width="100%">
 *
 * @param {Function} predicate - 函数,用来测试源 Observable 发出的每个数据项。
 * @return {Observable<T>} Observable,当指定的 predicate 函数返回 false 时,该 Observable 开始发出由源 Observable 发出的项。
 * @method skipWhile
 * @owner Observable
 */
export function skipWhile(predicate) {
    return this.lift(new SkipWhileOperator(predicate));
}
class SkipWhileOperator {
    constructor(predicate) {
        this.predicate = predicate;
    }
    call(subscriber, source) {
        return source.subscribe(new SkipWhileSubscriber(subscriber, this.predicate));
    }
}
/**
 * We need this JSDoc comment for affecting ESDoc.
 * @ignore
 * @extends {Ignored}
 */
class SkipWhileSubscriber extends Subscriber {
    constructor(destination, predicate) {
        super(destination);
        this.predicate = predicate;
        this.skipping = true;
        this.index = 0;
    }
    _next(value) {
        const destination = this.destination;
        if (this.skipping) {
            this.tryCallPredicate(value);
        }
        if (!this.skipping) {
            destination.next(value);
        }
    }
    tryCallPredicate(value) {
        try {
            const result = this.predicate(value, this.index++);
            this.skipping = Boolean(result);
        }
        catch (err) {
            this.destination.error(err);
        }
    }
}
//# sourceMappingURL=skipWhile.js.map