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(bindCallback): create subject within factory #6369

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions spec/observables/bindCallback-spec.ts
Expand Up @@ -126,6 +126,29 @@ describe('bindCallback', () => {
done();
});
});

it('should create a separate internal subject for each call', () => {
function callback(datum: number, cb: (result: number) => void) {
cb(datum);
}
const boundCallback = bindCallback(callback);
const results: Array<string|number> = [];

boundCallback(42)
.subscribe(x => {
results.push(x);
}, null, () => {
results.push('done');
});
boundCallback(54)
.subscribe(x => {
results.push(x);
}, null, () => {
results.push('done');
});

expect(results).to.deep.equal([42, 'done', 54, 'done']);
});
});

describe('when scheduled', () => {
Expand Down
23 changes: 23 additions & 0 deletions spec/observables/bindNodeCallback-spec.ts
Expand Up @@ -124,6 +124,29 @@ describe('bindNodeCallback', () => {
done();
});
});

it('should create a separate internal subject for each call', () => {
function callback(datum: number, cb: (err: any, n: number) => void) {
cb(null, datum);
}
const boundCallback = bindNodeCallback(callback);
const results: Array<number | string> = [];

boundCallback(42)
.subscribe(x => {
results.push(x);
}, null, () => {
results.push('done');
});
boundCallback(54)
.subscribe(x => {
results.push(x);
}, null, () => {
results.push('done');
});

expect(results).to.deep.equal([42, 'done', 54, 'done']);
});
});

describe('when scheduled', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/internal/observable/bindCallbackInternals.ts
Expand Up @@ -35,11 +35,11 @@ export function bindCallbackInternals(
};
}

// We're using AsyncSubject, because it emits when it completes,
// and it will play the value to all late-arriving subscribers.
const subject = new AsyncSubject<any>();

return function (this: any, ...args: any[]): Observable<any> {
// We're using AsyncSubject, because it emits when it completes,
// and it will play the value to all late-arriving subscribers.
const subject = new AsyncSubject<any>();

// If this is true, then we haven't called our function yet.
let uninitialized = true;
return new Observable((subscriber) => {
Expand Down