Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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".
- 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):
- ```js
- m = Machine({
- foo: {
- on: {
- A: bar,
- B: {
- target: bar
- guard: isEmpty
- }
- }
- },
- bar: {}
- })
- var object = []
- direct = transition(m.initialState, 'A'); // transitions directly to bar, as before
- guarded = transition(m.initialState, 'A'); // about to transition from foo to bar, but it was guarded.
- guarded.missingGuards; // [ "isEmpty" ]
- failure = transition(m.initialState, 'A', { isEmpty: false }); // state is still "foo" — the event was basically not handled.
- success = transition(m.initialState, 'A', { isEmpty: true }); // state is "bar" as the guard allowed the transition to happen.
- ```
- 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.
- The third parameter to transition would be somewhat of a bitset with the names of the guards and their boolean values.
- 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