Skip to content

Commit

Permalink
fix(AsyncSubject): properly emits values during reentrant subscriptio…
Browse files Browse the repository at this point in the history
…ns (#6522)

Resolves #6520
  • Loading branch information
benlesh committed Jul 14, 2021
1 parent a675423 commit dd8bdf3
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
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
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 dd8bdf3

Please sign in to comment.