Last active
March 17, 2020 14:06
-
-
Save pafry7/4f2d03c29150a5f035afa71343f17c0d 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.8) resolve("got jwt"); | |
else reject("oopsy doopsy"); | |
}, 500); | |
}); | |
} | |
function getUserInfo() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if (Math.random() < 0.8) resolve("got jwt"); | |
else reject("oopsy doopsy"); | |
}, 1500); | |
}); | |
} | |
const authMachine = Machine({ | |
id: 'auth', | |
initial: 'loading', | |
states: { | |
loading: { | |
invoke:{ | |
id: "getJWT", | |
src: () => mockAuthenticate(), | |
onDone:{ | |
target:"has_jwt" | |
}, | |
onError: { | |
target:"has_no_jwt" | |
} | |
} | |
}, | |
has_jwt: { | |
initial:'loading', | |
states: { | |
loading:{ | |
invoke: { | |
id:"getUserInfo", | |
src: () =>getUserInfo(), | |
onDone: {target:"has_data"}, | |
onError:{target:"has_no_data"} | |
}, | |
}, | |
has_no_data: {}, | |
has_data: {type:'final'}, | |
}, | |
onDone:{ | |
target:"logged_in" | |
} | |
}, | |
has_no_jwt:{ | |
initial:"getPhoneNumber", | |
states:{ | |
getPhoneNumber:{}, | |
sendSMSCode:{} | |
} | |
}, | |
logged_in:{} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment