import { actions } from '@muze-labs/simplyflow/actions'Actions are named application functions. In normal apps you configure them through app({ actions }).
app({
data: { count: 0 },
actions: {
increment({ amount = 1 } = {}) {
this.data.count += amount
}
},
commands: {
addOne() {
this.actions.increment({ amount: 1 })
}
}
})Inside an action, this is the app instance.
SimplyFlow encourages actions that take one object argument with named values:
actions: {
selectContact({ id, tab = 'details' }) {
this.data.selectedContactId = id
this.data.tab = tab
}
}This style works well with route action shorthand and commands.
When the app has onError, action errors are caught and passed to it.
app({
data: {},
onError(error, context) {
console.error('Action failed', error, context)
},
actions: {
async save() {
throw new Error('not saved')
}
}
})context is the action function that failed.
Without onError, action errors throw normally.
Reading or calling a missing action logs a warning once per missing action name.
simplyflow/action: unknown action "svae". Did you mean "save"?
If there is no close match, the warning omits the suggestion.
Lower-level factory.
const actionApi = actions({
app: myApp,
actions: {
save() {}
}
})When options.app is supplied, returned action functions are bound to that app. When no app is supplied, actions(options) returns options unchanged.