Notification.js 4.3 KB

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