console
const { Machine, interpret } = XState
const machine = Machine({
id: 'lights',
initial: 'nolights',
states: {
nolights: {
on: {
p1: {
target: 'l1',
actions: 'turnOnL1'
},
p2: {
target: 'l2',
actions: 'turnOnL2'
}
},
},
l1: {
on: {
p1: {
target: 'l2',
actions: 'turnOnL2'
},
p2: {
target: 'l3',
actions: 'turnOnL3'
}
}
},
l2: {
on: {
p1: {
target: 'nolights',
actions: ['turnOffAll']
},
p2: {
target: 'nolights',
actions: ['turnOffAll']
}
}
},
l3: {
on: {
p1: {
target: 'nolights',
actions: ['turnOffAll']
},
p2: {
target: 'nolights',
actions: ['turnOffAll']
}
}
},
}
},
{
actions: {
turnOnL1 (context, event) {
console.log('turnOnL1', JSON.stringify(event))
},
turnOnL2 (context, event) {
console.log('turnOnL2', event)
},
turnOnL3 (content, event) {
console.log('turnOnL3', event)
},
truenOffAll (context, event) {
console.log('tuenOffAll', event)
}
}
})
const service = interpret(machine).start()
console.log(service.send('p1').value)
console.log(service.send('p1').value)
console.log(service.send('p1').value)
<script src="https://unpkg.com/xstate@4/dist/xstate.js"></script>