Closed Obi-Dann closed 5 years ago
This seems related to a discussion we've been having in the Gitter (cc. @mogsie @mpolichette).
I think what we want to do is add a property to the State
instance that answers the question:
Did this event result in a state transition?
With the results:
undefined
or null
Initially I was thinking of adding a changed: true
property to the State
instance, but we can bikeshed that.
What I don't think we should do (at least by default) is throw an error if an event is unhandled. Happy to discuss this, though.
I agree, except maybe for the last "state did not transition, event was handled but next state target is undefined or null" — To me it's an indication of the statechart explicitly understanding the event, but choosing not to do anything. I'll have to think about it a bit, maybe come up with some more use cases, I guess :-)
Edit: Oh, and I agree: We should definitely not throw an error if the event was ignored :-)
To me, it makes sense that for the states
states: {
idle: {
on: {
START: 'processing'
}
},
processing: {
on: {
START: null, // or START: '' ?
DONE: 'idle'
}
},
}
then machine.transition('idle', 'DONE') would return an object that indicated:
Conersely, machine.transition('processing', 'START') would return an object that indicated:
In my opinion, the fact that a state transition occurred or not is not worth exposing. If it's important, then you should add an action on the state entry, and react to that signal instead. Knowing if an event was consumed or not is the important part.
the fact that a state transition occurred or not is not worth exposing
This is now under state.changed
(boolean | undefined
).
There seems to be no way to determine if an event can be handled by the current state node
This is now under state.nextEvents
and can be used like:
const currentState = machine.transition(...);
// Does it handle the FOO event?
currentState.nextEvents.includes('FOO');
// => true | false
Bug (at least I think so)
There seems to be no way to determine if an event can be handled by the current state node. For example:
Expected result:
Actual result:
Link to reproduction:
https://codepen.io/anon/pen/qKXbEZ
Discussion
I believe it's a bug, or at least there should be a way to avoid transitions. There's
handles
function that I can potentially use withmachine.getStateNode
, eg:However, it might not be as easy to implement for nested machines and I believe it should be checked in transition when running in
strict
mode.Am I missing something ?