Skip to content

Instantly share code, notes, and snippets.

@mrsln
Created April 10, 2020 20:34
Show Gist options
  • Save mrsln/32d905d031275e4367409e79d3493c6c to your computer and use it in GitHub Desktop.
Save mrsln/32d905d031275e4367409e79d3493c6c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchMachine = Machine({
id: "fetch",
initial: "idle",
context: {
// constants
reproductiveNumber: 0.4,
deathRate: 0.15,
startingPopulation: 1000,
judgmentDay: 7,
// the current numbers
day: 1,
population: 990,
neverContracted: 990,
contractedAll: 10,
recoveredToday: 0,
diedAll: 0,
diedToday: 0,
contractedHistory: [10],
},
states: {
idle: {
on: {
NEXT: {
internal: true,
actions: assign((ctx) => {
let newDied = 0;
let recovered = 0;
const day = ctx.day + 1;
if (day >= 7) {
const contracted7DaysAgo = ctx.contractedHistory[ctx.day-ctx.judgmentDay+1] || 0;
newDied = Math.round(contracted7DaysAgo * ctx.deathRate);
recovered = contracted7DaysAgo - newDied;
}
const diedYesterday = ctx.diedToday;
const recoveredYesterday = ctx.recoveredToday;
const contractedAllYesterday = ctx.contractedAll;
const newContracted = Math.round((contractedAllYesterday - recoveredYesterday - diedYesterday)*ctx.reproductiveNumber*ctx.neverContracted/ctx.startingPopulation);
const contractedHistory = [...ctx.contractedHistory, newContracted];
return {
population: ctx.population - newDied,
contractedAll: newContracted + contractedAllYesterday - recovered - newDied,
diedAll: ctx.diedAll + newDied,
diedToday: newDied,
neverContracted: ctx.neverContracted - newContracted,
day,
contractedHistory,
recoveredToday: recovered,
};
}),
},
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment