Skip to content

Instantly share code, notes, and snippets.

@mstruebing
Created April 28, 2020 07:51
Show Gist options
  • Save mstruebing/31c8b7e55d8d28a74b9ddaf5e2d776c1 to your computer and use it in GitHub Desktop.
Save mstruebing/31c8b7e55d8d28a74b9ddaf5e2d776c1 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const careplanMachine = Machine(
{
id: "careplan",
key: "careplan",
initial: "onboarding",
states: {
onboarding: {
on: {
REDUCE_WEIGHT: "reduce_weight",
SLEEP_ENHANCEMENT: "sleep_enhancement"
}
},
reduce_weight: {
id: "reduce_weight",
key: "reduce_weight",
type: "parallel",
states: {
recipe: {
id: "recipe",
key: "recipe",
initial: "show_recipe",
states: {
show_recipe: {
entry: ["refresh"],
meta: {
refresh_hour: "8"
},
on: {
REFRESH: "show_recipe"
}
}
}
},
blood_sugar_survey: {
id: "blood_sugar_survey",
key: "blood_sugar_survey",
initial: "input",
states: {
input: {
on: {
OVER_500: "over_500",
UNDER_500: "under_500"
}
// needs action to notify in 2 days
},
over_500: {
meta: {
content: "Call a doctor!"
},
on: {
DONE: "wait_for_next_day"
}
},
under_500: {
meta: {
content: "Bloodsugar information"
},
on: {
DONE: "wait_for_next_day"
}
},
wait_for_next_day: {
entry: ["repeat"],
meta: {
interval: "0 10 * * *", // every day at 10AM
event: "WAITED,"
},
on: {
WAITED: "input"
}
}
}
},
research: {
initial: "show_video",
states: {
show_video: {
type: "final",
meta: {
content: "videourl"
}
}
}
}
}
},
sleep_enhancement: {
id: "sleep_enhancement",
key: "sleep_enhancement",
type: "parallel",
states: {
content: {
initial: "show_content",
states: {
show_content: {
entry: ["notify", "refresh"],
exit: ["cancel_notify"],
meta: {
notify_hour: "22",
refresh_hour: "20",
// needs some meta data to choose from contents
},
on: {
REFRESH: "refresh"
}
},
refresh: {
on: {
"": "show_content"
}
}
}
},
blood_sugar_survey: {
id: "blood_sugar_survey",
key: "blood_sugar_survey",
initial: "input",
states: {
input: {
on: {
OVER_500: "over_500",
UNDER_500: "under_500"
}
// needs action to notify in 2 days
},
over_500: {
meta: {
content: "Call a doctor!"
},
on: {
DONE: "wait_for_next_day"
}
},
under_500: {
meta: {
content: "Bloodsugar information"
},
on: {
DONE: "wait_for_next_day"
}
},
wait_for_next_day: {
entry: ["repeat"],
meta: {
interval: "0 10 * * *", // every day at 10AM
event: "WAITED,"
},
on: {
WAITED: "input"
}
}
}
},
wellbeing_survey: {
id: "wellbeing_survey",
key: "wellbeing_survey",
initial: "how_are_you",
states: {
how_are_you: {
on: {
GOOD: "good",
bad: "bad"
}
// needs action to notify in 2 weeks
},
good: {
type: "final",
meta: {
content: "Great, you are healthy"
}
},
bad: {
on: {
FEET: "feet",
HEAD: "head"
}
},
feet: {
on: {
PAIN: "pain",
NO_PAIN: "no_pain"
}
},
head: {
on: {
PAIN: "pain",
NO_PAIN: "no_pain"
}
},
pain: {
type: "final",
meta: {
content: "Call a doctor"
}
},
no_pain: {
type: "final",
meta: {
content: "Not so bad"
}
}
}
// action to repeat every week
// after 2 weeks: sms
}
}
}
}
},
{
actions: {
notify: (context, user_id) => {
// maybe a crontab string?
const when = Object.values(context.meta)[0].notify_hour;
console.log(`INVOKE SERVICE TO NOTIFY AT ${when} O'CLOCK`);
},
refresh: (context, user_id) => {
// maybe a crontab string?
const when = Object.values(context.meta)[0].refresh_hour;
console.log(`INVOKE SERVICE TO REFRESH AT ${when} O'CLOCK`);
},
cancel_notify: (context, user_id) => {
console.log("INVOKE SERVICE TO CANCEL THE FORMER NOTIFICATION");
// How to identify notifications?
},
repeat: (context, user_id) => {
const metaData = Object.values(context.meta)[0];
// maybe a crontab string?
const interval = metaData.interval;
const message = metaData.message;
console.log(`INVOKE SERVICE TO REPEAT ${interval} AND SEND ${message} TO FSM`);
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment