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

Fixed an issue with useSelector always computing fresh snapshots internally for uninitialized services #3500

Merged
merged 2 commits into from
Aug 5, 2022

Conversation

Andarist
Copy link
Member

@Andarist Andarist commented Aug 5, 2022

closes #3497

@changeset-bot
Copy link

changeset-bot bot commented Aug 5, 2022

🦋 Changeset detected

Latest commit: 214baeb

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

This PR includes changesets to release 1 package
Name Type
@xstate/react 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

@ghost
Copy link

ghost commented Aug 5, 2022

👇 Click on the image for a new way to code review
  • Make big changes easier — review code in small groups of related files

  • Know where to start — see the whole change at a glance

  • Take a code tour — explore the change with an interactive tour

  • Make comments and review — all fully sync’ed with github

    Try it now!

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 5, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 214baeb:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration

Comment on lines 12 to 22
const defaultGetSnapshot = (a, initialStateCacheRef) => {
if (isService(a)) {
if (a.status === 0 && initialStateCacheRef.current) {
return initialStateCacheRef.current;
}
const snapshot = getServiceSnapshot(a);
initialStateCacheRef.current = a.status === 0 ? snapshot : null;
return snapshot;
}
return isActorWithState(a) ? a.state : undefined;
};
Copy link
Member Author

Choose a reason for hiding this comment

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

Is it pretty? Not at all. It's basically a hack to cache those results as they should represent the same thing, even if they are not preferentially transparent.

We can't easily refactor this in v4 because we can't compute the initial "true" snapshot when the service isn't initialized as computing this might lead to side-effects. This problem doesn't exist in v5


const [snapshot1] = snapshots;
expect(snapshots.every((s) => s === snapshot1));
expect(console.error).toHaveBeenCalledTimes(0);
Copy link
Member

Choose a reason for hiding this comment

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

I'd love to find a more robust/flexible way of specifying the logging/error sinks for v5 (future note, not for this PR)

Copy link
Member Author

Choose a reason for hiding this comment

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

Note that this doesn't test a log that we control. This one comes from React

Co-authored-by: David Khourshid <davidkpiano@gmail.com>
@Andarist Andarist merged commit 0dfc6d9 into main Aug 5, 2022
@Andarist Andarist deleted the andarist/fix-3497 branch August 5, 2022 12:27
@github-actions github-actions bot mentioned this pull request Aug 5, 2022
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.

Bug: Accessing children within useSelector() throws an error
2 participants