Skip to content

Instantly share code, notes, and snippets.

@chukinas
Last active May 7, 2020 20:06
Show Gist options
  • Save chukinas/34a459320c03883d6606fbedec4ac50d to your computer and use it in GitHub Desktop.
Save chukinas/34a459320c03883d6606fbedec4ac50d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const buttonA = {
id: 'buttonA',
initial: 'hist',
states: {
off: {
on: {
TOGGLEA: 'on'
}
},
on: {
on: {
TOGGLEA: 'off'
}
},
hist: {
type: 'history',
target: 'off',
},
},
}
const buttonFactory = id => {
const toggleName = `TOGGLE${id}`
return {
id: `button${id}`,
initial: 'hist',
states: {
off: {
on: {
[toggleName]: 'on'
}
},
on: {
on: {
[toggleName]: 'off'
}
},
hist: {
type: 'history',
target: 'off',
},
},
}
}
const buttonB = {
id: 'buttonB',
initial: 'lastSelected',
states: {
off: {
on: {
TOGGLEB: 'on'
}
},
on: {
on: {
TOGGLEB: 'off'
}
},
hist: {
type: 'history',
target: 'off',
}
},
}
const buttons = {
id: 'buttons',
type: 'parallel',
states: {
button1: buttonFactory(1),
buttonB
},
on: {
SLEEP: '#idle',
offA: {
target: [
'#button1.off',
'#buttonB.hist'
]
},
offB: {
target: [
'#buttonB.off',
'#button1.hist'
]
},
}
}
const parallelTest = Machine({
id: 'test',
initial: 'idle',
states: {
idle: {
id: 'idle',
on: {
START: 'buttons',
}
},
buttons,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment