Skip to content

Commit

Permalink
fix(Notification): replace const enum (#4556)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
cartant authored and benlesh committed Apr 23, 2019
1 parent 2aa666b commit e460eec
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/internal/Notification.ts
Expand Up @@ -3,8 +3,13 @@ import { Observable } from './Observable';
import { empty } from './observable/empty'; import { empty } from './observable/empty';
import { of } from './observable/of'; import { of } from './observable/of';
import { throwError } from './observable/throwError'; import { throwError } from './observable/throwError';
import { deprecate } from 'util';


export const enum NotificationKind { // TODO: When this enum is removed, replace it with a type alias. See #4556.
/**
* @deprecated NotificationKind is deprecated as const enums are not compatible with isolated modules. Use a string literal instead.
*/
export enum NotificationKind {
NEXT = 'N', NEXT = 'N',
ERROR = 'E', ERROR = 'E',
COMPLETE = 'C', COMPLETE = 'C',
Expand All @@ -27,8 +32,8 @@ export const enum NotificationKind {
export class Notification<T> { export class Notification<T> {
hasValue: boolean; hasValue: boolean;


constructor(public kind: NotificationKind, public value?: T, public error?: any) { constructor(public kind: 'N' | 'E' | 'C', public value?: T, public error?: any) {
this.hasValue = kind === NotificationKind.NEXT; this.hasValue = kind === 'N';
} }


/** /**
Expand All @@ -38,11 +43,11 @@ export class Notification<T> {
*/ */
observe(observer: PartialObserver<T>): any { observe(observer: PartialObserver<T>): any {
switch (this.kind) { switch (this.kind) {
case NotificationKind.NEXT: case 'N':
return observer.next && observer.next(this.value); return observer.next && observer.next(this.value);
case NotificationKind.ERROR: case 'E':
return observer.error && observer.error(this.error); return observer.error && observer.error(this.error);
case NotificationKind.COMPLETE: case 'C':
return observer.complete && observer.complete(); return observer.complete && observer.complete();
} }
} }
Expand All @@ -58,11 +63,11 @@ export class Notification<T> {
do(next: (value: T) => void, error?: (err: any) => void, complete?: () => void): any { do(next: (value: T) => void, error?: (err: any) => void, complete?: () => void): any {
const kind = this.kind; const kind = this.kind;
switch (kind) { switch (kind) {
case NotificationKind.NEXT: case 'N':
return next && next(this.value); return next && next(this.value);
case NotificationKind.ERROR: case 'E':
return error && error(this.error); return error && error(this.error);
case NotificationKind.COMPLETE: case 'C':
return complete && complete(); return complete && complete();
} }
} }
Expand Down Expand Up @@ -92,18 +97,18 @@ export class Notification<T> {
toObservable(): Observable<T> { toObservable(): Observable<T> {
const kind = this.kind; const kind = this.kind;
switch (kind) { switch (kind) {
case NotificationKind.NEXT: case 'N':
return of(this.value); return of(this.value);
case NotificationKind.ERROR: case 'E':
return throwError(this.error); return throwError(this.error);
case NotificationKind.COMPLETE: case 'C':
return empty(); return empty();
} }
throw new Error('unexpected notification kind value'); throw new Error('unexpected notification kind value');
} }


private static completeNotification: Notification<any> = new Notification(NotificationKind.COMPLETE); private static completeNotification: Notification<any> = new Notification('C');
private static undefinedValueNotification: Notification<any> = new Notification(NotificationKind.NEXT, undefined); private static undefinedValueNotification: Notification<any> = new Notification('N', undefined);


/** /**
* A shortcut to create a Notification instance of the type `next` from a * A shortcut to create a Notification instance of the type `next` from a
Expand All @@ -115,7 +120,7 @@ export class Notification<T> {
*/ */
static createNext<T>(value: T): Notification<T> { static createNext<T>(value: T): Notification<T> {
if (typeof value !== 'undefined') { if (typeof value !== 'undefined') {
return new Notification(NotificationKind.NEXT, value); return new Notification('N', value);
} }
return Notification.undefinedValueNotification; return Notification.undefinedValueNotification;
} }
Expand All @@ -129,7 +134,7 @@ export class Notification<T> {
* @nocollapse * @nocollapse
*/ */
static createError<T>(err?: any): Notification<T> { static createError<T>(err?: any): Notification<T> {
return new Notification(NotificationKind.ERROR, undefined, err); return new Notification('E', undefined, err);
} }


/** /**
Expand Down

0 comments on commit e460eec

Please sign in to comment.