Skip to content

Instantly share code, notes, and snippets.

@vojtaholik
Last active January 14, 2022 14:11
Show Gist options
  • Save vojtaholik/924b0877aba5bfe2a7dace10c6de01c7 to your computer and use it in GitHub Desktop.
Save vojtaholik/924b0877aba5bfe2a7dace10c6de01c7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const videoMachine = Machine({
id: "video",
initial: "loading",
context: {
video: null,
duration: 0,
elapsed: 0,
lastAction: undefined,
currentNote: ''
},
states: {
loading: {
on: {
LOADED: {
target: "ready",
actions: ["setVideo"]
},
FAIL: "failure"
}
},
ready: {
initial: "paused",
on: {
TAKE_NOTE: {
target: 'taking_note',
}
},
states: {
paused: {
on: {
PLAY: {
target: "playing",
actions: ["setElapsed", "playVideo", assign({lastAction: () => 'PLAY'})]
}
}
},
playing: {
on: {
TIMING: {
target: "playing",
actions: "setElapsed"
},
PAUSE: {
target: "paused",
actions: ["setElapsed", "pauseVideo", assign({lastAction: () => 'PAUSE'})]
},
END: "ended"
}
},
ended: {
on: {
PLAY: {
target: "playing",
actions: "restartVideo"
}
}
},
}
},
taking_note: {
initial: 'focused',
on: {
SUBMITTED: [{
target: 'ready.playing',
cond: (context) => context.lastAction === 'PLAY' && context.currentNote !== ''
}, {
target: 'ready',
cond: (context) => context.lastAction !== 'PLAY' && context.currentNote !== ''
}],
CANCELLED: [{
target: 'ready.playing',
cond: (context) => context.lastAction === 'PLAY'
}, {
target: 'ready',
cond: (context) => context.lastAction !== 'PLAY'
}],
},
states: {
focused: {
on: {
CHANGE: {
target: 'typing'
}
}
},
typing: {
entry: ["pauseVideo", assign({currentNote: (context, event) => event.currentNote})],
},
}
},
failure: {
type: "final"
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment