Skip to content

Instantly share code, notes, and snippets.

@zsimo
Last active June 26, 2020 20:03
Show Gist options
  • Save zsimo/0b2db0b6f9cbd102bc544838adcccbfd to your computer and use it in GitHub Desktop.
Save zsimo/0b2db0b6f9cbd102bc544838adcccbfd to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
var error_manager = Machine(
{
id: 'error_manager',
initial: 'ready',
context: {
errors: 0,
},
states: {
ready: {
on: {
UPDATE: {
target: "check",
actions: ['udpate'],
}
},
},
check: {
on: {
'': [
{ target: 'ready', cond: "ok" },
{ target: 'send_email', cond: "too_many_errors" }
]
}
},
send_email: {
on: {
// null event '' always occurs once state is entered
// immediately take the transition to 'await'
"": {
"target": "await",
"actions": ["sendEmail"]
},
}
},
"await": {
after: {
5000: 'ready'
},
exit: ['reset']
}
},
},
{
actions: {
udpate: assign({
errors: function (context, event) {
if (event.error) {
return context.errors + 1;
}
return context.errors;
},
}),
reset: assign({
errors: function () {
return 0;
},
}),
sendEmail: function () {
console.log("sendEmail");
}
},
guards: {
too_many_errors: function (context) {
return context.errors >= 3;
},
ok: function (context) {
return context.errors < 3;
},
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment