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

fix(Notification): ensure do and observe handle partial observation #1260

Closed
wants to merge 1 commit into from
Closed
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
42 changes: 42 additions & 0 deletions spec/Notification-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ describe('Notification', function () {

expect(invoked).toBe(true);
});

it('should handle next if no next was passed', function () {
var n = Notification.createNext();
expect(function () {
n.do(null, null, null);
}).not.toThrow();
});

it('should handle error if no error was passed', function () {
var n = Notification.createError();
expect(function () {
n.do(null, null, null);
}).not.toThrow();
});

it('should handle complete if no complete was passed', function () {
var n = Notification.createComplete();
expect(function () {
n.do(null, null, null);
}).not.toThrow();
});
});

describe('accept', function () {
Expand Down Expand Up @@ -277,5 +298,26 @@ describe('Notification', function () {
n.observe(observer);
expect(observed).toBe(true);
});

it('should not error for complete notifications if no complete handler', function () {
var n = Notification.createComplete();
expect(function () {
n.observe({});
}).not.toThrow();
});

it('should not error for error notifications if no error handler', function () {
var n = Notification.createError();
expect(function () {
n.observe({});
}).not.toThrow();
});

it('should not error for next notifications if no next complete', function () {
var n = Notification.createNext();
expect(function () {
n.observe({});
}).not.toThrow();
});
});
});
12 changes: 6 additions & 6 deletions src/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ export class Notification<T> {
observe(observer: Observer<T>): any {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type definition still requires observer interface, so typescript won't be able to pass partial implementation of observers. Should it be updated in this PR, or leave it for further type enhancement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I thought I also had a PR to change the Observer interface to allow partials as well...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, https://github.com/ReactiveX/RxJS/blob/master/src/Observer.ts#L3-L5 seems we may need those changes also. (better to include in this PR as one shot changes?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just filed an issue related to this here: microsoft/TypeScript#6806

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This microsoft/TypeScript#4889 might be relevant to what we want?

switch (this.kind) {
case 'N':
return observer.next(this.value);
return observer.next && observer.next(this.value);
case 'E':
return observer.error(this.exception);
return observer.error && observer.error(this.exception);
case 'C':
return observer.complete();
return observer.complete && observer.complete();
}
}

do(next: (value: T) => void, error?: (err: any) => void, complete?: () => void): any {
const kind = this.kind;
switch (kind) {
case 'N':
return next(this.value);
return next && next(this.value);
case 'E':
return error(this.exception);
return error && error(this.exception);
case 'C':
return complete();
return complete && complete();
}
}

Expand Down