2b31026dd6f33ea31ee89ba9733e19224ff99e6705fb3aadcb8c68b86614edf5dedd56a5fad1c3f60c2f0469f6194522294949751260825973a02f229bc62d 1.5 KB

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