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';
8 // TODO: When this enum is removed, replace it with a type alias. See #4556.
10 * @deprecated NotificationKind is deprecated as const enums are not compatible with isolated modules. Use a string literal instead.
12 export enum NotificationKind {
19 * Represents a push-based event or value that an {@link Observable} can emit.
20 * This class is particularly useful for operators that manage notifications,
21 * like {@link materialize}, {@link dematerialize}, {@link observeOn}, and
22 * others. Besides wrapping the actual delivered value, it also annotates it
23 * with metadata of, for instance, what type of push message it is (`next`,
24 * `error`, or `complete`).
26 * @see {@link materialize}
27 * @see {@link dematerialize}
28 * @see {@link observeOn}
30 * @class Notification<T>
32 export class Notification<T> {
35 constructor(public kind: 'N' | 'E' | 'C', public value?: T, public error?: any) {
36 this.hasValue = kind === 'N';
40 * Delivers to the given `observer` the value wrapped by this Notification.
41 * @param {Observer} observer
44 observe(observer: PartialObserver<T>): any {
47 return observer.next && observer.next(this.value);
49 return observer.error && observer.error(this.error);
51 return observer.complete && observer.complete();
56 * Given some {@link Observer} callbacks, deliver the value represented by the
57 * current Notification to the correctly corresponding callback.
58 * @param {function(value: T): void} next An Observer `next` callback.
59 * @param {function(err: any): void} [error] An Observer `error` callback.
60 * @param {function(): void} [complete] An Observer `complete` callback.
63 do(next: (value: T) => void, error?: (err: any) => void, complete?: () => void): any {
64 const kind = this.kind;
67 return next && next(this.value);
69 return error && error(this.error);
71 return complete && complete();
76 * Takes an Observer or its individual callback functions, and calls `observe`
77 * or `do` methods accordingly.
78 * @param {Observer|function(value: T): void} nextOrObserver An Observer or
79 * the `next` callback.
80 * @param {function(err: any): void} [error] An Observer `error` callback.
81 * @param {function(): void} [complete] An Observer `complete` callback.
84 accept(nextOrObserver: PartialObserver<T> | ((value: T) => void), error?: (err: any) => void, complete?: () => void) {
85 if (nextOrObserver && typeof (<PartialObserver<T>>nextOrObserver).next === 'function') {
86 return this.observe(<PartialObserver<T>>nextOrObserver);
88 return this.do(<(value: T) => void>nextOrObserver, error, complete);
93 * Returns a simple Observable that just delivers the notification represented
94 * by this Notification instance.
97 toObservable(): Observable<T> {
98 const kind = this.kind;
101 return of(this.value);
103 return throwError(this.error);
107 throw new Error('unexpected notification kind value');
110 private static completeNotification: Notification<any> = new Notification('C');
111 private static undefinedValueNotification: Notification<any> = new Notification('N', undefined);
114 * A shortcut to create a Notification instance of the type `next` from a
116 * @param {T} value The `next` value.
117 * @return {Notification<T>} The "next" Notification representing the
121 static createNext<T>(value: T): Notification<T> {
122 if (typeof value !== 'undefined') {
123 return new Notification('N', value);
125 return Notification.undefinedValueNotification;
129 * A shortcut to create a Notification instance of the type `error` from a
131 * @param {any} [err] The `error` error.
132 * @return {Notification<T>} The "error" Notification representing the
136 static createError<T>(err?: any): Notification<T> {
137 return new Notification('E', undefined, err);
141 * A shortcut to create a Notification instance of the type `complete`.
142 * @return {Notification<any>} The valueless "complete" Notification.
145 static createComplete(): Notification<any> {
146 return Notification.completeNotification;