The DefaultIfEmpty operator simply mirrors the source Observable exactly
if the source Observable emits any items. If the source Observable terminates normally (with an
onComplete) without emitting any items, the Observable returned from
DefaultIfEmpty will instead emit a default item of your choosing before
it too completes.
TBD
TBD
RxGroovy implements this operator as defaultIfEmpty.
This operator does not by default operate on any particular Scheduler.
defaultIfEmpty(T)
There is also a new operator in RxGroovy 1.1 called switchIfEmpty that, rather than emitting
a backup value if the source Observable terminates without having emitted any items, it emits the
emissions from a backup Observable.
RxJava implements this operator as defaultIfEmpty.
This operator does not by default operate on any particular Scheduler.
defaultIfEmpty(T)
There is also a new operator in RxJava 1.1 called switchIfEmpty that, rather than emitting a
backup value if the source Observable terminates without having emitted any items, it emits the
emissions from a backup Observable.
Observable.empty().defaultIfEmpty(10).subscribe(
val -> System.out.println("next: " + val),
err -> System.err.println(err) ,
() -> System.out.println("completed")
); next: 10 completed
RxJava implements this operator as defaultIfEmpty.
This operator does not by default operate on any particular Scheduler.
defaultIfEmpty(T)
Flowable.empty().defaultIfEmpty(10).subscribe(
val -> System.out.println("next: " + val),
err -> System.err.println(err) ,
() -> System.out.println("completed")
); next: 10 completed
RxJS implements defaultIfEmpty, but the parameter that sets the default value is optional.
If you do not pass this default value, defaultIfEmpty will emit a
“null” if the source Observable completes without emitting anything. (Note
that an emission of a “null” is not the same as no emission.)
/* Without a default value */
var source = Rx.Observable.empty().defaultIfEmpty();
var subscription = source.subscribe(
function (x) { console.log('Next: ' + x.toString()); },
function (err) { console.log('Error: ' + err); },
function () { console.log('Completed'); });Next: null Completed
/* With a defaultValue */
var source = Rx.Observable.empty().defaultIfEmpty(false);
var subscription = source.subscribe(
function (x) { console.log('Next: ' + x.toString()); },
function (err) { console.log('Error: ' + err); },
function () { console.log('Completed'); });Next: false Completed
defaultIfEmpty is found in each of the following distributions:
rx.jsrx.all.jsrx.all.compat.jsrx.compat.jsrx.lite.jsrx.lite.compat.js
RxPHP implements this operator as defaultIfEmpty.
Returns the specified value of an observable if the sequence is empty.
//from https://github.com/ReactiveX/RxPHP/blob/master/demo/defaultIfEmpty/defaultIfEmpty.php
$source = \Rx\Observable::empty()->defaultIfEmpty(Rx\Observable::of('something'));
$subscription = $source->subscribe($stdoutObserver);
Next value: something
Complete!