Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AsyncSubject): properly emits values during reentrant subscriptions #6522

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to get prettier going on this file. This was just moving a space that was bothering me. haha.

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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, there's a temporal space where we're _isComplete, but not isStopped while we're nexting to consumers in our complete() method. If someone subscribes during that, we end up hitting this code, and if we're only checking isStopped it's not "stopped" yet, so we didn't notify the consumer.

_hasValue && subscriber.next(_value!);
subscriber.complete();
}
Expand Down