Skip to content

Instantly share code, notes, and snippets.

@drmikecrowe
Last active November 13, 2019 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drmikecrowe/6c6221acbacdd94c1c350f2debd819d7 to your computer and use it in GitHub Desktop.
Save drmikecrowe/6c6221acbacdd94c1c350f2debd819d7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const SECONDS = 1000;
const MINUTES = 60 * SECONDS;
const HOURS = 60 * MINUTES;
const constants = {
UPDATE_PODCAST_INTERVAL: 4 * HOURS,
};
const condHaveEpisodes = (context, event) => {
return context.downloadCount > 0 || true;
};
const waitingMenuStates = {
initial: "askUpdateNow",
context: {},
states: {
askUpdateNow: {
on: {
PLAY_BUTTON: {
actions: send("UPDATE_NOW", {to: "#Root.running.updater"}),
target: "done"
},
MENU_BUTTON: "askMenuTwo",
},
},
askMenuTwo: {
on: {
PLAY_BUTTON: {
actions: ["TBD"],
target: "done"
},
MENU_BUTTON: "askMenuThree",
},
},
askMenuThree: {
on: {
PLAY_BUTTON: {
actions: ["TBD"],
target: "done"
},
MENU_BUTTON: "done",
},
},
done: {
type: 'final'
}
},
};
const playMenuStates = {
initial: "askMenuOne",
context: {},
states: {
askMenuOne: {
on: {
PLAY_BUTTON: {
actions: ["TBD"],
target: "done"
},
MENU_BUTTON: "askMenuTwo",
},
},
askMenuTwo: {
on: {
PLAY_BUTTON: {
actions: ["TBD"],
target: "done"
},
MENU_BUTTON: "askMenuThree",
},
},
askMenuThree: {
on: {
PLAY_BUTTON: {
actions: ["TBD"],
target: "done"
},
MENU_BUTTON: "done",
},
},
done: {
type: 'final'
}
},
};
const playerStates = {
initial: "waiting",
context: {
retries: 0,
},
states: {
waiting: {
on: {
PLAY_BUTTON: {
target: "playing",
cond: condHaveEpisodes
},
MENU_BUTTON: "waitingMenu",
},
},
waitingMenu: {
...waitingMenuStates,
onDone: "waiting"
},
playing: {
on: {
'': {
actions: "playPodcast",
},
STOP_BUTTON: "waiting",
MENU_BUTTON: "playingMenu",
},
},
playingMenu: {
...playMenuStates,
onDone: "playing"
},
},
};
const updaterStates = {
initial: "checkForUpdates",
context: {},
states: {
checkForUpdates: {
on: {
CHECK_UPDATE_COMPLETE: "waitForUpdates",
},
},
waitForUpdates: {
on: {
UPDATE_NOW: "checkForUpdates",
},
after: {
[4 * HOURS]: {
target: "checkForUpdates",
},
},
},
},
};
const runningStates = {
type: "parallel",
initial: "waiting",
states: {
player: {
...playerStates,
},
updater: {
...updaterStates,
},
},
};
const masterMachine = Machine({
id: "Root",
initial: "running",
context: {},
states: {
running: {
...runningStates,
onDone: "shutdown",
},
shutdown: {
type: "final",
},
},
on: {
SHUTDOWN: "shutdown",
STOP_BUTTON_HELD: "shutdown",
UPDATE_NOW: "#Root.running.updater",
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment