source: trip-planner-front/node_modules/rxjs/src/internal/operators/isEmpty.ts@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import { Operator } from '../Operator';
2import { Subscriber } from '../Subscriber';
3import { Observable } from '../Observable';
4import { OperatorFunction } from '../types';
5
6/**
7 * Emits false if the input observable emits any values, or emits true if the
8 * input observable completes without emitting any values.
9 *
10 * <span class="informal">Tells whether any values are emitted by an observable</span>
11 *
12 * ![](isEmpty.png)
13 *
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
19 * complete.
20 *
21 * A similar effect could be achieved with {@link count}, but `isEmpty` can emit
22 * a `false` value sooner.
23 *
24 * ## Examples
25 *
26 * Emit `false` for a non-empty Observable
27 * ```javascript
28 * import { Subject } from 'rxjs';
29 * import { isEmpty } from 'rxjs/operators';
30 *
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));
35 * source.next('a');
36 * source.next('b');
37 * source.next('c');
38 * source.complete();
39 *
40 * // Results in:
41 * // a
42 * // false
43 * // b
44 * // c
45 * ```
46 *
47 * Emit `true` for an empty Observable
48 * ```javascript
49 * import { EMPTY } from 'rxjs';
50 * import { isEmpty } from 'rxjs/operators';
51 *
52 * const result = EMPTY.pipe(isEmpty());
53 * result.subscribe(x => console.log(x));
54 * // Results in:
55 * // true
56 * ```
57 *
58 * @see {@link count}
59 * @see {@link EMPTY}
60 *
61 * @return {OperatorFunction<T, boolean>} An Observable of a boolean value indicating whether observable was empty or not
62 * @method isEmpty
63 * @owner Observable
64 */
65
66export function isEmpty<T>(): OperatorFunction<T, boolean> {
67 return (source: Observable<T>) => source.lift(new IsEmptyOperator());
68}
69
70class IsEmptyOperator implements Operator<any, boolean> {
71 call (observer: Subscriber<boolean>, source: any): any {
72 return source.subscribe(new IsEmptySubscriber(observer));
73 }
74}
75
76/**
77 * We need this JSDoc comment for affecting ESDoc.
78 * @ignore
79 * @extends {Ignored}
80 */
81class IsEmptySubscriber extends Subscriber<any> {
82 constructor(destination: Subscriber<boolean>) {
83 super(destination);
84 }
85
86 private notifyComplete(isEmpty: boolean): void {
87 const destination = this.destination;
88
89 destination.next(isEmpty);
90 destination.complete();
91 }
92
93 protected _next(value: boolean) {
94 this.notifyComplete(false);
95 }
96
97 protected _complete() {
98 this.notifyComplete(true);
99 }
100}
Note: See TracBrowser for help on using the repository browser.