Notification.js 3.1 KB

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