Skip to content

Instantly share code, notes, and snippets.

@raarts
Last active June 12, 2020 20:42
Show Gist options
  • Save raarts/ed5b829bf075ac3661d0c640e80488a2 to your computer and use it in GitHub Desktop.
Save raarts/ed5b829bf075ac3661d0c640e80488a2 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 neemopMachine = Machine({
id: 'neemop',
context: {
number: '',
retry: 0,
},
initial: 'offline',
states: {
offline: {
on: {
ANSWER: 'online',
},
},
done: {
type: 'final',
},
online: {
initial: 'welcome',
states: {
welcome: {
entry: [
'playGoodMorning',
'playWelcome',
],
on: {
'': '#clearnumber'
}
},
getnumber: {
initial: 'clearnumber',
states: {
clearnumber: {
id: 'clearnumber',
entry: [
'clearNumber',
'playEnterNumberToDial',
'playConfirmUsingHash'
],
after: {
DIGIT_TIMEOUT: 'counting_down',
},
},
counting_down: {
after: {
RESPONSE_TIMEOUT: [
{
target: '#done',
cond: 'maxRetry',
actions: [
'playNoValidInputReceived'
]
},
{
target: '#clearnumber',
cond: 'noNumber',
actions: [
'incRetry', 'playNoValidInputReceived'
]
},
{
target: '#dialing'
}
],
},
on: {
'DTMF': {
target: 'counting_down',
actions: [
'addDigit',
(context, event) => console.log(context)
]
},
},
},
},
on: {
HASH: 'dialing',
},
},
dialing: {
id: 'dialing',
entry: 'clearRetry',
on: {
ANSWER: 'incall',
UNREACHABLE: '#clearnumber',
},
after: {
DIAL_TIMEOUT: '#clearnumber',
}
},
incall: {
initial: 'call',
states: {
call: {
on: {
LASTMINUTEWARN: 'lastMinute',
},
},
lastMinute: {
on: {
LAST30SECWARN: 'last30secs',
},
},
last30secs: {
on: {
NOMONEYLEFT: '#done',
},
},
},
},
done: {
id: 'done',
entry: 'playLineWillBeDisconnected',
type: 'final',
}
},
on: {
HANGUP: 'done',
},
},
},
},
{
guards: {
noNumber: (context, event) => { return context.number.length < 10},
maxRetry: (context, event) => { return context.retry >= 2 },
isMorning: (context, event) => { return true },
},
actions: {
addDigit: assign({ number: (context, event) => context.number + (event.value || '1')}),
clearNumber: assign({ number: (context, event) => '' }),
incRetry: assign({ retry: (context, event) => context.retry + 1 }),
clearRetry: assign({ retry: (context, event) => 0 }),
playGoodMorning: () => console.log('GoodMorning'),
playWelcome: () => console.log('Welcome to Neemop'),
playEnterNumberToDial: () => console.log('Enter the number you want to dial'),
playConfirmUsingHash: () => console.log('Confirm using the hash key'),
playNoValidInputReceived: () => console.log('No valid number was received'),
playLineWillBeDisconnected: () => console.log('The line will now be disconnected'),
},
delays: {
DIGIT_TIMEOUT: 200,
RESPONSE_TIMEOUT: 5000,
DIAL_TIMEOUT: 60000,
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment