Skip to content

Commit

Permalink
fix(AsyncSubject): properly emits values during reentrant subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jul 14, 2021
1 parent e04dc57 commit b3a6bbb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
23 changes: 22 additions & 1 deletion spec/subjects/AsyncSubject-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('AsyncSubject', () => {
expect(calls).to.equal(1);
});

it('should not be reentrant via next', () => {
it('should not be reentrant via next', () => {
const subject = new AsyncSubject<number>();
let calls = 0;
subject.subscribe({
Expand All @@ -229,4 +229,25 @@ describe('AsyncSubject', () => {

expect(calls).to.equal(1);
});

it('should allow reentrant subscriptions', () => {
const subject = new AsyncSubject<number>()
let results: any[] = [];

subject.subscribe({
next: (value) => {
subject.subscribe({
next: value => results.push('inner: ' + (value + value)),
complete: () => results.push('inner: done')
});
results.push('outer: ' + value);
},
complete: () => results.push('outer: done')
});

subject.next(1);
expect(results).to.deep.equal([]);
subject.complete();
expect(results).to.deep.equal(['inner: 2', 'inner: done', 'outer: 1', 'outer: done']);
});
});
4 changes: 2 additions & 2 deletions src/internal/AsyncSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export class AsyncSubject<T> extends Subject<T> {

/** @internal */
protected _checkFinalizedStatuses(subscriber: Subscriber<T>) {
const { hasError, _hasValue, _value, thrownError, isStopped } = this;
const { hasError, _hasValue, _value, thrownError, isStopped, _isComplete } = this;
if (hasError) {
subscriber.error(thrownError);
} else if (isStopped) {
} else if (isStopped || _isComplete) {
_hasValue && subscriber.next(_value!);
subscriber.complete();
}
Expand Down

0 comments on commit b3a6bbb

Please sign in to comment.