1 | import { Observable } from '../Observable';
|
---|
2 | import { Operator } from '../Operator';
|
---|
3 | import { Subscriber } from '../Subscriber';
|
---|
4 | import { OperatorFunction } from '../types';
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Ignores all items emitted by the source Observable and only passes calls of `complete` or `error`.
|
---|
8 | *
|
---|
9 | * ![](ignoreElements.png)
|
---|
10 | *
|
---|
11 | * ## Examples
|
---|
12 | * ### Ignores emitted values, reacts to observable's completion.
|
---|
13 | * ```ts
|
---|
14 | * import { of } from 'rxjs';
|
---|
15 | * import { ignoreElements } from 'rxjs/operators';
|
---|
16 | *
|
---|
17 | * of('you', 'talking', 'to', 'me').pipe(
|
---|
18 | * ignoreElements(),
|
---|
19 | * )
|
---|
20 | * .subscribe(
|
---|
21 | * word => console.log(word),
|
---|
22 | * err => console.log('error:', err),
|
---|
23 | * () => console.log('the end'),
|
---|
24 | * );
|
---|
25 | * // result:
|
---|
26 | * // 'the end'
|
---|
27 | * ```
|
---|
28 | * @return {Observable} An empty Observable that only calls `complete`
|
---|
29 | * or `error`, based on which one is called by the source Observable.
|
---|
30 | * @method ignoreElements
|
---|
31 | * @owner Observable
|
---|
32 | */
|
---|
33 | export function ignoreElements(): OperatorFunction<any, never> {
|
---|
34 | return function ignoreElementsOperatorFunction(source: Observable<any>) {
|
---|
35 | return source.lift(new IgnoreElementsOperator());
|
---|
36 | };
|
---|
37 | }
|
---|
38 |
|
---|
39 | class IgnoreElementsOperator<T, R> implements Operator<T, R> {
|
---|
40 | call(subscriber: Subscriber<R>, source: any): any {
|
---|
41 | return source.subscribe(new IgnoreElementsSubscriber(subscriber));
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * We need this JSDoc comment for affecting ESDoc.
|
---|
47 | * @ignore
|
---|
48 | * @extends {Ignored}
|
---|
49 | */
|
---|
50 | class IgnoreElementsSubscriber<T> extends Subscriber<T> {
|
---|
51 | protected _next(unused: T): void {
|
---|
52 | // Do nothing
|
---|
53 | }
|
---|
54 | }
|
---|