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 |
|
---|
8 | // TODO: When this enum is removed, replace it with a type alias. See #4556.
|
---|
9 | /**
|
---|
10 | * @deprecated NotificationKind is deprecated as const enums are not compatible with isolated modules. Use a string literal instead.
|
---|
11 | */
|
---|
12 | export enum NotificationKind {
|
---|
13 | NEXT = 'N',
|
---|
14 | ERROR = 'E',
|
---|
15 | COMPLETE = 'C',
|
---|
16 | }
|
---|
17 |
|
---|
18 | /**
|
---|
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`).
|
---|
25 | *
|
---|
26 | * @see {@link materialize}
|
---|
27 | * @see {@link dematerialize}
|
---|
28 | * @see {@link observeOn}
|
---|
29 | *
|
---|
30 | * @class Notification<T>
|
---|
31 | */
|
---|
32 | export class Notification<T> {
|
---|
33 | hasValue: boolean;
|
---|
34 |
|
---|
35 | constructor(public kind: 'N' | 'E' | 'C', public value?: T, public error?: any) {
|
---|
36 | this.hasValue = kind === 'N';
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Delivers to the given `observer` the value wrapped by this Notification.
|
---|
41 | * @param {Observer} observer
|
---|
42 | * @return
|
---|
43 | */
|
---|
44 | observe(observer: PartialObserver<T>): any {
|
---|
45 | switch (this.kind) {
|
---|
46 | case 'N':
|
---|
47 | return observer.next && observer.next(this.value);
|
---|
48 | case 'E':
|
---|
49 | return observer.error && observer.error(this.error);
|
---|
50 | case 'C':
|
---|
51 | return observer.complete && observer.complete();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
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.
|
---|
61 | * @return {any}
|
---|
62 | */
|
---|
63 | do(next: (value: T) => void, error?: (err: any) => void, complete?: () => void): any {
|
---|
64 | const kind = this.kind;
|
---|
65 | switch (kind) {
|
---|
66 | case 'N':
|
---|
67 | return next && next(this.value);
|
---|
68 | case 'E':
|
---|
69 | return error && error(this.error);
|
---|
70 | case 'C':
|
---|
71 | return complete && complete();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
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.
|
---|
82 | * @return {any}
|
---|
83 | */
|
---|
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);
|
---|
87 | } else {
|
---|
88 | return this.do(<(value: T) => void>nextOrObserver, error, complete);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Returns a simple Observable that just delivers the notification represented
|
---|
94 | * by this Notification instance.
|
---|
95 | * @return {any}
|
---|
96 | */
|
---|
97 | toObservable(): Observable<T> {
|
---|
98 | const kind = this.kind;
|
---|
99 | switch (kind) {
|
---|
100 | case 'N':
|
---|
101 | return of(this.value);
|
---|
102 | case 'E':
|
---|
103 | return throwError(this.error);
|
---|
104 | case 'C':
|
---|
105 | return empty();
|
---|
106 | }
|
---|
107 | throw new Error('unexpected notification kind value');
|
---|
108 | }
|
---|
109 |
|
---|
110 | private static completeNotification: Notification<any> = new Notification('C');
|
---|
111 | private static undefinedValueNotification: Notification<any> = new Notification('N', undefined);
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * A shortcut to create a Notification instance of the type `next` from a
|
---|
115 | * given value.
|
---|
116 | * @param {T} value The `next` value.
|
---|
117 | * @return {Notification<T>} The "next" Notification representing the
|
---|
118 | * argument.
|
---|
119 | * @nocollapse
|
---|
120 | */
|
---|
121 | static createNext<T>(value: T): Notification<T> {
|
---|
122 | if (typeof value !== 'undefined') {
|
---|
123 | return new Notification('N', value);
|
---|
124 | }
|
---|
125 | return Notification.undefinedValueNotification;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * A shortcut to create a Notification instance of the type `error` from a
|
---|
130 | * given error.
|
---|
131 | * @param {any} [err] The `error` error.
|
---|
132 | * @return {Notification<T>} The "error" Notification representing the
|
---|
133 | * argument.
|
---|
134 | * @nocollapse
|
---|
135 | */
|
---|
136 | static createError<T>(err?: any): Notification<T> {
|
---|
137 | return new Notification('E', undefined, err);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * A shortcut to create a Notification instance of the type `complete`.
|
---|
142 | * @return {Notification<any>} The valueless "complete" Notification.
|
---|
143 | * @nocollapse
|
---|
144 | */
|
---|
145 | static createComplete(): Notification<any> {
|
---|
146 | return Notification.completeNotification;
|
---|
147 | }
|
---|
148 | }
|
---|