#subscribe to only state changes

1 messages · Page 1 of 1 (latest)

civic yew
#

Hello!
I'm using xstate to control the rendering in a vscode extension I'm building.

Currently I have a couple of actions like renderLoading etc that are fired on entry to various states.
To make my state machine as simple as possible, I'm thinking of extracting that logic to an external actor.subscribe call that listens to the machine's state and calls those renderXYZ functions itself.

Is there something in xstate itself that would allow me to listen only to state changes (and re-entries)? Or would I have to build something with actor.subscribe and keep track of previous values myself?

runic bluff
#

Which state changes do you want to listen for? Do you mean changes to snapshot.value?

civic yew
#

yes!

runic bluff
#

So you may have to build that yourself. Keep track of the previous value and compare the next value, and emit a change when they're different.

civic yew
#

yeah okay that makes sense. Thanks!

atomic oxide
#

snapshot.value seems to contain .... a lot... How to get just the name of the current state, for a start?

harsh musk
atomic oxide
#

Oh hold on, I am mistaken

#

I was subscribing using inspect

#

and, indeed, the following does just log the name of the new state:

#
if (inspectionEvent.type === "@xstate.event") {
        logger.debug({
          newState: inspectionEvent.actorRef.getSnapshot().value,
        });
      }
#

My mistake, and thank you!