Skip to content

Instantly share code, notes, and snippets.

@alexanderson1993
Last active April 23, 2020 23:43
Show Gist options
  • Save alexanderson1993/679bd3c785cc334c5077391f0632961d to your computer and use it in GitHub Desktop.
Save alexanderson1993/679bd3c785cc334c5077391f0632961d 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 systemDamageMachine = Machine({
id:"SystemDamage",
initial:"damaged",
on:{
REPAIR:"repaired"
},
states: {
damaged:{
on: {
REQUEST_REPORT:"reportRequested"
}
},
reportRequested:{
on:{
REPORT:"report"
}
},
report:{
on:{
REACTIVATE:"reactivating"
}
},
reactivating:{
},
destroyed:{},
repaired:{final:true}
}
})
const internalCommMachine = Machine({
id: 'InternalComm',
initial: 'idle',
context: {
incomingCall:null,
outgoingCall:null
},
on: {
DAMAGED:"damaged"
},
states: {
damaged: {
invoke: {
src: systemDamageMachine,
onDone: 'idle'
}
},
idle: {
on: {
CALL_INTERNAL: {
target:'calling',
actions:"incoming",
cond:{type:"hasLocation"}
},
CALL_EXTERNAL: {
target:'calling',
actions:"outgoing",
cond:{type:"hasLocation"}
}
}
},
calling: {
on: {
CANCEL: {target:'idle',actions:["clear"]},
CONNECT_INCOMING: {target:'connected',
actions:["clearOutgoing"],
cond:{type:'canCallIncoming'}
},
CONNECT_OUTGOING: {target:'connected',
actions:["clearIncoming"],
cond:{type:"canCallOutgoing"}
}
}
},
connected: {
on: {
DISCONNECT:{target:"idle",actions:["clear"]}
}
},
}
},{
actions: {
clear: assign((context)=>{
return {
incomingCall:null,
outgoingCall:null
}
}),
incoming: assign({
incomingCall: (context, event) => context.incomingCall = event.callLocation
}),
outgoing: assign({
outgoingCall: (context, event) => context.outgoingCall = event.callLocation
}),
clearIncoming: assign({incomingCall:null}),
clearOutgoing: assign({outgoingCall:null}),
},
guards:{
hasLocation:(context, event) => {
return Boolean(event.callLocation)
},
canCallOutgoing:(context) => {
return Boolean(context.outgoingCall)
},
canCallIncoming:(context) => {
return Boolean(context.incomingCall)
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment