#Detecting when actor.send sends event that does not exist.
1 messages · Page 1 of 1 (latest)
you should be able to typecheck the events, no?
btw, when an event triggers no transition, it’s not an error but intentional: statecharts are deterministic, firing only explicitly defined transitions.
Type checking seems like a good idea though, and I'm guessing the inspect API could be useful for debugging if worry about typos and are not using types: https://stately.ai/docs/inspection
The Inspect API is a way to inspect the state transitions of your state machines and every aspect of actors in an actor system. Including:
The context was this: Say I create an interface element that should send an {type: "increment"} event, but I accidentally set it up to send the {type: "inclement"} event.
If I use the inspect hook, it gets an '@xstate.event', followed by an '@xstate.microstep', followed by an '@xstate.snapshot'. The '@xstate.snapshot' event suggests that missing transitions are just treated as self transitions. I don't know how to tell from this that {type: "inclement"} was not a defined transition without visually examining the state machine diagram.
I have unexpected behavior in my system that I can't work out how to turn into an error. The only way to find the bug is visual inspection.
Though I could wrap every event call so that it first calls snapshot.can(event), though that feels a little off.
are you not using typescript?
createMachine({
types: {
events: {} as
| { type: "increment" }
| { type: "decrement" }
}
})
... then sending an event not listed would return a type error
You can also define a catch-all transition: https://stately.ai/docs/transitions#wildcard-transitions
re: self-transition, I wouldn't expect a self-transition to happen when sending an unhandled event. You can add an entry action to console log if you want to double check that
sharing a minimal complete machine (on a gist or something) could help explaining your concern, if I'm missing something
It seems like there used to be an "strict" mode, but not anymore: https://stately.ai/docs/migration#use-wildcard--transitions-not-strict-mode
The guide below explains how to migrate from XState version 4 to version 5. Migrating from XState v4 to v5 should be a straightforward process. If you get stuck or have any questions, please reach out to the Stately team on our Discord.