From b28521df251b75168e33a17cd1aedb21f8b0f7fe Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Mon, 11 Feb 2019 19:35:31 +1000 Subject: [PATCH 1/2] fix(Notification): replace const enum Closes #4538 --- src/internal/Notification.ts | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/internal/Notification.ts b/src/internal/Notification.ts index 3ddb20d2c8..a2b7ff0916 100644 --- a/src/internal/Notification.ts +++ b/src/internal/Notification.ts @@ -4,11 +4,7 @@ import { empty } from './observable/empty'; import { of } from './observable/of'; import { throwError } from './observable/throwError'; -export const enum NotificationKind { - NEXT = 'N', - ERROR = 'E', - COMPLETE = 'C', -} +export type NotificationKind = 'N' | 'E' | 'C'; /** * Represents a push-based event or value that an {@link Observable} can emit. @@ -28,7 +24,7 @@ export class Notification { hasValue: boolean; constructor(public kind: NotificationKind, public value?: T, public error?: any) { - this.hasValue = kind === NotificationKind.NEXT; + this.hasValue = kind === 'N'; } /** @@ -38,11 +34,11 @@ export class Notification { */ observe(observer: PartialObserver): any { switch (this.kind) { - case NotificationKind.NEXT: + case 'N': return observer.next && observer.next(this.value); - case NotificationKind.ERROR: + case 'E': return observer.error && observer.error(this.error); - case NotificationKind.COMPLETE: + case 'C': return observer.complete && observer.complete(); } } @@ -58,11 +54,11 @@ export class Notification { do(next: (value: T) => void, error?: (err: any) => void, complete?: () => void): any { const kind = this.kind; switch (kind) { - case NotificationKind.NEXT: + case 'N': return next && next(this.value); - case NotificationKind.ERROR: + case 'E': return error && error(this.error); - case NotificationKind.COMPLETE: + case 'C': return complete && complete(); } } @@ -92,18 +88,18 @@ export class Notification { toObservable(): Observable { const kind = this.kind; switch (kind) { - case NotificationKind.NEXT: + case 'N': return of(this.value); - case NotificationKind.ERROR: + case 'E': return throwError(this.error); - case NotificationKind.COMPLETE: + case 'C': return empty(); } throw new Error('unexpected notification kind value'); } - private static completeNotification: Notification = new Notification(NotificationKind.COMPLETE); - private static undefinedValueNotification: Notification = new Notification(NotificationKind.NEXT, undefined); + private static completeNotification: Notification = new Notification('C'); + private static undefinedValueNotification: Notification = new Notification('N', undefined); /** * A shortcut to create a Notification instance of the type `next` from a @@ -115,7 +111,7 @@ export class Notification { */ static createNext(value: T): Notification { if (typeof value !== 'undefined') { - return new Notification(NotificationKind.NEXT, value); + return new Notification('N', value); } return Notification.undefinedValueNotification; } @@ -129,7 +125,7 @@ export class Notification { * @nocollapse */ static createError(err?: any): Notification { - return new Notification(NotificationKind.ERROR, undefined, err); + return new Notification('E', undefined, err); } /** From ca0afc0fbdd420c3330c669ea6a24512e1087cd9 Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Tue, 26 Mar 2019 12:05:39 +1000 Subject: [PATCH 2/2] 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. --- src/internal/Notification.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/internal/Notification.ts b/src/internal/Notification.ts index a2b7ff0916..f65f70cd64 100644 --- a/src/internal/Notification.ts +++ b/src/internal/Notification.ts @@ -3,8 +3,17 @@ import { Observable } from './Observable'; import { empty } from './observable/empty'; import { of } from './observable/of'; import { throwError } from './observable/throwError'; +import { deprecate } from 'util'; -export type NotificationKind = 'N' | 'E' | 'C'; +// 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', + ERROR = 'E', + COMPLETE = 'C', +} /** * Represents a push-based event or value that an {@link Observable} can emit. @@ -23,7 +32,7 @@ export type NotificationKind = 'N' | 'E' | 'C'; export class Notification { 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 === 'N'; }