Skip to content

Instantly share code, notes, and snippets.

@sytabaresa
Last active October 26, 2019 17:26
Show Gist options
  • Save sytabaresa/655cd249767f498a6a3cab48ec21f58e to your computer and use it in GitHub Desktop.
Save sytabaresa/655cd249767f498a6a3cab48ec21f58e to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const { cancel } = actions
const playerMachine = Machine(
{
id: 'player',
initial: 'idle',
context: {
promotion: 0,
promotionCounter: 0
},
states: {
idle: {
on: {
PLAY: [
{target: 'promo', cond: 'showPromo'},
{target: 'changing'}
]
}
},
changing: {
entry: 'setIdleTimer',
onExit: 'clearIdleTimer',
on: {
PLAY: [
{target: 'idle', actions: ["displaySong", 'incrementPromotionalCounter']}
],
TIMER: [
{target: 'idle', actions: ["displaySong", 'incrementPromotionalCounter']}
],
END: {target: 'idle'},
}
},
promo: {
on: {
END: {target: 'idle'}
}
}
}
},
{
guards: {
showPromo: (context, event) => context.promotionCounter > 10
},
actions: {
incrementPromotionalCounter: assign({
promotionCounter: (context, event) => context.promotionCounter + 1
}),
displaySong: (context, event) => console.log("change"),
setIdleTimer: send('TIMER', {id: 'idleTimer',delay:1000}),
clearIdleTimer: cancel('idleTimer')
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment