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

null out _unsubscribe after unsubscription #5465

Merged
merged 7 commits into from
Jul 30, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions spec/Subscriber-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,17 @@ describe('Subscriber', () => {
expect(subscriberUnsubscribed).to.be.true;
expect(subscriptionUnsubscribed).to.be.true;
});

it('should have idempotent unsubscription', () => {
let count = 0;
const subscriber = new Subscriber();
subscriber.add(() => ++count);
expect(count).to.equal(0);

subscriber.unsubscribe();
expect(count).to.equal(1);

subscriber.unsubscribe();
expect(count).to.equal(1);
});
});
12 changes: 12 additions & 0 deletions spec/Subscription-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,17 @@ describe('Subscription', () => {
done();
});
});

it('Should have idempotent unsubscription', () => {
let count = 0;
const subscription = new Subscription(() => ++count);
expect(count).to.equal(0);

subscription.unsubscribe();
expect(count).to.equal(1);

subscription.unsubscribe();
expect(count).to.equal(1);
});
});
});
19 changes: 16 additions & 3 deletions src/internal/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class Subscription implements SubscriptionLike {
*/
constructor(unsubscribe?: () => void) {
if (unsubscribe) {
(<any> this)._unsubscribe = unsubscribe;
(this as any)._ctorUnsubscribe = true;
(this as any)._unsubscribe = unsubscribe;
}
}

Expand All @@ -56,7 +57,7 @@ export class Subscription implements SubscriptionLike {
return;
}

let { _parentOrParents, _unsubscribe, _subscriptions } = (<any> this);
let { _parentOrParents, _ctorUnsubscribe, _unsubscribe, _subscriptions } = (this as any);

this.closed = true;
this._parentOrParents = null;
Expand All @@ -74,6 +75,18 @@ export class Subscription implements SubscriptionLike {
}

if (isFunction(_unsubscribe)) {
// It's only possible to null _unsubscribe - to release the reference to
// any teardown function passed in the constructor - if the property was
// actually assigned in the constructor, as there are some classes that
// are derived from Subscriber (which derives from Subscription) that
// implement an _unsubscribe method as a mechanism for obtaining
// unsubscription notifications and some of those subscribers are
// recycled. Also, in some of those subscribers, _unsubscribe switches
// from a prototype method to an instance property - see notifyNext in
// RetryWhenSubscriber.
if (_ctorUnsubscribe) {
(this as any)._unsubscribe = undefined;
}
try {
_unsubscribe.call(this);
} catch (e) {
Expand Down Expand Up @@ -130,7 +143,7 @@ export class Subscription implements SubscriptionLike {
add(teardown: TeardownLogic): Subscription {
let subscription = (<Subscription>teardown);

if (!(<any>teardown)) {
if (!teardown) {
return Subscription.EMPTY;
}

Expand Down