wobsoriano / pinia-xstate

Put your xstate state machines into a global pinia store.
MIT License
55 stars 4 forks source link

Support for xstate v5 (beta) #3

Closed Simonl9l closed 1 year ago

Simonl9l commented 1 year ago

Great to have this package available. Thanks!

Do you have plans to support the https://github.com/statelyai/xstate/tree/next version of xState?

There is a migration guide here

Also if one has child or spawned state machines will the package also ensure the state of those machines are persisted to pinia too?

Whilst it's in beta it would be great to have a matching beta pina-xstate package...!

Given pinia's use case perhaps this is something that the xstate maintainers would bring into the fold of their packages?

Thanks again!

wobsoriano commented 1 year ago

You're welcome! I'll definitely support xstate v5 :)

wobsoriano commented 1 year ago

https://github.com/wobsoriano/pinia-xstate/tree/xstate-v5

You can try using npm install pinia-xstate@2.0.0-beta.0

Simonl9l commented 1 year ago

@wobsoriano thanks for looking into this, and setting up a new npm beta package...

I did try to instal but get this error:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: website@0.0.0
npm ERR! Found: xstate@5.0.0-beta.27
npm ERR! node_modules/xstate
npm ERR!   xstate@"5.0.0-beta.27" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer xstate@"^5.0.0" from pinia-xstate@2.0.0-beta.0
npm ERR! node_modules/pinia-xstate
npm ERR!   pinia-xstate@"2.0.0-beta.0" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

the latest beta xstate version is "xstate": "5.0.0-beta.27" I'd assume (I have no clue on this stuff) you'd need to make the peer dependency be something like "xstate": "^5.0.0-beta.27" than "xstate": "^5.0.0"?

davidkpiano commented 1 year ago

I can help out with this if needed; please let me know.

wobsoriano commented 1 year ago

Hey @davidkpiano, that would be nice!

Simonl9l commented 1 year ago

would it be at all complex to also include the ability to initialize the actor with input?

wobsoriano commented 1 year ago

@Simonl9l It's not documented, but you can do that by passing in a 2nd param to the middleware:

const interpreterOptions = {
    input: {
        userId: '123',
        defaultRating: 5,
    }
}

export const useToggleStore = defineStore(
  toggleMachine.id,
  xstate(toggleMachine, interpreterOptions)
)

https://github.com/wobsoriano/pinia-xstate/blob/b924b1ca4db1b2b3d738fe2b2d6d1a909ce27f4e/src/index.ts#L20

wobsoriano commented 1 year ago

Feel free to try again npm install pinia-xstate@2.0.0-beta.1. Thanks!

Simonl9l commented 1 year ago

Hi @wobsoriano --- not sure we have this set yet?

npm install pinia-xstate@2.0.0-beta.1
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: website@0.0.0
npm ERR! Found: xstate@5.0.0-beta.27
npm ERR! node_modules/xstate
npm ERR!   xstate@"5.0.0-beta.27" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer xstate@"^5.0.0-beta.28" from pinia-xstate@2.0.0-beta.1
npm ERR! node_modules/pinia-xstate
npm ERR!   pinia-xstate@"2.0.0-beta.1" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
davidkpiano commented 1 year ago

Might be a cache issue? Worked for me:

CleanShot 2023-09-15 at 18 40 50