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.
TBD
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 );
1 2 3 4 5 6 Sequence complete
takeWhile
does not by default operate on any particular
Scheduler.
takeWhile(Func1)
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)
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'); });
Next: 0 Next: 1 Next: 2 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
TBD
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);
Next value: 1 Next value: 2 Complete!
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);
Next value: 1 Next value: 2 Next value: 3 Complete!
TBD