Last active
March 11, 2020 13:09
-
-
Save pafry7/fa04f1ca3a38fd273b4ea03de033f638 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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