defaultIfEmpty.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /* tslint:enable:max-line-length */
  5. /**
  6. * Emits a given value if the source Observable completes without emitting any
  7. * `next` value, otherwise mirrors the source Observable.
  8. *
  9. * <span class="informal">If the source Observable turns out to be empty, then
  10. * this operator will emit a default value.</span>
  11. *
  12. * <img src="./img/defaultIfEmpty.png" width="100%">
  13. *
  14. * `defaultIfEmpty` emits the values emitted by the source Observable or a
  15. * specified default value if the source Observable is empty (completes without
  16. * having emitted any `next` value).
  17. *
  18. * @example <caption>If no clicks happen in 5 seconds, then emit "no clicks"</caption>
  19. * var clicks = Rx.Observable.fromEvent(document, 'click');
  20. * var clicksBeforeFive = clicks.takeUntil(Rx.Observable.interval(5000));
  21. * var result = clicksBeforeFive.defaultIfEmpty('no clicks');
  22. * result.subscribe(x => console.log(x));
  23. *
  24. * @see {@link empty}
  25. * @see {@link last}
  26. *
  27. * @param {any} [defaultValue=null] The default value used if the source
  28. * Observable is empty.
  29. * @return {Observable} An Observable that emits either the specified
  30. * `defaultValue` if the source Observable emits no items, or the values emitted
  31. * by the source Observable.
  32. * @method defaultIfEmpty
  33. * @owner Observable
  34. */
  35. function defaultIfEmpty(defaultValue) {
  36. if (defaultValue === void 0) { defaultValue = null; }
  37. return operators_1.defaultIfEmpty(defaultValue)(this);
  38. }
  39. exports.defaultIfEmpty = defaultIfEmpty;
  40. //# sourceMappingURL=defaultIfEmpty.js.map