088fa9cf7c6db01bf3539551d39cbf199d5bea70186af6293b0d1a69f3032a9bd0dbbbd98408a64ea5d905c560cda0e08fba02c02626f66bfac4f202398b10 1.5 KB

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