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

req: derivate children snapshot #11

Open
DavideSegullo opened this issue Dec 5, 2024 · 8 comments · May be fixed by #12
Open

req: derivate children snapshot #11

DavideSegullo opened this issue Dec 5, 2024 · 8 comments · May be fixed by #12

Comments

@DavideSegullo
Copy link

DavideSegullo commented Dec 5, 2024

Hi guys, I'm trying to use jotai-xstate to derivate children actor snapshot, the context is that I have a parent state machine, which based on conditions spaws children, I would like to be able to access the snapshot of these children (when spawned) to be able to determine the substates of the children, is this already possible using atomWithActorSnapshot?

I assume not because if the actor used as input changes, a new subscription is not created. Maybe we only need a way to detect input atom changes and then subscribe again

If someone can give me some suggestions I can also try to do a PR myself, thank you!

For example using react is possible to use useSelector with empty actor if it's undefined: https://stately.ai/docs/actors#empty-actors

@dai-shi
Copy link
Member

dai-shi commented Dec 5, 2024

@brunocangs thoughts?

@brunocangs
Copy link
Collaborator

brunocangs commented Dec 12, 2024

I've never coded anything that spawns children before so not sure. Could you provide a basic example and point where it breaks? Maybe we can come up with an idea

Edit: typo

@DavideSegullo
Copy link
Author

I've never coded anything that spawns children before so not sure. Could you provide a basic example and point where it breaks? Maybe we can come up with an idea

Edit: typo

Ye ofc, I'll send you and example today when I'll get back home, thank you!

@DavideSegullo
Copy link
Author

@brunocangs Here a link I also added some comments inside the code, let me know if it makes sense for you! Thank you

https://stackblitz.com/edit/vitejs-vite-rd4cpznr?file=src%2FApp.tsx

@brunocangs
Copy link
Collaborator

Thanks! I'll take a look and play around and get back to you in a few hours

@brunocangs
Copy link
Collaborator

brunocangs commented Dec 16, 2024

@DavideSegullo It seems that a quick fix allowing the atom to handle undefined is enough to cover the case you showed on the stackblitz. Could you check the PR/branch to see if it fixes the problem? I tested it with the example you provided and it seemed to work well. Do you have any other cases this change might not cover?

image
image

Edit: added second image

@DavideSegullo
Copy link
Author

DavideSegullo commented Dec 17, 2024

@DavideSegullo It seems that a quick fix allowing the atom to handle undefined is enough to cover the case you showed on the stackblitz. Could you check the PR/branch to see if it fixes the problem? I tested it with the example you provided and it seemed to work well. Do you have any other cases this change might not cover?

image image

Edit: added second image

It looks good, I tried it locally by patching jotai-xstate package, I only get this ts error:

Screenshot 2024-12-17 alle 09 29 02

@DavideSegullo
Copy link
Author

So actually we can suppress this error by casting snapshot.children.getUser to AnyActor

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 a pull request may close this issue.

3 participants