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(subscribe): allows functions where bind has been patched to be weird #6796

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions spec/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,34 @@ describe('Observable', () => {
});

describe('subscribe', () => {
it('should work with handlers with hacked bind methods', () => {
const source = of('Hi');
const results: any[] = [];
const next = function (value: string) {
results.push(value);
}
next.bind = () => { /* lol */};

const complete = function () {
results.push('done');
}
complete.bind = () => { /* lol */};

source.subscribe({ next, complete });
expect(results).to.deep.equal(['Hi', 'done']);
});

it('should work with handlers with hacked bind methods, in the error case', () => {
const source = throwError(() => 'an error');
const results: any[] = [];
const error = function (value: string) {
results.push(value);
}

source.subscribe({ error });
expect(results).to.deep.equal(['an error']);
});

it('should be synchronous', () => {
let subscribed = false;
let nexted: string;
Expand Down
6 changes: 3 additions & 3 deletions src/internal/Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ export class SafeSubscriber<T> extends Subscriber<T> {
} else {
context = observerOrNext;
}
next = next?.bind(context);
error = error?.bind(context);
complete = complete?.bind(context);
next = next && ((value: T) => context.next(value));
error = error && ((err: any) => context.error(err));
complete = complete && (() => context.complete());
}

// Once we set the destination, the superclass `Subscriber` will
Expand Down