diff --git a/spec/observables/bindCallback-spec.ts b/spec/observables/bindCallback-spec.ts index 3763498296..60a16cedd4 100644 --- a/spec/observables/bindCallback-spec.ts +++ b/spec/observables/bindCallback-spec.ts @@ -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 = []; + + 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', () => { diff --git a/spec/observables/bindNodeCallback-spec.ts b/spec/observables/bindNodeCallback-spec.ts index b2a0f7dd5d..c3cd890662 100644 --- a/spec/observables/bindNodeCallback-spec.ts +++ b/spec/observables/bindNodeCallback-spec.ts @@ -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 = []; + + 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', () => { diff --git a/src/internal/observable/bindCallbackInternals.ts b/src/internal/observable/bindCallbackInternals.ts index d89999c5ee..1b43a5dc4f 100644 --- a/src/internal/observable/bindCallbackInternals.ts +++ b/src/internal/observable/bindCallbackInternals.ts @@ -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(); - return function (this: any, ...args: any[]): Observable { + // 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(); + // If this is true, then we haven't called our function yet. let uninitialized = true; return new Observable((subscriber) => {