Debian Codegraph @

node/rxjs/src/internal/Notification.ts

raw
1 import { PartialObserver, ObservableNotification, CompleteNotification, NextNotification, ErrorNotification } 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 { isFunction } from './util/isFunction';
7
8 // TODO: When this enum is removed, replace it with a type alias. See #4556.
9 /**
10 * @deprecated Use a string literal instead. `NotificationKind` will be replaced with a type alias in v8.
11 * It will not be replaced with a const enum as those are not compatible with isolated modules.
12 */
13 export enum NotificationKind {
14 NEXT = 'N',
15 ERROR = 'E',
16 COMPLETE = 'C',
17 }
18
19 /**
20 * Represents a push-based event or value that an {@link Observable} can emit.
21 * This class is particularly useful for operators that manage notifications,
22 * like {@link materialize}, {@link dematerialize}, {@link observeOn}, and
23 * others. Besides wrapping the actual delivered value, it also annotates it
24 * with metadata of, for instance, what type of push message it is (`next`,
25 * `error`, or `complete`).
26 *
27 * @see {@link materialize}
28 * @see {@link dematerialize}
29 * @see {@link observeOn}
30 * @deprecated It is NOT recommended to create instances of `Notification` directly.
31 * Rather, try to create POJOs matching the signature outlined in {@link ObservableNotification}.
32 * For example: `{ kind: 'N', value: 1 }`, `{ kind: 'E', error: new Error('bad') }`, or `{ kind: 'C' }`.
33 * Will be removed in v8.
34 */
35 export class Notification<T> {
36 /**
37 * A value signifying that the notification will "next" if observed. In truth,
38 * This is really synonymous with just checking `kind === "N"`.
39 * @deprecated Will be removed in v8. Instead, just check to see if the value of `kind` is `"N"`.
40 */
41 readonly hasValue: boolean;
42
43 /**
44 * Creates a "Next" notification object.
45 * @param kind Always `'N'`
46 * @param value The value to notify with if observed.
47 * @deprecated Internal implementation detail. Use {@link Notification#createNext createNext} instead.
48 */
49 constructor(kind: 'N', value?: T);
50 /**
51 * Creates an "Error" notification object.
52 * @param kind Always `'E'`
53 * @param value Always `undefined`
54 * @param error The error to notify with if observed.
55 * @deprecated Internal implementation detail. Use {@link Notification#createError createError} instead.
56 */
57 constructor(kind: 'E', value: undefined, error: any);
58 /**
59 * Creates a "completion" notification object.
60 * @param kind Always `'C'`
61 * @deprecated Internal implementation detail. Use {@link Notification#createComplete createComplete} instead.
62 */
63 constructor(kind: 'C');
64 constructor(public readonly kind: 'N' | 'E' | 'C', public readonly value?: T, public readonly error?: any) {
65 this.hasValue = kind === 'N';
66 }
67
68 /**
69 * Executes the appropriate handler on a passed `observer` given the `kind` of notification.
70 * If the handler is missing it will do nothing. Even if the notification is an error, if
71 * there is no error handler on the observer, an error will not be thrown, it will noop.
72 * @param observer The observer to notify.
73 */
74 observe(observer: PartialObserver<T>): void {
75 return observeNotification(this as ObservableNotification<T>, observer);
76 }
77
78 /**
79 * Executes a notification on the appropriate handler from a list provided.
80 * If a handler is missing for the kind of notification, nothing is called
81 * and no error is thrown, it will be a noop.
82 * @param next A next handler
83 * @param error An error handler
84 * @param complete A complete handler
85 * @deprecated Replaced with {@link Notification#observe observe}. Will be removed in v8.
86 */
87 do(next: (value: T) => void, error: (err: any) => void, complete: () => void): void;
88 /**
89 * Executes a notification on the appropriate handler from a list provided.
90 * If a handler is missing for the kind of notification, nothing is called
91 * and no error is thrown, it will be a noop.
92 * @param next A next handler
93 * @param error An error handler
94 * @deprecated Replaced with {@link Notification#observe observe}. Will be removed in v8.
95 */
96 do(next: (value: T) => void, error: (err: any) => void): void;
97 /**
98 * Executes the next handler if the Notification is of `kind` `"N"`. Otherwise
99 * this will not error, and it will be a noop.
100 * @param next The next handler
101 * @deprecated Replaced with {@link Notification#observe observe}. Will be removed in v8.
102 */
103 do(next: (value: T) => void): void;
104 do(nextHandler: (value: T) => void, errorHandler?: (err: any) => void, completeHandler?: () => void): void {
105 const { kind, value, error } = this;
106 return kind === 'N' ? nextHandler?.(value!) : kind === 'E' ? errorHandler?.(error) : completeHandler?.();
107 }
108
109 /**
110 * Executes a notification on the appropriate handler from a list provided.
111 * If a handler is missing for the kind of notification, nothing is called
112 * and no error is thrown, it will be a noop.
113 * @param next A next handler
114 * @param error An error handler
115 * @param complete A complete handler
116 * @deprecated Replaced with {@link Notification#observe observe}. Will be removed in v8.
117 */
118 accept(next: (value: T) => void, error: (err: any) => void, complete: () => void): void;
119 /**
120 * Executes a notification on the appropriate handler from a list provided.
121 * If a handler is missing for the kind of notification, nothing is called
122 * and no error is thrown, it will be a noop.
123 * @param next A next handler
124 * @param error An error handler
125 * @deprecated Replaced with {@link Notification#observe observe}. Will be removed in v8.
126 */
127 accept(next: (value: T) => void, error: (err: any) => void): void;
128 /**
129 * Executes the next handler if the Notification is of `kind` `"N"`. Otherwise
130 * this will not error, and it will be a noop.
131 * @param next The next handler
132 * @deprecated Replaced with {@link Notification#observe observe}. Will be removed in v8.
133 */
134 accept(next: (value: T) => void): void;
135
136 /**
137 * Executes the appropriate handler on a passed `observer` given the `kind` of notification.
138 * If the handler is missing it will do nothing. Even if the notification is an error, if
139 * there is no error handler on the observer, an error will not be thrown, it will noop.
140 * @param observer The observer to notify.
141 * @deprecated Replaced with {@link Notification#observe observe}. Will be removed in v8.
142 */
143 accept(observer: PartialObserver<T>): void;
144 accept(nextOrObserver: PartialObserver<T> | ((value: T) => void), error?: (err: any) => void, complete?: () => void) {
145 return isFunction((nextOrObserver as any)?.next)
146 ? this.observe(nextOrObserver as PartialObserver<T>)
147 : this.do(nextOrObserver as (value: T) => void, error as any, complete as any);
148 }
149
150 /**
151 * Returns a simple Observable that just delivers the notification represented
152 * by this Notification instance.
153 *
154 * @deprecated Will be removed in v8. To convert a `Notification` to an {@link Observable},
155 * use {@link of} and {@link dematerialize}: `of(notification).pipe(dematerialize())`.
156 */
157 toObservable(): Observable<T> {
158 const { kind, value, error } = this;
159 // Select the observable to return by `kind`
160 const result =
161 kind === 'N'
162 ? // Next kind. Return an observable of that value.
163 of(value!)
164 : //
165 kind === 'E'
166 ? // Error kind. Return an observable that emits the error.
167 throwError(() => error)
168 : //
169 kind === 'C'
170 ? // Completion kind. Kind is "C", return an observable that just completes.
171 EMPTY
172 : // Unknown kind, return falsy, so we error below.
173 0;
174 if (!result) {
175 // TODO: consider removing this check. The only way to cause this would be to
176 // use the Notification constructor directly in a way that is not type-safe.
177 // and direct use of the Notification constructor is deprecated.
178 throw new TypeError(`Unexpected notification kind ${kind}`);
179 }
180 return result;
181 }
182
183 private static completeNotification = new Notification('C') as Notification<never> & CompleteNotification;
184 /**
185 * A shortcut to create a Notification instance of the type `next` from a
186 * given value.
187 * @param value The `next` value.
188 * @return The "next" Notification representing the argument.
189 * @deprecated It is NOT recommended to create instances of `Notification` directly.
190 * Rather, try to create POJOs matching the signature outlined in {@link ObservableNotification}.
191 * For example: `{ kind: 'N', value: 1 }`, `{ kind: 'E', error: new Error('bad') }`, or `{ kind: 'C' }`.
192 * Will be removed in v8.
193 */
194 static createNext<T>(value: T) {
195 return new Notification('N', value) as Notification<T> & NextNotification<T>;
196 }
197
198 /**
199 * A shortcut to create a Notification instance of the type `error` from a
200 * given error.
201 * @param err The `error` error.
202 * @return The "error" Notification representing the argument.
203 * @deprecated It is NOT recommended to create instances of `Notification` directly.
204 * Rather, try to create POJOs matching the signature outlined in {@link ObservableNotification}.
205 * For example: `{ kind: 'N', value: 1 }`, `{ kind: 'E', error: new Error('bad') }`, or `{ kind: 'C' }`.
206 * Will be removed in v8.
207 */
208 static createError(err?: any) {
209 return new Notification('E', undefined, err) as Notification<never> & ErrorNotification;
210 }
211
212 /**
213 * A shortcut to create a Notification instance of the type `complete`.
214 * @return The valueless "complete" Notification.
215 * @deprecated It is NOT recommended to create instances of `Notification` directly.
216 * Rather, try to create POJOs matching the signature outlined in {@link ObservableNotification}.
217 * For example: `{ kind: 'N', value: 1 }`, `{ kind: 'E', error: new Error('bad') }`, or `{ kind: 'C' }`.
218 * Will be removed in v8.
219 */
220 static createComplete(): Notification<never> & CompleteNotification {
221 return Notification.completeNotification;
222 }
223 }
224
225 /**
226 * Executes the appropriate handler on a passed `observer` given the `kind` of notification.
227 * If the handler is missing it will do nothing. Even if the notification is an error, if
228 * there is no error handler on the observer, an error will not be thrown, it will noop.
229 * @param notification The notification object to observe.
230 * @param observer The observer to notify.
231 */
232 export function observeNotification<T>(notification: ObservableNotification<T>, observer: PartialObserver<T>) {
233 const { kind, value, error } = notification as any;
234 if (typeof kind !== 'string') {
235 throw new TypeError('Invalid notification, missing "kind"');
236 }
237 kind === 'N' ? observer.next?.(value!) : kind === 'E' ? observer.error?.(error) : observer.complete?.();
238 }