Description
I'm trying to reproduce the watch statechart from David Harel paper but looks like the history state is not working. I'm stuck on Fig. 10(b), when entering the substate alarm1 it should go to the substate that was selected last time (on or off), or if it's the first time it should go directly to off.
I've modeled this behavior by adding a lastMode history state which should target (redirect) to off if undefined. This lastMode state is the initial state for alarm1, so it should initialize it based on previous history.
Expected Result
It should remember the last state selected on or off. If it's the first time entering on the alarm1 substate it should move to off.
Actual Result
When entering on alarm1 it always moves to off state.
Reproduction
https://xstate.js.org/viz/?gist=95fe324c3dbccd5ddd2ef630fa3c34da
Additional context
XState: 4.6.7
Thanks!
Workaround: set
A_DOWN: 'alarm1.lastMode',
and make the initial state off. I'll investigate, but that works. https://xstate.js.org/viz/?gist=95fe324c3dbccd5ddd2ef630fa3c34da
I believe this is caused by naive~ implementation here:
https://github.com/davidkpiano/xstate/blob/fb8c83fefefa406ceeace62094f6df66fb5757db/src/StateNode.ts#L1625-L1635
While resolving history/initial states the algorithm assumes they are mutually exclusive - so it either resolves one or another. The correct implementation would have to work recursively.
I'm investigating how initial states are handled in general right now (to implement initial transitions - with actions) so I can take a look at this one too at the same time.
History algorithm has been reworked in V5 (next branch right now).