Skip to content

Instantly share code, notes, and snippets.

@samwarnick
Created September 11, 2019 15:09
Show Gist options
  • Save samwarnick/00a017a625206f2ab066a20649134003 to your computer and use it in GitHub Desktop.
Save samwarnick/00a017a625206f2ab066a20649134003 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: 'pomodoro',
initial: 'idle',
context: {
pomodoros: 0,
rounds: 0,
extra: 0
},
states: {
idle: {
on: {
START: 'working'
}
},
working: {
entry: ['pomodoroComplete'],
on: {
TAKE_SHORT_BREAK: {
target: 'shortBreak',
cond: 'notTimeForLongBreak'
},
TAKE_LONG_BREAK: {
target: 'longBreak',
cond: 'timeForLongBreak'
},
KEEP_GOING: {
target: 'working',
actions: ['doingExtraWork']
}
}
},
shortBreak: {
on: {
TIMER: 'working',
}
},
longBreak: {
exit: ['startOver'],
on: {
TIMER: 'working'
}
}
}
}, {
actions: {
pomodoroComplete: assign({
pomodoros: (context, event) => context.pomodoros + 1
}),
startOver: assign({
pomodoros: 0,
rounds: (context, event) => context.rounds + 1
}),
doingExtraWork: assign({
extra: (context, event) => context.extra + 1
})
},
guards: {
timeForLongBreak: (context, event) => {
return context.pomodoros > 3;
},
notTimeForLongBreak: (context, event) => {
return context.pomodoros < 4;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment