Skip to content

Commit

Permalink
fix(Notification): replace const enum
Browse files Browse the repository at this point in the history
Closes #4538
  • Loading branch information
cartant committed Feb 11, 2019
1 parent 82482b2 commit 5d3a78c
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/internal/Notification.ts
Expand Up @@ -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.
Expand All @@ -28,7 +24,7 @@ export class Notification<T> {
hasValue: boolean;

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

/**
Expand All @@ -38,11 +34,11 @@ export class Notification<T> {
*/
observe(observer: PartialObserver<T>): 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();
}
}
Expand All @@ -58,11 +54,11 @@ export class Notification<T> {
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();
}
}
Expand Down Expand Up @@ -92,18 +88,18 @@ export class Notification<T> {
toObservable(): Observable<T> {
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<any> = new Notification(NotificationKind.COMPLETE);
private static undefinedValueNotification: Notification<any> = new Notification(NotificationKind.NEXT, undefined);
private static completeNotification: Notification<any> = new Notification('C');
private static undefinedValueNotification: Notification<any> = new Notification('N', undefined);

/**
* A shortcut to create a Notification instance of the type `next` from a
Expand All @@ -115,7 +111,7 @@ export class Notification<T> {
*/
static createNext<T>(value: T): Notification<T> {
if (typeof value !== 'undefined') {
return new Notification(NotificationKind.NEXT, value);
return new Notification('N', value);
}
return Notification.undefinedValueNotification;
}
Expand All @@ -129,7 +125,7 @@ export class Notification<T> {
* @nocollapse
*/
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 5d3a78c

Please sign in to comment.