| 1 |
import { OperatorFunction } from '../types'; |
| 2 |
import { operate } from '../util/lift'; |
| 3 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 4 |
import { noop } from '../util/noop'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Ignores all items emitted by the source Observable and only passes calls of `complete` or `error`. |
| 8 |
* |
| 9 |
*  |
| 10 |
* |
| 11 |
* The `ignoreElements` operator suppresses all items emitted by the source Observable, |
| 12 |
* but allows its termination notification (either `error` or `complete`) to pass through unchanged. |
| 13 |
* |
| 14 |
* If you do not care about the items being emitted by an Observable, but you do want to be notified |
| 15 |
* when it completes or when it terminates with an error, you can apply the `ignoreElements` operator |
| 16 |
* to the Observable, which will ensure that it will never call its observers’ `next` handlers. |
| 17 |
* |
| 18 |
* ## Example |
| 19 |
* |
| 20 |
* Ignore all `next` emissions from the source |
| 21 |
* |
| 22 |
* ```ts |
| 23 |
* import { of, ignoreElements } from 'rxjs'; |
| 24 |
* |
| 25 |
* of('you', 'talking', 'to', 'me') |
| 26 |
* .pipe(ignoreElements()) |
| 27 |
* .subscribe({ |
| 28 |
* next: word => console.log(word), |
| 29 |
* error: err => console.log('error:', err), |
| 30 |
* complete: () => console.log('the end'), |
| 31 |
* }); |
| 32 |
* |
| 33 |
* // result: |
| 34 |
* // 'the end' |
| 35 |
* ``` |
| 36 |
* |
| 37 |
* @return A function that returns an empty Observable that only calls |
| 38 |
* `complete` or `error`, based on which one is called by the source |
| 39 |
* Observable. |
| 40 |
*/ |
| 41 |
export function ignoreElements(): OperatorFunction<unknown, never> { |
| 42 |
return operate((source, subscriber) => { |
| 43 |
source.subscribe(createOperatorSubscriber(subscriber, noop)); |
| 44 |
}); |
| 45 |
} |