mirror items emitted by an Observable until a specified condition becomes false
The TakeWhile mirrors the source Observable until such time as some condition you specify becomes false, at which point TakeWhile stops mirroring the source Observable and terminates its own Observable.
takeWhile
The takeWhile
operator returns an Observable that mirrors the behavior of the source Observable until such time as a function, applied to an item emitted by that Observable, returns false
, whereupon the new Observable terminates with an onCompleted
notification.
numbers = Observable.from( [1, 2, 3, 4, 5, 6, 7, 8, 9] ); numbers.takeWhile({ ((it < 6) || (0 == (it % 2))) }).subscribe( { println(it); }, // onNext { println("Error: " + it.getMessage()); }, // onError { println("Sequence complete"); } // onCompleted );
takeWhile
does not by default operate on any particular Scheduler.
takeWhile(Func1)
takeWhile
The takeWhile
operator returns an Observable that mirrors the behavior of the source Observable until such time as a function, applied to an item emitted by that Observable, returns false
, whereupon the new Observable terminates with an onCompleted
notification.
takeWhile
does not by default operate on any particular Scheduler.
takeWhile(Func1)
takeWhile
RxJS implements the takeWhile
operator. You pass it a function that governs the takeping process. takeWhile
calls that function for each item emitted by the source Observable until such time as the function returns false
, whereupon takeWhile
stops mirroring the source Observable (starting with that item) and issues an onCompleted
notification. The function takes three parameters:
You may optionally pass a second parameter to takeWhile
. If so, that item will also be available to your predicate function as “this
”.
var source = Rx.Observable.range(1, 5) .takeWhile(function (x) { return x < 3; }); var subscription = source.subscribe( function (x) { console.log('Next: ' + x); }, function (err) { console.log('Error: ' + err); }, function () { console.log('Completed'); });
takeWhile
is found in each of the following distributions:
rx.js
rx.all.js
rx.all.compat.js
rx.compat.js
rx.lite.js
rx.lite.compat.js
takeWhile takeWhileWithIndex
RxPHP implements this operator as takeWhile
.
Returns elements from an observable sequence as long as a specified condition is true. It takes as a parameter a a callback to test each source element for a condition. The callback predicate is called with the value of the element.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/take/takeWhile.php $source = \Rx\Observable::range(1, 5) ->takeWhile(function ($x) { return $x < 3; }); $subscription = $source->subscribe($stdoutObserver);
RxPHP also has an operator takeWhileWithIndex
.
Returns elements from an observable sequence as long as a specified condition is true. It takes as a parameter a a callback to test each source element for a condition. The callback predicate is called with the index and the value of the element.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/take/takeWhileWithIndex.php $source = \Rx\Observable::range(1, 5) ->takeWhileWithIndex(function ($i) { return $i < 3; }); $subscription = $source->subscribe($stdoutObserver);
© ReactiveX contributors
Licensed under the Apache License 2.0.
http://reactivex.io/documentation/operators/takewhile.html