Notification.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.observeNotification = exports.Notification = exports.NotificationKind = void 0;
  4. var empty_1 = require("./observable/empty");
  5. var of_1 = require("./observable/of");
  6. var throwError_1 = require("./observable/throwError");
  7. var isFunction_1 = require("./util/isFunction");
  8. var NotificationKind;
  9. (function (NotificationKind) {
  10. NotificationKind["NEXT"] = "N";
  11. NotificationKind["ERROR"] = "E";
  12. NotificationKind["COMPLETE"] = "C";
  13. })(NotificationKind = exports.NotificationKind || (exports.NotificationKind = {}));
  14. var Notification = (function () {
  15. function Notification(kind, value, error) {
  16. this.kind = kind;
  17. this.value = value;
  18. this.error = error;
  19. this.hasValue = kind === 'N';
  20. }
  21. Notification.prototype.observe = function (observer) {
  22. return observeNotification(this, observer);
  23. };
  24. Notification.prototype.do = function (nextHandler, errorHandler, completeHandler) {
  25. var _a = this, kind = _a.kind, value = _a.value, error = _a.error;
  26. return kind === 'N' ? nextHandler === null || nextHandler === void 0 ? void 0 : nextHandler(value) : kind === 'E' ? errorHandler === null || errorHandler === void 0 ? void 0 : errorHandler(error) : completeHandler === null || completeHandler === void 0 ? void 0 : completeHandler();
  27. };
  28. Notification.prototype.accept = function (nextOrObserver, error, complete) {
  29. var _a;
  30. return isFunction_1.isFunction((_a = nextOrObserver) === null || _a === void 0 ? void 0 : _a.next)
  31. ? this.observe(nextOrObserver)
  32. : this.do(nextOrObserver, error, complete);
  33. };
  34. Notification.prototype.toObservable = function () {
  35. var _a = this, kind = _a.kind, value = _a.value, error = _a.error;
  36. var result = kind === 'N'
  37. ?
  38. of_1.of(value)
  39. :
  40. kind === 'E'
  41. ?
  42. throwError_1.throwError(function () { return error; })
  43. :
  44. kind === 'C'
  45. ?
  46. empty_1.EMPTY
  47. :
  48. 0;
  49. if (!result) {
  50. throw new TypeError("Unexpected notification kind " + kind);
  51. }
  52. return result;
  53. };
  54. Notification.createNext = function (value) {
  55. return new Notification('N', value);
  56. };
  57. Notification.createError = function (err) {
  58. return new Notification('E', undefined, err);
  59. };
  60. Notification.createComplete = function () {
  61. return Notification.completeNotification;
  62. };
  63. Notification.completeNotification = new Notification('C');
  64. return Notification;
  65. }());
  66. exports.Notification = Notification;
  67. function observeNotification(notification, observer) {
  68. var _a, _b, _c;
  69. var _d = notification, kind = _d.kind, value = _d.value, error = _d.error;
  70. if (typeof kind !== 'string') {
  71. throw new TypeError('Invalid notification, missing "kind"');
  72. }
  73. kind === 'N' ? (_a = observer.next) === null || _a === void 0 ? void 0 : _a.call(observer, value) : kind === 'E' ? (_b = observer.error) === null || _b === void 0 ? void 0 : _b.call(observer, error) : (_c = observer.complete) === null || _c === void 0 ? void 0 : _c.call(observer);
  74. }
  75. exports.observeNotification = observeNotification;
  76. //# sourceMappingURL=Notification.js.map