Skip to content

Instantly share code, notes, and snippets.

Christian Nwamba christiannwamba

View GitHub Profile
View machine.js
// DEMO: Call events in child machines
// NOTE: Child machine viz is not supported yet
const childMachine = Machine({
id: 'child',
initial: 'step1',
states: {
step1: {
on: { STEP: 'step2' },
View machine.js
// DEMO: Call events anywhere (including parent machines/states) with callbacks
const echoCallbackHandler = (context, event) =>
(callback, onEvent) => {
onEvent(e => {
if(e.type === 'HEAR') {
callback('ECHO')
}
})
}
View machine.js
// DEMO: Async and Promises
const fetchCuteAnimals = () => {
// Uncomment to test failure
// return Promise.reject()
return fetch('https://www.reddit.com/r/aww.json')
.then(res => res.json())
.then(data => data.data.children.map(child => child.data))
}
View machine.js
//DEMO: Delay Events and
// Use after instead of `on` for timer events
const states = {
green: {
after: {
RED_TIMER: 'yellow'
}
},
View machine.js
// DEMO: Null or Transient Transitions with ''
// Cick TRY 3 times and it will get to success but will keep coming back to idle at 1 and 2 time
// You can see the state increment in the STATE tab when you click TRY
const ifAtFirstYouDontSucceed = Machine(
{
id: 'tryTryAgain',
initial: 'idle',
context: {
tries: 0,
View machine.js
// DEMO: History State Deep
const heatedStates = {
lowHeat: {
on: { TOGGLE_HEAT: 'highHeat' }
},
highHeat: {
on: { TOGGLE_HEAT: 'lowHeat' }
}
}
View machine.js
// DEMO: History State (Shallow)
const poweredOnStates = {
lowHeat: {
on: { TOGGLE_HEAT: 'highHeat' }
},
highHeat: {
on: { TOGGLE_HEAT: 'lowHeat' }
},
View machine.js
// DEMO: Guarding
const vendingMachineMachine = Machine(
{
id: "vendingMachine",
initial: "idle",
context: {
deposited: 0
},
states: {
View machine.js
// DEMO: Side effects with activities
// Look for beebing in the console
const initial = "idle";
const states = {
idle: {
on: {
ALARM: "alarming"
}
},
View machine.js
// DEMO: Calling events from other events
const echoMachine = Machine({
id: "echo",
initial: "listening",
states: {
listening: {
on: {
SPEAK: {
// Calls the ECHO action
You can’t perform that action at this time.