-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Actions inside state? #93
Comments
Thanks for starting the discussion! There are three design considerations that heavily drive Robot's API:
Having a second way to perform actions would violate (1) and (2). You correctly point out that having many transitions that all want to perform an action could result in verbose code. There's sort of 2 ways to solve this:
const validatingTransition = event => transition("submit", "validate",
action(ctx => log('Validation attempt', ctx)))
const machine = createMachine({
idle: state(transition("submit", "log_validate")),
log_validate: state(
immediate("validate", action(ctx => log('Validation attempt', ctx)))
),
validate: state()
}); |
Thanks for the suggestions! To keep the discussion going, for a while at least, I'll throw in a few counterarguments:
Regarding violating the design principles 1 & 2
|
For the project I've been working on, which is building an API on top of const createStep = (stepName: string, ...transitions: Transition[]) => {
return {
[stepName]: [transition('updateValues', stepName', updateValuesReducer), ...transitions]
};
} This is similar to the A composable function could be created that includes whatever action is required when transitioning to a certain state. 🤷♂ |
Yeah @HipsterBrown, I would do it that way as well. Very cool to see Robot being used in a wizard! I'd love to hear more about it if you can. @arggh One thing to keep in mind is that, as implemented, an action is just a reducer that has no return value. https://github.com/matthewp/robot/blob/master/machine.js#L31 . I just say this to say that an action inside of state would be a new feature. I'm curious, what is the usecase for enter/exit actions? Or actions in general? I've really only used them for debugging. |
I actually just found a great use for actions today. In the wizard tool I'm building, I was originally using the service Once I get approval to open-source this stuff, it will help illustrate what I'm talking about. |
@arggh haven't read the whole thread, but what about using const machine = createMachine({
initial: state(
transition('START', 'start'),
),
start: state(
immediate('start',
guard((ctx) => !ctx),
reduce(() => true),
action(() => {
console.log('I run once on start!')
})
),
transition('DONE', 'done'),
),
done: state(),
}), |
I'm going to close because we aren't going to do this right now. Happy to continue discussion and would be happy to come up with patterns to document on the site to achieve this sort of thing through composition. |
Is this still working today? I think it's not. At least I'm not able to. Can you help me, @kybarg? |
@HipsterBrown can you publish some code for that wizard with robot? |
I'll start with the disclaimer that I'm just dipping my toes with robot, but here goes.
Currently the documentation for actions states that:
Question
Why are actions allowed only within transitions? What if I want to invoke an action every time the machine enters a specific state, regardless of the previous state? With the current API, I have to define that action in all the transitions that lead to that specific state.
Suggestion
Allow actions to be defined also as children of state.
Now the action get's invoked every time the machine enters validate-state.
To take it one step further, there could also be an option to define whether the action should be triggered upon entry or exit of state.
The text was updated successfully, but these errors were encountered: