diff --git a/spec/Notification-spec.ts b/spec/Notification-spec.ts index c5cbd6fc2f..cbf8094ce3 100644 --- a/spec/Notification-spec.ts +++ b/spec/Notification-spec.ts @@ -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(); }); diff --git a/src/internal/Notification.ts b/src/internal/Notification.ts index 9a4a4abaeb..3ddb20d2c8 100644 --- a/src/internal/Notification.ts +++ b/src/internal/Notification.ts @@ -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, @@ -21,8 +27,8 @@ import { throwError } from './observable/throwError'; export class Notification { 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; } /** @@ -32,11 +38,11 @@ export class Notification { */ observe(observer: PartialObserver): 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(); } } @@ -52,11 +58,11 @@ export class Notification { 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(); } } @@ -86,18 +92,18 @@ export class Notification { toObservable(): Observable { 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 = new Notification('C'); - private static undefinedValueNotification: Notification = new Notification('N', undefined); + private static completeNotification: Notification = new Notification(NotificationKind.COMPLETE); + private static undefinedValueNotification: Notification = new Notification(NotificationKind.NEXT, undefined); /** * A shortcut to create a Notification instance of the type `next` from a @@ -109,7 +115,7 @@ export class Notification { */ static createNext(value: T): Notification { if (typeof value !== 'undefined') { - return new Notification('N', value); + return new Notification(NotificationKind.NEXT, value); } return Notification.undefinedValueNotification; } @@ -123,7 +129,7 @@ export class Notification { * @nocollapse */ static createError(err?: any): Notification { - return new Notification('E', undefined, err); + return new Notification(NotificationKind.ERROR, undefined, err); } /** diff --git a/src/internal/Rx.ts b/src/internal/Rx.ts index e6b3010822..ee6ef897a1 100644 --- a/src/internal/Rx.ts +++ b/src/internal/Rx.ts @@ -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';