Xstate: Question: Is there a way to get the current states of a state machine?

Created on 29 Nov 2018  路  5Comments  路  Source: davidkpiano/xstate

Hi, I would like to get the set of current states (i.e. the id strings of all the leaf states of all orthogonal regions) of a state machine. Is there a way to get this? I didn't seem to find an API for that.

Thanks!

question

All 5 comments

What is your use-case?

For example, you can represent the state value as an array of strings using state.toStrings() (see docs):

state.toStrings();
// => ['north', 'east', 'north.red', 'north.red.walk', 'east.green', ...]

Hi, my use case is for debugging. I want to print out the current state(s) of my state machine (created from Machine(...)).
In other words, I am not going to print out the strings of all states in the state machine, but just the current one(s) - the leaf state in all orthogonal regions.
For example, I have:
m = Machine(config)

Sometime later, say from a debug console, I type a command "print currStates", I would need to do something like this:
console.log(m.getCurrStates)

Is there such an API as getCurrStates()? I think this would be a useful feature.

I could save the current state myself in onTransition(state). However this won't work for orthogonal regions, as the state reported by onTransition is only for one particular region (one at a time).

Thanks for any hints.

This is what the toStrings() method of a State object does. State objects are returned from machine.transition(), and are also accessible from the callback listener set by service.onTransition(). You could probably set up transition listener that saved the new states somewhere, and your command line tool could just recall the last saved state.
This is just based on my understanding of the library and of your question, please correct me if I've got something wrong.

Closing as this is answered: use state.toStrings(): https://xstate.js.org/docs/guides/states.html#state-tostrings

Hello there,
Where is the reference in the global scope of the machines and states?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mattiamanzati picture mattiamanzati  路  3Comments

3plusalpha picture 3plusalpha  路  3Comments

kurtmilam picture kurtmilam  路  3Comments

carlbarrdahl picture carlbarrdahl  路  3Comments

laurentpierson picture laurentpierson  路  3Comments