Switch subscribes to an Observable that emits Observables. Each time it observes one of these emitted Observables, the Observable returned by Switch unsubscribes from the previously-emitted Observable begins emitting items from the latest Observable. Note that it will unsubscribe from the previously-emitted Observable when a new Observable is emitted from the source Observable, not when the new Observable emits an item. This means that items emitted by the previous Observable between the time the subsequent Observable is emitted and the time that subsequent Observable itself begins emitting items will be dropped (as with the yellow circle in the diagram above).
TBD
RxGroovy implements this operator as switchOnNext
. It does not by default operate on any
particular Scheduler.
switchOnNext(Observable)
RxJava implements this operator as switchOnNext
. It does not by default operate on any
particular Scheduler.
switchOnNext(Observable)
RxJS implements this operator as switch
var source = Rx.Observable.range(0, 3) .select(function (x) { return Rx.Observable.range(x, 3); }) .switch(); 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 Next: 3 Next: 4 Completed
switch
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 switch
.
Transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/switch/switch.php $source = Rx\Observable::range(0, 3) ->map(function ($x) { return \Rx\Observable::range($x, 3); }) ->switch(); $subscription = $source->subscribe($stdoutObserver);
Next value: 0 Next value: 1 Next value: 2 Next value: 3 Next value: 4 Complete!
RxPHP also has an operator switchFirst
.
Receives an Observable of Observables and propagates the first Observable exclusively until it completes before it begins subscribes to the next Observable. Observables that come before the current Observable completes will be dropped and will not propagate. This operator is similar to concatAll() except that it will not hold onto Observables that come in before the current one is finished completed.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/switch/switchFirst.php $source = Rx\Observable::fromArray([ \Rx\Observable::interval(100)->mapTo('a'), \Rx\Observable::interval(200)->mapTo('b'), \Rx\Observable::interval(300)->mapTo('c'), ]) ->switchFirst() ->take(3); $subscription = $source->subscribe($stdoutObserver);
Next value: a Next value: a Next value: a Complete!
TBD
TBD