Skip to content

Instantly share code, notes, and snippets.

@radiodario
Last active February 2, 2022 14:19
Show Gist options
  • Save radiodario/ba13c8845fd6816cd9e1a9b3e193ae50 to your computer and use it in GitHub Desktop.
Save radiodario/ba13c8845fd6816cd9e1a9b3e193ae50 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 callingMachine = Machine({
id: "calling",
initial: "idle",
context: {
retries: 0,
duration: 5 * 60,
elapsed: 0,
interval: 0.1
},
states: {
idle: {
on: {
DIAL: {
target: "connecting"
},
INCOMING: {
target: "incoming"
}
}
},
incoming: {
on: {
ACCEPT: {
target: "connecting",
},
REJECT: {
target: "idle",
},
}
},
connecting: {
on: {
CONNECTED: "inCall",
WAIT: "connecting",
TIMEOUT: "notAnswered",
REJECTED: "rejected"
}
},
inCall: {
invoke: {
src: (ctx) => (cb) => {
const interval = setInterval(() => {
cb('TICK');
}, 1000 * ctx.interval);
return () => {
clearInterval(interval);
};
}
},
on: {
MUTE: "muted",
PAUSE: "onHold",
DISCONNECTED: "connecting",
HANGUP: "finished",
TICK: {
actions: assign({
elapsed: (ctx) => +(ctx.elapsed + context.interval).toFixed(2)
})
},
'': {
target: 'finished',
cond: (ctx) => ctx.elapsed >= duration,
}
}
},
muted: {
on: {
UNMUTE: "inCall",
HANGUP: "finished",
DISCONNECTED: "connecting"
}
},
onHold: {
on: {
// only callee can put a call on hold
RESUME: "inCall",
HANGUP: "finished",
DISCONNECTED: "connecting"
}
},
notAnswered: {
on: {
REDIAL: "connecting",
DONE: "idle"
}
},
rejected: {
on: {
DONE: "idle"
}
},
finished: {
on: {
REDIAL: "connecting",
DONE: "idle"
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment