Skip to content

Instantly share code, notes, and snippets.

@zsimo
Last active June 28, 2020 12:57
Show Gist options
  • Save zsimo/0c08067ca7168ffd55a00af8d52eaaf0 to your computer and use it in GitHub Desktop.
Save zsimo/0c08067ca7168ffd55a00af8d52eaaf0 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
function isOk (context) {
return context.errors_counter < 3;
}
function isOKAndShouldSendSolvedEmail (context, event) {
return !event.error && context.should_send_solved_email === true;
}
var error_manager = Machine(
{
id: 'error_manager',
initial: 'ready',
context: {
errors_counter: 0,
should_send_solved_email: false
},
states: {
ready: {
on: {
UPDATE: [{
target: "should_send_error_email",
actions: ['update_counter'],
cond: "ko_and_should_not_send_solved_email"
}, {
target: "send_solved_email",
actions: ['update_counter'],
cond: "ok_and_should_send_solved_email"
}]
}
},
should_send_error_email: {
on: {
'': [
{ target: 'ready', cond: "ok" },
{ target: 'send_error_email', cond: "ko" },
]
}
},
send_solved_email: {
on: {
"": {
"target": "ready",
"actions": ["send_solved_email"]
}
},
exit: ["deactivate_solved_email"]
},
send_error_email: {
on: {
"": {
"target": "await",
"actions": ["send_error_email"]
}
},
exit: ["activate_solved_email"],
},
"await": {
after: {
2000: 'ready'
},
exit: ["reset_counter"]
}
}
},
{
actions: {
update_counter: assign({
errors_counter: function (context, event) {
if (event.error) {
return context.errors_counter + 1;
}
return 0;
}
}),
reset_counter: assign({
errors_counter: 0
}),
activate_solved_email: assign({
should_send_solved_email: true
}),
deactivate_solved_email: assign({
should_send_solved_email: false
}),
send_solved_email: function (context, event) {
console.log("send_solved_email");
},
send_error_email: function (context, event) {
console.log("send_error_email");
}
},
guards: {
ok: isOk,
ko: function (context) {
return !isOk(context);
},
ok_and_should_send_solved_email: isOKAndShouldSendSolvedEmail,
ko_and_should_not_send_solved_email: function (context, event) {
return !isOKAndShouldSendSolvedEmail(context, event);
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment