Skip to content

Commit e460eec

Browse files
cartantbenlesh
authored andcommittedApr 23, 2019
fix(Notification): replace const enum (#4556)
* fix(Notification): replace const enum Closes #4538 * chore: use literal union and keep enum The enum is kept, but it is no longer a const enum. It cannot be exported as a const enum without effecting an error if isolated modules are used.
1 parent 2aa666b commit e460eec

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed
 

‎src/internal/Notification.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import { Observable } from './Observable';
33
import { empty } from './observable/empty';
44
import { of } from './observable/of';
55
import { throwError } from './observable/throwError';
6+
import { deprecate } from 'util';
67

7-
export const enum NotificationKind {
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 {
813
NEXT = 'N',
914
ERROR = 'E',
1015
COMPLETE = 'C',
@@ -27,8 +32,8 @@ export const enum NotificationKind {
2732
export class Notification<T> {
2833
hasValue: boolean;
2934

30-
constructor(public kind: NotificationKind, public value?: T, public error?: any) {
31-
this.hasValue = kind === NotificationKind.NEXT;
35+
constructor(public kind: 'N' | 'E' | 'C', public value?: T, public error?: any) {
36+
this.hasValue = kind === 'N';
3237
}
3338

3439
/**
@@ -38,11 +43,11 @@ export class Notification<T> {
3843
*/
3944
observe(observer: PartialObserver<T>): any {
4045
switch (this.kind) {
41-
case NotificationKind.NEXT:
46+
case 'N':
4247
return observer.next && observer.next(this.value);
43-
case NotificationKind.ERROR:
48+
case 'E':
4449
return observer.error && observer.error(this.error);
45-
case NotificationKind.COMPLETE:
50+
case 'C':
4651
return observer.complete && observer.complete();
4752
}
4853
}
@@ -58,11 +63,11 @@ export class Notification<T> {
5863
do(next: (value: T) => void, error?: (err: any) => void, complete?: () => void): any {
5964
const kind = this.kind;
6065
switch (kind) {
61-
case NotificationKind.NEXT:
66+
case 'N':
6267
return next && next(this.value);
63-
case NotificationKind.ERROR:
68+
case 'E':
6469
return error && error(this.error);
65-
case NotificationKind.COMPLETE:
70+
case 'C':
6671
return complete && complete();
6772
}
6873
}
@@ -92,18 +97,18 @@ export class Notification<T> {
9297
toObservable(): Observable<T> {
9398
const kind = this.kind;
9499
switch (kind) {
95-
case NotificationKind.NEXT:
100+
case 'N':
96101
return of(this.value);
97-
case NotificationKind.ERROR:
102+
case 'E':
98103
return throwError(this.error);
99-
case NotificationKind.COMPLETE:
104+
case 'C':
100105
return empty();
101106
}
102107
throw new Error('unexpected notification kind value');
103108
}
104109

105-
private static completeNotification: Notification<any> = new Notification(NotificationKind.COMPLETE);
106-
private static undefinedValueNotification: Notification<any> = new Notification(NotificationKind.NEXT, undefined);
110+
private static completeNotification: Notification<any> = new Notification('C');
111+
private static undefinedValueNotification: Notification<any> = new Notification('N', undefined);
107112

108113
/**
109114
* A shortcut to create a Notification instance of the type `next` from a
@@ -115,7 +120,7 @@ export class Notification<T> {
115120
*/
116121
static createNext<T>(value: T): Notification<T> {
117122
if (typeof value !== 'undefined') {
118-
return new Notification(NotificationKind.NEXT, value);
123+
return new Notification('N', value);
119124
}
120125
return Notification.undefinedValueNotification;
121126
}
@@ -129,7 +134,7 @@ export class Notification<T> {
129134
* @nocollapse
130135
*/
131136
static createError<T>(err?: any): Notification<T> {
132-
return new Notification(NotificationKind.ERROR, undefined, err);
137+
return new Notification('E', undefined, err);
133138
}
134139

135140
/**

0 commit comments

Comments
 (0)
Please sign in to comment.