Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style(Notification): Use enum to safely type the notification kinds #4405

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/Notification-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Notification', () => {
});

it('should not allow convert to observable if given kind is unknown', () => {
const n = new Notification('x');
const n = new Notification('x' as any);
expect(() => n.toObservable()).to.throw();
});

Expand Down
36 changes: 21 additions & 15 deletions src/internal/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ 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',
}

/**
* Represents a push-based event or value that an {@link Observable} can emit.
* This class is particularly useful for operators that manage notifications,
Expand All @@ -21,8 +27,8 @@ import { throwError } from './observable/throwError';
export class Notification<T> {
hasValue: boolean;

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

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

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

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/internal/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export {AsyncSubject} from './AsyncSubject';
export {ReplaySubject} from './ReplaySubject';
export {BehaviorSubject} from './BehaviorSubject';
export {ConnectableObservable} from './observable/ConnectableObservable';
export {Notification} from './Notification';
export {Notification, NotificationKind} from './Notification';
export {EmptyError} from './util/EmptyError';
export {ArgumentOutOfRangeError} from './util/ArgumentOutOfRangeError';
export {ObjectUnsubscribedError} from './util/ObjectUnsubscribedError';
Expand Down