Skip to content

Instantly share code, notes, and snippets.

@jhesgodi
Last active February 26, 2021 01:43
Show Gist options
  • Save jhesgodi/c408c35a5181e3a09cb2df3a67cf76a7 to your computer and use it in GitHub Desktop.
Save jhesgodi/c408c35a5181e3a09cb2df3a67cf76a7 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 displayWorkingLogs = (ctx, evt) => {
const log = () => {
console.log('🌬')
}
log()
const intRef = setInterval(log, 1000)
return () => clearInterval(intRef)
}
const setContext = (key) => (value) => () => {
console.log(`set => ${key}: ${value};`)
assign({ [key]: value });
}
const log = () => console.log('----->')
const intensityMachine = {
initial: 'low',
states: {
low: {
on: { SWITCH_INTENSITY: 'medium' }
},
medium: {
on: { SWITCH_INTENSITY: 'high' }
},
high: {
on: { SWITCH_INTENSITY: 'low' }
},
}
}
const functionModeMachine = {
initial: 'cool',
states: {
cool: {
entry: ['setCool'],
on: { SWITCH_FUNCTION: 'heat' }
},
heat: {
entry: ['setHeat'],
on: { SWITCH_FUNCTION: 'cool' }
}
}
}
const fetchMachine = Machine({
id: 'AirCon',
initial: 'off',
context: {
status: {
on: false,
func: '',
oscillating: false
}
},
states: {
off: {
entry: ['log', 'setOff'],
on: { SWITCH_POWER: 'on.history' },
},
on: {
type: 'parallel',
entry: ['log', 'setOn'],
activities: ['working'],
on: { SWITCH_POWER: 'off' },
states: {
intensity: intensityMachine,
mode: functionModeMachine,
history: {
type: 'history',
history: 'deep'
}
}
}
}
}, {
activities: {
working: displayWorkingLogs,
},
actions: {
log,
setOn: setContext('on')(true),
setOff: setContext('on')(false),
setHeat: setContext('func')('🥵'),
setCool: setContext('func')('🥶'),
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment