Guest User

Untitled

a guest
Jan 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. I'm wondering if it would be useful to provide a way to have guards be kept out of the state machine, to keep it all "pure function".
  2.  
  3. The way I thought it might work would be to add a parameter to the `transition` to specify the value of any guards. The guards would always be optional, but would of course influence the outcome. Here's a machine with a named guard (I use 'guard' instead of 'cond' because I like statechart terminology better than scxml):
  4.  
  5. ```js
  6. m = Machine({
  7. foo: {
  8. on: {
  9. A: bar,
  10. B: {
  11. target: bar
  12. guard: isEmpty
  13. }
  14. }
  15. },
  16. bar: {}
  17. })
  18.  
  19. var object = []
  20.  
  21. direct = transition(m.initialState, 'A'); // transitions directly to bar, as before
  22. guarded = transition(m.initialState, 'A'); // about to transition from foo to bar, but it was guarded.
  23. guarded.missingGuards; // [ "isEmpty" ]
  24. failure = transition(m.initialState, 'A', { isEmpty: false }); // state is still "foo" — the event was basically not handled.
  25. success = transition(m.initialState, 'A', { isEmpty: true }); // state is "bar" as the guard allowed the transition to happen.
  26. ```
  27.  
  28. A successful state transition requires that you verify that `missingGuards` is empty. If there are any `missingGuards` then you need to retry the state transition providing those missing guards. The `missingGuards` would be a property on the State object returned.
  29.  
  30. The third parameter to transition would be somewhat of a bitset with the names of the guards and their boolean values.
  31.  
  32. The `guard` attribute should allow boolean logic, though, so that it's possible to say `guard: "!isEmpty && ready"` in the machine definition, and that this would require the `isEmpty` and `ready` guards to be provided.
Add Comment
Please, Sign In to add comment