blob: 6036411d68ae7954651cef439e0a5d83790c95ae [file] [log] [blame]
/** PURE_IMPORTS_START .._Subscriber PURE_IMPORTS_END */
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 __());
};
import { Subscriber } from '../Subscriber';
/**
* Emits values emitted by the source Observable so long as each value satisfies
* the given `predicate`, and then completes as soon as this `predicate` is not
* satisfied.
*
* <span class="informal">Takes values from the source only while they pass the
* condition given. When the first value does not satisfy, it completes.</span>
*
* <img src="./img/takeWhile.png" width="100%">
*
* `takeWhile` subscribes and begins mirroring the source Observable. Each value
* emitted on the source is given to the `predicate` function which returns a
* boolean, representing a condition to be satisfied by the source values. The
* output Observable emits the source values until such time as the `predicate`
* returns false, at which point `takeWhile` stops mirroring the source
* Observable and completes the output Observable.
*
* @example <caption>Emit click events only while the clientX property is greater than 200</caption>
* var clicks = Rx.Observable.fromEvent(document, 'click');
* var result = clicks.takeWhile(ev => ev.clientX > 200);
* result.subscribe(x => console.log(x));
*
* @see {@link take}
* @see {@link takeLast}
* @see {@link takeUntil}
* @see {@link skip}
*
* @param {function(value: T, index: number): boolean} predicate A function that
* evaluates a value emitted by the source Observable and returns a boolean.
* Also takes the (zero-based) index as the second argument.
* @return {Observable<T>} An Observable that emits the values from the source
* Observable so long as each value satisfies the condition defined by the
* `predicate`, then completes.
* @method takeWhile
* @owner Observable
*/
export function takeWhile(predicate) {
return function (source) { return source.lift(new TakeWhileOperator(predicate)); };
}
var TakeWhileOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
function TakeWhileOperator(predicate) {
this.predicate = predicate;
}
TakeWhileOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new TakeWhileSubscriber(subscriber, this.predicate));
};
return TakeWhileOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var TakeWhileSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
__extends(TakeWhileSubscriber, _super);
function TakeWhileSubscriber(destination, predicate) {
_super.call(this, destination);
this.predicate = predicate;
this.index = 0;
}
TakeWhileSubscriber.prototype._next = function (value) {
var destination = this.destination;
var result;
try {
result = this.predicate(value, this.index++);
}
catch (err) {
destination.error(err);
return;
}
this.nextOrComplete(value, result);
};
TakeWhileSubscriber.prototype.nextOrComplete = function (value, predicateResult) {
var destination = this.destination;
if (Boolean(predicateResult)) {
destination.next(value);
}
else {
destination.complete();
}
};
return TakeWhileSubscriber;
}(Subscriber));
//# sourceMappingURL=takeWhile.js.map