1 import { Operator } from '../Operator';
2 import { Subscriber } from '../Subscriber';
3 import { Observable } from '../Observable';
4 import { OperatorFunction } from '../types';
7 * Emits false if the input observable emits any values, or emits true if the
8 * input observable completes without emitting any values.
10 * <span class="informal">Tells whether any values are emitted by an observable</span>
14 * `isEmpty` transforms an Observable that emits values into an Observable that
15 * emits a single boolean value representing whether or not any values were
16 * emitted by the source Observable. As soon as the source Observable emits a
17 * value, `isEmpty` will emit a `false` and complete. If the source Observable
18 * completes having not emitted anything, `isEmpty` will emit a `true` and
21 * A similar effect could be achieved with {@link count}, but `isEmpty` can emit
22 * a `false` value sooner.
26 * Emit `false` for a non-empty Observable
28 * import { Subject } from 'rxjs';
29 * import { isEmpty } from 'rxjs/operators';
31 * const source = new Subject<string>();
32 * const result = source.pipe(isEmpty());
33 * source.subscribe(x => console.log(x));
34 * result.subscribe(x => console.log(x));
47 * Emit `true` for an empty Observable
49 * import { EMPTY } from 'rxjs';
50 * import { isEmpty } from 'rxjs/operators';
52 * const result = EMPTY.pipe(isEmpty());
53 * result.subscribe(x => console.log(x));
61 * @return {OperatorFunction<T, boolean>} An Observable of a boolean value indicating whether observable was empty or not
66 export function isEmpty<T>(): OperatorFunction<T, boolean> {
67 return (source: Observable<T>) => source.lift(new IsEmptyOperator());
70 class IsEmptyOperator implements Operator<any, boolean> {
71 call (observer: Subscriber<boolean>, source: any): any {
72 return source.subscribe(new IsEmptySubscriber(observer));
77 * We need this JSDoc comment for affecting ESDoc.
81 class IsEmptySubscriber extends Subscriber<any> {
82 constructor(destination: Subscriber<boolean>) {
86 private notifyComplete(isEmpty: boolean): void {
87 const destination = this.destination;
89 destination.next(isEmpty);
90 destination.complete();
93 protected _next(value: boolean) {
94 this.notifyComplete(false);
97 protected _complete() {
98 this.notifyComplete(true);