Skip to content

Instantly share code, notes, and snippets.

@pafry7
Last active March 11, 2020 13:09
Show Gist options
  • Save pafry7/fa04f1ca3a38fd273b4ea03de033f638 to your computer and use it in GitHub Desktop.
Save pafry7/fa04f1ca3a38fd273b4ea03de033f638 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)
function mockAuthenticate() {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() < 0.5) resolve("got jwt");
else reject("oopsy doopsy");
}, 1500);
});
}
const authMachine = Machine({
id: 'authentication',
initial: 'authenticating',
states: {
// checking if one has jwt token
// in local storage
authenticating: {
initial:"loggedOut",
states:{
loggedOut:{
}
},
invoke: {
id: "authenticateUser",
src: ctx => {
return mockAuthenticate();
},
onDone: {
target: "loggedIn",
},
onError: {
target: "authenticating.loggedOut"
}
}
},
loaded:{on:{
GOT_JWT_AND_DATA: {
target:"loggedIn"
},
GOT_JWT_NOT_DATA:{
target:"uncompletedData"
},
}},
uncompletedData:{
on:{
RESOLVE:{
target:"loggedIn"
},
REJECTED:{
target:"failure"
}
}
},
failure:{
on:{
RETRY:{
target:"authenticating"
}
}
},
loggedIn:{type:"final"},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment