a zip code crypto-currency system good for red ONLY

defaultIfEmpty.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Subscriber } from '../Subscriber';
  2. /* tslint:enable:max-line-length */
  3. /**
  4. * Emits a given value if the source Observable completes without emitting any
  5. * `next` value, otherwise mirrors the source Observable.
  6. *
  7. * <span class="informal">If the source Observable turns out to be empty, then
  8. * this operator will emit a default value.</span>
  9. *
  10. * <img src="./img/defaultIfEmpty.png" width="100%">
  11. *
  12. * `defaultIfEmpty` emits the values emitted by the source Observable or a
  13. * specified default value if the source Observable is empty (completes without
  14. * having emitted any `next` value).
  15. *
  16. * @example <caption>If no clicks happen in 5 seconds, then emit "no clicks"</caption>
  17. * var clicks = Rx.Observable.fromEvent(document, 'click');
  18. * var clicksBeforeFive = clicks.takeUntil(Rx.Observable.interval(5000));
  19. * var result = clicksBeforeFive.defaultIfEmpty('no clicks');
  20. * result.subscribe(x => console.log(x));
  21. *
  22. * @see {@link empty}
  23. * @see {@link last}
  24. *
  25. * @param {any} [defaultValue=null] The default value used if the source
  26. * Observable is empty.
  27. * @return {Observable} An Observable that emits either the specified
  28. * `defaultValue` if the source Observable emits no items, or the values emitted
  29. * by the source Observable.
  30. * @method defaultIfEmpty
  31. * @owner Observable
  32. */
  33. export function defaultIfEmpty(defaultValue = null) {
  34. return (source) => source.lift(new DefaultIfEmptyOperator(defaultValue));
  35. }
  36. class DefaultIfEmptyOperator {
  37. constructor(defaultValue) {
  38. this.defaultValue = defaultValue;
  39. }
  40. call(subscriber, source) {
  41. return source.subscribe(new DefaultIfEmptySubscriber(subscriber, this.defaultValue));
  42. }
  43. }
  44. /**
  45. * We need this JSDoc comment for affecting ESDoc.
  46. * @ignore
  47. * @extends {Ignored}
  48. */
  49. class DefaultIfEmptySubscriber extends Subscriber {
  50. constructor(destination, defaultValue) {
  51. super(destination);
  52. this.defaultValue = defaultValue;
  53. this.isEmpty = true;
  54. }
  55. _next(value) {
  56. this.isEmpty = false;
  57. this.destination.next(value);
  58. }
  59. _complete() {
  60. if (this.isEmpty) {
  61. this.destination.next(this.defaultValue);
  62. }
  63. this.destination.complete();
  64. }
  65. }
  66. //# sourceMappingURL=defaultIfEmpty.js.map