Skip to content

Instantly share code, notes, and snippets.

@arbaaz
Last active April 11, 2020 16:18
Show Gist options
  • Save arbaaz/4f033d2dcceb188bdbafb71446f6118a to your computer and use it in GitHub Desktop.
Save arbaaz/4f033d2dcceb188bdbafb71446f6118a to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const timeOfDayMachine = Machine({
id: 'psm',
initial: 'namaz',
context: {
time: new Date(Date.now())
},
on: {
Root_Mode: {
actions: assign({ level: 'root' })
},
Start_Work: [{ target: 'work' }],
},
states: {
namaz: {
type: 'compound',
initial: 'dawn',
onDone: 'dinner',
states: {
dawn: {
on: {
'': [
{ target: 'fajr', cond: 'isFajr' },
{ target: 'zohar', cond: 'isZohar' },
{ target: 'asar', cond: 'isAsar' },
{ target: 'maghrib', cond: 'isMaghrib' },
{ target: 'isha', cond: 'isIsha' }
]
}
},
fajr: {
on: {
NEXT: '#psm.work'
}
},
zohar: {
on: {
NEXT: 'asar'
}
},
asar: {
on: {
NEXT: 'maghrib'
}
},
maghrib: {
on: {
NEXT: 'isha'
}
},
isha: {
on: {
NEXT: 'done'
},
},
done: {
type: 'final'
}
},
},
breakfast: {
on: {
NEXT: 'work'
}
},
work: {
meta: {
message: 'The request failed.'
},
on: {
FIRST_BREAK: 'namaz.zohar',
SECOND_BREAK: 'namaz.asar'
}
},
dinner: {
on: {
NEXT: 'quran'
},
},
quran: {
type: 'compound',
initial: 'pending',
states: {
pending: {
on: {
NEXT: 'read'
}
},
read: {
on: {
NEXT: 'sleep'
}
},
sleep: {
type: 'final'
}
},
}
}
}, {
guards: {
isFajr: (context) => {
return context.time.getHours() < 6;
},
isZohar: (context) => {
return context.time.getHours() > 12 && context.time.getHours() < 16;
},
isAsar: (context) => {
return context.time.getHours() > 16 && context.time.getHours() < 18;
},
isMaghrib: (context) => {
return context.time.getHours() > 18 && context.time.getHours() < 19;
},
isIsha: (context) => {
return context.time.getHours() >= 19;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment