Skip to content

Instantly share code, notes, and snippets.

@LostKobrakai
Last active May 10, 2020 13:17
Show Gist options
  • Save LostKobrakai/3b824a6fe12f6dc3adc22dfef1de3672 to your computer and use it in GitHub Desktop.
Save LostKobrakai/3b824a6fe12f6dc3adc22dfef1de3672 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 fetchMachine = Machine({
id: 'timer',
initial: 'stopped',
context: {
duration: 0,
endDate: null
},
states: {
stopped: {
on: {
START: {
cond: (_, event) => event.endDate,
target: 'running',
actions: assign({
duration: (context, event) =>
new Date(event.endDate) - (new Date()),
endDate: (context, event) =>
new Date(event.endDate),
})
}
}
},
running: {
on: {
TICK: [
{
cond: (context) =>
context.endDate <= (new Date()),
target: 'stopped'
},
{
actions: assign({
duration: (context, event) =>
context.endDate - (new Date())
})
}
],
PAUSE: 'paused'
}
},
paused: {
on: {
START: 'running',
STOP: {
target: 'stopped',
actions: assign({
duration: 0
})
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment