defaultIfEmpty.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /** PURE_IMPORTS_START tslib,_Subscriber PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. export function defaultIfEmpty(defaultValue) {
  5. if (defaultValue === void 0) {
  6. defaultValue = null;
  7. }
  8. return function (source) { return source.lift(new DefaultIfEmptyOperator(defaultValue)); };
  9. }
  10. var DefaultIfEmptyOperator = /*@__PURE__*/ (function () {
  11. function DefaultIfEmptyOperator(defaultValue) {
  12. this.defaultValue = defaultValue;
  13. }
  14. DefaultIfEmptyOperator.prototype.call = function (subscriber, source) {
  15. return source.subscribe(new DefaultIfEmptySubscriber(subscriber, this.defaultValue));
  16. };
  17. return DefaultIfEmptyOperator;
  18. }());
  19. var DefaultIfEmptySubscriber = /*@__PURE__*/ (function (_super) {
  20. tslib_1.__extends(DefaultIfEmptySubscriber, _super);
  21. function DefaultIfEmptySubscriber(destination, defaultValue) {
  22. var _this = _super.call(this, destination) || this;
  23. _this.defaultValue = defaultValue;
  24. _this.isEmpty = true;
  25. return _this;
  26. }
  27. DefaultIfEmptySubscriber.prototype._next = function (value) {
  28. this.isEmpty = false;
  29. this.destination.next(value);
  30. };
  31. DefaultIfEmptySubscriber.prototype._complete = function () {
  32. if (this.isEmpty) {
  33. this.destination.next(this.defaultValue);
  34. }
  35. this.destination.complete();
  36. };
  37. return DefaultIfEmptySubscriber;
  38. }(Subscriber));
  39. //# sourceMappingURL=defaultIfEmpty.js.map