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

Do not crash when stopping already stopped interpreter. #1710

Merged
merged 3 commits into from
Feb 6, 2021

Conversation

davidkpiano
Copy link
Member

Fixes #1697

@changeset-bot
Copy link

changeset-bot bot commented Dec 9, 2020

🦋 Changeset detected

Latest commit: 572a711

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
xstate Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -503,6 +503,11 @@ export class Interpreter<
this.doneListeners.delete(listener);
}

if (!this.initialized) {
// Interpreter already stopped; do nothing
return this;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we throw an error that just would lead users to fix their mistake? isn't stopping twice breaking an invariant rule of XState? I see arguments both ways (throwing vs making this noop) and I'm somewhat undecided what's better myself - just raising this as a general question.

Copy link
Member Author

Choose a reason for hiding this comment

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

There can exist use-cases where the not-started interpreter is stopped by no fault of the developer; throwing would be counter-productive and unexpected.

Contrived example: in React, a component that is rendered but immediately unmounted (for some reason) would need to .stop() all services, even if they haven't started yet.

I think the best thing to do is to no-op, since the intention is to stop the service, and if the service is already stopped, mission accomplished 👍

Copy link
Member

Choose a reason for hiding this comment

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

Contrived example: in React, a component that is rendered but immediately unmounted (for some reason) would need to .stop() all services, even if they haven't started yet.

This actually can't happen - if we don't reach the commit phase we won't start anything and unmounting just wont happen so stop won't be called either.

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
@davidkpiano davidkpiano merged commit 07586cc into master Feb 6, 2021
@davidkpiano davidkpiano deleted the davidkpiano/1697 branch February 6, 2021 18:11
@github-actions github-actions bot mentioned this pull request Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants