Skip to content

Commit abf2bc1

Browse files
authored
fix(bindCallback): resulting function now recreated underlying Subject and is reusable once again. (#6369)
* test: add failing tests * fix(bindCallback): create subject within factory
1 parent 2931fed commit abf2bc1

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

spec/observables/bindCallback-spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,29 @@ describe('bindCallback', () => {
126126
done();
127127
});
128128
});
129+
130+
it('should create a separate internal subject for each call', () => {
131+
function callback(datum: number, cb: (result: number) => void) {
132+
cb(datum);
133+
}
134+
const boundCallback = bindCallback(callback);
135+
const results: Array<string|number> = [];
136+
137+
boundCallback(42)
138+
.subscribe(x => {
139+
results.push(x);
140+
}, null, () => {
141+
results.push('done');
142+
});
143+
boundCallback(54)
144+
.subscribe(x => {
145+
results.push(x);
146+
}, null, () => {
147+
results.push('done');
148+
});
149+
150+
expect(results).to.deep.equal([42, 'done', 54, 'done']);
151+
});
129152
});
130153

131154
describe('when scheduled', () => {

spec/observables/bindNodeCallback-spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,29 @@ describe('bindNodeCallback', () => {
124124
done();
125125
});
126126
});
127+
128+
it('should create a separate internal subject for each call', () => {
129+
function callback(datum: number, cb: (err: any, n: number) => void) {
130+
cb(null, datum);
131+
}
132+
const boundCallback = bindNodeCallback(callback);
133+
const results: Array<number | string> = [];
134+
135+
boundCallback(42)
136+
.subscribe(x => {
137+
results.push(x);
138+
}, null, () => {
139+
results.push('done');
140+
});
141+
boundCallback(54)
142+
.subscribe(x => {
143+
results.push(x);
144+
}, null, () => {
145+
results.push('done');
146+
});
147+
148+
expect(results).to.deep.equal([42, 'done', 54, 'done']);
149+
});
127150
});
128151

129152
describe('when scheduled', () => {

src/internal/observable/bindCallbackInternals.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ export function bindCallbackInternals(
3535
};
3636
}
3737

38-
// We're using AsyncSubject, because it emits when it completes,
39-
// and it will play the value to all late-arriving subscribers.
40-
const subject = new AsyncSubject<any>();
41-
4238
return function (this: any, ...args: any[]): Observable<any> {
39+
// We're using AsyncSubject, because it emits when it completes,
40+
// and it will play the value to all late-arriving subscribers.
41+
const subject = new AsyncSubject<any>();
42+
4343
// If this is true, then we haven't called our function yet.
4444
let uninitialized = true;
4545
return new Observable((subscriber) => {

0 commit comments

Comments
 (0)