defaultIfEmpty.js 2.9KB

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