statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
27.09k stars 1.25k forks source link

Feature: State memory and gate pseudo states #261

Closed josefbetancourt closed 5 years ago

josefbetancourt commented 5 years ago

Not sure this is relevant to XState goals. The concept has not been evaluated yet.

Not much on the use of memory in Statechart. Only History, afaik, is in the standard. This blog post introduces the concept of states with memory attribute. These states with memory can then be connected to gates, similar to guards. So that statecharts "... handle more complex applications while still remaining a transition network at its core."

http://www.octodecillion.com/statechart-memory-gate/

davidkpiano commented 5 years ago

Have you seen the semantics of <final> states (as in SCXML)? https://xstate.js.org/docs/guides/final.html

We can model "all fields must be valid" by setting 'valid' as the "final" state for each orthogonal node and listening for the done.state.* event.

Suppose field1 looks like this (simplified):

const field1 = {
  states: {
    invalid: {
      on: {
        CHANGE: [
          { target: 'valid', cond: isValid },
          { target: 'invalid' }
        ]
      }
    },
    valid: {
      type: 'final'
    }
  }
};

Then the fields parallel state can capture whether all fields are in their "final" states:

// ...
fields: {
  type: 'parallel',
  states: {
    field1,
    field2,
    field3
  },
  onDone: 'formValid'
},
formValid: {
  on: {
    SUBMIT: // submittable
  }
}
// ...
josefbetancourt commented 5 years ago

@davidkpiano That is truly awesome. I will update the blog post with this info. Thanks.