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

refactor(Notification): Define a private 'isObserver' type guard. #1211

Closed
wants to merge 5 commits into from
Closed
8 changes: 6 additions & 2 deletions src/Notification.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {Observer} from './Observer';
import {Observable} from './Observable';

function isObserver<T>(v: any | Observer<T>): v is Observer<T> {
return !!v && typeof v.next === 'function' && typeof v.error === 'function' && typeof v.complete === 'function';
}

export class Notification<T> {
hasValue: boolean;

Expand Down Expand Up @@ -32,8 +36,8 @@ export class Notification<T> {
}

accept(nextOrObserver: Observer<T> | ((value: T) => void), error?: (err: any) => void, complete?: () => void) {
if (nextOrObserver && typeof (<Observer<T>>nextOrObserver).next === 'function') {
return this.observe(<Observer<T>>nextOrObserver);
if (isObserver(nextOrObserver)) {
return this.observe(nextOrObserver);
} else {
return this.do(<(value: T) => void>nextOrObserver, error, complete);
}
Expand Down