#Detecting when actor.send sends event that does not exist.

1 messages · Page 1 of 1 (latest)

timid crescent
#

If i make a typo when sending an event, or send an event unavailable to the current state, the send function fails silently.
How can I detect when send fails in this way without using wildcard events on the state machine (they stop snapshot.can() from being useful)?

real aspen
#

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:

timid crescent
#

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.

real aspen
#

are you not using typescript?

createMachine({
  types: {
    events: {} as
      | { type: "increment" }
      | { type: "decrement" }
  }
})

... then sending an event not listed would return a type error

#

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