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): add no longer returns unnecessary Subscription … #5656

Merged

Conversation

benlesh
Copy link
Member

@benlesh benlesh commented Aug 18, 2020

…reference

  • Updates a few locations where this feature was being used incorrectly.
  • Eliminates confusion where people were trying to "chain" add calls.

BREAKING CHANGE: add no longer returns an unnecessary Subscription reference. This means that if you are calling add with a function and not a Subscription (e.g. subscription.add(() => { /* teardown */ })), you will not be able to remove that teardown function with remove. The fix for this is to wrap your function with a Subscription like so: const childSub = new Subscription(() => { /* teardown */ }); subscription.add(childSub);. Then you will be able to remove it via subscription.remove(childSub);. Bear in mind that is it an edge case to need to manually remove a child subscription from a parent subscription. All subscriptions that have been added to other subscriptions will remove themselves from the parent subscription(s) automatically when they are unsubscribed..

@benlesh benlesh requested review from cartant and kwonoj August 18, 2020 21:04
@benlesh
Copy link
Member Author

benlesh commented Aug 18, 2020

This is an important step toward fixing some critical internal pieces of our library.

@benlesh benlesh force-pushed the feat/Subscription-add-returns-void branch 2 times, most recently from 54eeda7 to e155442 Compare August 18, 2020 21:59
Copy link
Collaborator

@cartant cartant left a comment

Choose a reason for hiding this comment

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

LGTM. IDK why ts-api-guardian does funky stuff with the union orderings. I might investigate it sometime. Although, Craig Spence tweeted about it, last night, so maybe he'll figure it out. 🤞

@@ -170,14 +169,14 @@ export class Subscription implements SubscriptionLike {

// Add `this` as parent of `subscription` if that's not already the case.
let { _parentOrParents } = subscription;
if (_parentOrParents === null) {
if (_parentOrParents == null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

thought: Yikes. I wonder if there's a lint rule to catch this sort of thing?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is intentional. == null matches null or undefined.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah. I know, your change fixes a potential bug. I'm wondering if comparisons like this could be elsewhere. I know there is a lint rule that relaxes === when comparing null, but IDK that there's one that enforces/recommends the use of == when comparing against null. With the varied use of null and undefined in this codebase - callers can pass either - it would be interesting - to me, anyway - to see if this occurs elsewhere. A search could find it, but a lint rule could enforce it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems that there are about half a dozen other uses of either === null or === undefined throughout the codebase (ignoring the docs app).

…reference

- Updates a few locations where this feature was being used incorrectly.
- Eliminates confusion where people were trying to "chain" `add` calls.

BREAKING CHANGE: `add` no longer returns an unnecessary Subscription reference. This means that if you are calling `add` with a _function_ and not a `Subscription` (e.g. `subscription.add(() => { /* teardown */ })`), you will not be able to remove that teardown _function_ with `remove`. The fix for this is to wrap your function with a `Subscription` like so:  `const childSub = new Subscription(() => { /* teardown */ }); subscription.add(childSub);`. Then you will be able to remove it via `subscription.remove(childSub);`. Bear in mind that is it an edge case to need to manually remove a child subscription from a parent subscription. **All subscriptions that have been added to other subscriptions will remove themselves from the parent subscription(s) automatically when they are unsubscribed.**.
@benlesh benlesh force-pushed the feat/Subscription-add-returns-void branch from add28f4 to dd8b8a9 Compare August 19, 2020 20:16
@benlesh benlesh merged commit 4de604e into ReactiveX:master Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants