Notification.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { PartialObserver } from './Observer';
  2. import { Observable } from './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. export class Notification<T> {
  18. hasValue: boolean;
  19. constructor(public kind: string, public value?: T, public error?: any) {
  20. this.hasValue = kind === 'N';
  21. }
  22. /**
  23. * Delivers to the given `observer` the value wrapped by this Notification.
  24. * @param {Observer} observer
  25. * @return
  26. */
  27. observe(observer: PartialObserver<T>): any {
  28. switch (this.kind) {
  29. case 'N':
  30. return observer.next && observer.next(this.value);
  31. case 'E':
  32. return observer.error && observer.error(this.error);
  33. case 'C':
  34. return observer.complete && observer.complete();
  35. }
  36. }
  37. /**
  38. * Given some {@link Observer} callbacks, deliver the value represented by the
  39. * current Notification to the correctly corresponding callback.
  40. * @param {function(value: T): void} next An Observer `next` callback.
  41. * @param {function(err: any): void} [error] An Observer `error` callback.
  42. * @param {function(): void} [complete] An Observer `complete` callback.
  43. * @return {any}
  44. */
  45. do(next: (value: T) => void, error?: (err: any) => void, complete?: () => void): any {
  46. const kind = this.kind;
  47. switch (kind) {
  48. case 'N':
  49. return next && next(this.value);
  50. case 'E':
  51. return error && error(this.error);
  52. case 'C':
  53. return complete && complete();
  54. }
  55. }
  56. /**
  57. * Takes an Observer or its individual callback functions, and calls `observe`
  58. * or `do` methods accordingly.
  59. * @param {Observer|function(value: T): void} nextOrObserver An Observer or
  60. * the `next` callback.
  61. * @param {function(err: any): void} [error] An Observer `error` callback.
  62. * @param {function(): void} [complete] An Observer `complete` callback.
  63. * @return {any}
  64. */
  65. accept(nextOrObserver: PartialObserver<T> | ((value: T) => void), error?: (err: any) => void, complete?: () => void) {
  66. if (nextOrObserver && typeof (<PartialObserver<T>>nextOrObserver).next === 'function') {
  67. return this.observe(<PartialObserver<T>>nextOrObserver);
  68. } else {
  69. return this.do(<(value: T) => void>nextOrObserver, error, complete);
  70. }
  71. }
  72. /**
  73. * Returns a simple Observable that just delivers the notification represented
  74. * by this Notification instance.
  75. * @return {any}
  76. */
  77. toObservable(): Observable<T> {
  78. const kind = this.kind;
  79. switch (kind) {
  80. case 'N':
  81. return Observable.of(this.value);
  82. case 'E':
  83. return Observable.throw(this.error);
  84. case 'C':
  85. return Observable.empty<T>();
  86. }
  87. throw new Error('unexpected notification kind value');
  88. }
  89. private static completeNotification: Notification<any> = new Notification('C');
  90. private static undefinedValueNotification: Notification<any> = new Notification('N', undefined);
  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<T>(value: T): Notification<T> {
  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<T>(err?: any): Notification<T> {
  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(): Notification<any> {
  119. return Notification.completeNotification;
  120. }
  121. }