EmptyObservable.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Observable } from '../Observable';
  2. /**
  3. * We need this JSDoc comment for affecting ESDoc.
  4. * @extends {Ignored}
  5. * @hide true
  6. */
  7. export class EmptyObservable extends Observable {
  8. constructor(scheduler) {
  9. super();
  10. this.scheduler = scheduler;
  11. }
  12. /**
  13. * Creates an Observable that emits no items to the Observer and immediately
  14. * emits a complete notification.
  15. *
  16. * <span class="informal">Just emits 'complete', and nothing else.
  17. * </span>
  18. *
  19. * <img src="./img/empty.png" width="100%">
  20. *
  21. * This static operator is useful for creating a simple Observable that only
  22. * emits the complete notification. It can be used for composing with other
  23. * Observables, such as in a {@link mergeMap}.
  24. *
  25. * @example <caption>Emit the number 7, then complete.</caption>
  26. * var result = Rx.Observable.empty().startWith(7);
  27. * result.subscribe(x => console.log(x));
  28. *
  29. * @example <caption>Map and flatten only odd numbers to the sequence 'a', 'b', 'c'</caption>
  30. * var interval = Rx.Observable.interval(1000);
  31. * var result = interval.mergeMap(x =>
  32. * x % 2 === 1 ? Rx.Observable.of('a', 'b', 'c') : Rx.Observable.empty()
  33. * );
  34. * result.subscribe(x => console.log(x));
  35. *
  36. * // Results in the following to the console:
  37. * // x is equal to the count on the interval eg(0,1,2,3,...)
  38. * // x will occur every 1000ms
  39. * // if x % 2 is equal to 1 print abc
  40. * // if x % 2 is not equal to 1 nothing will be output
  41. *
  42. * @see {@link create}
  43. * @see {@link never}
  44. * @see {@link of}
  45. * @see {@link throw}
  46. *
  47. * @param {Scheduler} [scheduler] A {@link IScheduler} to use for scheduling
  48. * the emission of the complete notification.
  49. * @return {Observable} An "empty" Observable: emits only the complete
  50. * notification.
  51. * @static true
  52. * @name empty
  53. * @owner Observable
  54. */
  55. static create(scheduler) {
  56. return new EmptyObservable(scheduler);
  57. }
  58. static dispatch(arg) {
  59. const { subscriber } = arg;
  60. subscriber.complete();
  61. }
  62. /** @deprecated internal use only */ _subscribe(subscriber) {
  63. const scheduler = this.scheduler;
  64. if (scheduler) {
  65. return scheduler.schedule(EmptyObservable.dispatch, 0, { subscriber });
  66. }
  67. else {
  68. subscriber.complete();
  69. }
  70. }
  71. }
  72. //# sourceMappingURL=EmptyObservable.js.map