Skip to content

Commit

Permalink
fix(bindCallback): resulting function now recreated underlying Subjec…
Browse files Browse the repository at this point in the history
…t and is reusable once again. (#6369)

* test: add failing tests

* fix(bindCallback): create subject within factory
  • Loading branch information
cartant committed May 6, 2021
1 parent 2931fed commit abf2bc1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
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

0 comments on commit abf2bc1

Please sign in to comment.