@reactivex/rxjs
Version:
Reactive Extensions for modern JavaScript
42 lines • 1.38 kB
JavaScript
import { Subscriber } from '../Subscriber';
/**
* Returns an Observable that emits the elements of the source or a specified default value if empty.
* @param {any} defaultValue the default value used if source is empty; defaults to null.
* @return {Observable} an Observable of the items emitted by the where empty values are replaced by the specified default value or null.
* @method defaultIfEmpty
* @owner Observable
*/
export function defaultIfEmpty(defaultValue = null) {
return this.lift(new DefaultIfEmptyOperator(defaultValue));
}
class DefaultIfEmptyOperator {
constructor(defaultValue) {
this.defaultValue = defaultValue;
}
call(subscriber, source) {
return source._subscribe(new DefaultIfEmptySubscriber(subscriber, this.defaultValue));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class DefaultIfEmptySubscriber extends Subscriber {
constructor(destination, defaultValue) {
super(destination);
this.defaultValue = defaultValue;
this.isEmpty = true;
}
_next(value) {
this.isEmpty = false;
this.destination.next(value);
}
_complete() {
if (this.isEmpty) {
this.destination.next(this.defaultValue);
}
this.destination.complete();
}
}
//# sourceMappingURL=defaultIfEmpty.js.map