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

feat(Subscription): idempotent add and remove of teardowns #6401

Merged
merged 2 commits into from Sep 19, 2021
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
2 changes: 1 addition & 1 deletion spec/operators/delay-spec.ts
Expand Up @@ -245,7 +245,7 @@ describe('delay', () => {
tap({
next() {
const [[subscriber]] = subscribeSpy.args;
counts.push(subscriber._teardowns.length);
counts.push(subscriber._teardowns.size);
},
complete() {
expect(counts).to.deep.equal([1, 1]);
Expand Down
6 changes: 3 additions & 3 deletions spec/operators/switchAll-spec.ts
Expand Up @@ -318,7 +318,7 @@ describe('switchAll', () => {
});

// Expect one child of switchAll(): The oStream
expect((sub as any)._teardowns?.[0]._teardowns?.length).to.equal(1);
expect([...(sub as any)._teardowns?.values()][0]._teardowns?.size).to.equal(1);
sub.unsubscribe();
});

Expand All @@ -333,10 +333,10 @@ describe('switchAll', () => {
oStreamControl.next(n); // creates inner
});
// Expect one child of switchAll(): The oStream
expect((sub as any)._teardowns?.[0]._teardowns?.length).to.equal(1);
expect([...((sub as any)._teardowns?.values() ?? [])][0]._teardowns?.size).to.equal(1);
// Expect two children of subscribe(): The destination and the first inner
// See #4106 - inner subscriptions are now added to destinations
expect((sub as any)._teardowns?.length).to.equal(2);
expect((sub as any)._teardowns?.size).to.equal(2);
sub.unsubscribe();
});

Expand Down
7 changes: 3 additions & 4 deletions src/internal/Subscription.ts
Expand Up @@ -34,7 +34,7 @@ export class Subscription implements SubscriptionLike {
* The list of registered teardowns to execute upon unsubscription. Adding and removing from this
* list occurs in the {@link #add} and {@link #remove} methods.
*/
private _teardowns: Exclude<TeardownLogic, void>[] | null = null;
private _teardowns: Set<Exclude<TeardownLogic, void>> | null = null;

/**
* @param initialTeardown A function executed first as part of the teardown
Expand Down Expand Up @@ -134,7 +134,7 @@ export class Subscription implements SubscriptionLike {
}
teardown._addParent(this);
}
(this._teardowns = this._teardowns ?? []).push(teardown);
(this._teardowns = this._teardowns ?? new Set()).add(teardown);
}
}
}
Expand Down Expand Up @@ -189,8 +189,7 @@ export class Subscription implements SubscriptionLike {
* @param teardown The teardown to remove from this subscription
*/
remove(teardown: Exclude<TeardownLogic, void>): void {
const { _teardowns } = this;
_teardowns && arrRemove(_teardowns, teardown);
this._teardowns?.delete(teardown);

if (teardown instanceof Subscription) {
teardown._removeParent(this);
Expand Down