a zip code crypto-currency system good for red ONLY

Notification.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. var Observable_1 = require('./Observable');
  3. /**
  4. * Represents a push-based event or value that an {@link Observable} can emit.
  5. * This class is particularly useful for operators that manage notifications,
  6. * like {@link materialize}, {@link dematerialize}, {@link observeOn}, and
  7. * others. Besides wrapping the actual delivered value, it also annotates it
  8. * with metadata of, for instance, what type of push message it is (`next`,
  9. * `error`, or `complete`).
  10. *
  11. * @see {@link materialize}
  12. * @see {@link dematerialize}
  13. * @see {@link observeOn}
  14. *
  15. * @class Notification<T>
  16. */
  17. var Notification = (function () {
  18. function Notification(kind, value, error) {
  19. this.kind = kind;
  20. this.value = value;
  21. this.error = error;
  22. this.hasValue = kind === 'N';
  23. }
  24. /**
  25. * Delivers to the given `observer` the value wrapped by this Notification.
  26. * @param {Observer} observer
  27. * @return
  28. */
  29. Notification.prototype.observe = function (observer) {
  30. switch (this.kind) {
  31. case 'N':
  32. return observer.next && observer.next(this.value);
  33. case 'E':
  34. return observer.error && observer.error(this.error);
  35. case 'C':
  36. return observer.complete && observer.complete();
  37. }
  38. };
  39. /**
  40. * Given some {@link Observer} callbacks, deliver the value represented by the
  41. * current Notification to the correctly corresponding callback.
  42. * @param {function(value: T): void} next An Observer `next` callback.
  43. * @param {function(err: any): void} [error] An Observer `error` callback.
  44. * @param {function(): void} [complete] An Observer `complete` callback.
  45. * @return {any}
  46. */
  47. Notification.prototype.do = function (next, error, complete) {
  48. var kind = this.kind;
  49. switch (kind) {
  50. case 'N':
  51. return next && next(this.value);
  52. case 'E':
  53. return error && error(this.error);
  54. case 'C':
  55. return complete && complete();
  56. }
  57. };
  58. /**
  59. * Takes an Observer or its individual callback functions, and calls `observe`
  60. * or `do` methods accordingly.
  61. * @param {Observer|function(value: T): void} nextOrObserver An Observer or
  62. * the `next` callback.
  63. * @param {function(err: any): void} [error] An Observer `error` callback.
  64. * @param {function(): void} [complete] An Observer `complete` callback.
  65. * @return {any}
  66. */
  67. Notification.prototype.accept = function (nextOrObserver, error, complete) {
  68. if (nextOrObserver && typeof nextOrObserver.next === 'function') {
  69. return this.observe(nextOrObserver);
  70. }
  71. else {
  72. return this.do(nextOrObserver, error, complete);
  73. }
  74. };
  75. /**
  76. * Returns a simple Observable that just delivers the notification represented
  77. * by this Notification instance.
  78. * @return {any}
  79. */
  80. Notification.prototype.toObservable = function () {
  81. var kind = this.kind;
  82. switch (kind) {
  83. case 'N':
  84. return Observable_1.Observable.of(this.value);
  85. case 'E':
  86. return Observable_1.Observable.throw(this.error);
  87. case 'C':
  88. return Observable_1.Observable.empty();
  89. }
  90. throw new Error('unexpected notification kind value');
  91. };
  92. /**
  93. * A shortcut to create a Notification instance of the type `next` from a
  94. * given value.
  95. * @param {T} value The `next` value.
  96. * @return {Notification<T>} The "next" Notification representing the
  97. * argument.
  98. */
  99. Notification.createNext = function (value) {
  100. if (typeof value !== 'undefined') {
  101. return new Notification('N', value);
  102. }
  103. return Notification.undefinedValueNotification;
  104. };
  105. /**
  106. * A shortcut to create a Notification instance of the type `error` from a
  107. * given error.
  108. * @param {any} [err] The `error` error.
  109. * @return {Notification<T>} The "error" Notification representing the
  110. * argument.
  111. */
  112. Notification.createError = function (err) {
  113. return new Notification('E', undefined, err);
  114. };
  115. /**
  116. * A shortcut to create a Notification instance of the type `complete`.
  117. * @return {Notification<any>} The valueless "complete" Notification.
  118. */
  119. Notification.createComplete = function () {
  120. return Notification.completeNotification;
  121. };
  122. Notification.completeNotification = new Notification('C');
  123. Notification.undefinedValueNotification = new Notification('N', undefined);
  124. return Notification;
  125. }());
  126. exports.Notification = Notification;
  127. //# sourceMappingURL=Notification.js.map