Reputation: 189
I have a catch all error state that does some cleanup for my application and exits nicely.
currently I have to create a specific event {type: "unexpected_error"}
and add a transition to every single state of my machine to achieve that.
Is there a simpler way that I can specify a transition for all states so I don't have to add this transition to every single state?
Upvotes: 2
Views: 279
Reputation: 5203
Yes! You can place transitions on the top-level machine.
const machine = Machine({
// ...
states: { /*...*/ },
// top-level transition
on: {
"unexpected-error": { actions: /*...*/ }
}
});
Alternatively, since it's just a JavaScript object, so you can make a helper function:
function transitionsWithErrorHandler(transitions) {
return {
...transitions,
"unexpected-error": { actions: /*...*/ }
}
}
// ...
states: {
foo: {
on: transitionsWithErrorHandler({
EVENT: 'bar',
ANOTHER_EVENT: 'baz'
})
}
}
Upvotes: 4