Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Notification.ts 4.8KB

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