Last active
October 3, 2019 04:37
-
-
Save njdancer/54974049c748e70b28215c66b40eb5d6 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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) | |
// { | |
// "type": "AUTH", | |
// "username": "", | |
// "password": "" | |
// } | |
const newToken = "jwt" | |
const storedToken = newToken | |
const loadToken = () => | |
new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(storedToken) | |
}, 1000) | |
}) | |
const authUser = () => | |
new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(newToken) | |
}, 1000) | |
}) | |
const refreshToken = () => | |
new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(newToken) | |
}, 1000) | |
}) | |
function isTokenSet(context, event) { | |
return context.token != null | |
} | |
function isCredentialsSet(context, event) { | |
return event.username != null && event.password != null | |
} | |
function handleAuthError(context, event) { | |
alert("Auth failed! Try again") | |
} | |
let failed = false | |
const loadResource = () => | |
new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve("resource data") | |
}, 1000) | |
}) | |
const resourceMachine = Machine( | |
{ | |
context: { | |
data: null, | |
error: null, | |
failGracefully: true, | |
ttl: 10000, | |
url: "http://sos2us.com/activeIncidents", | |
}, | |
id: "resource", | |
initial: "loading", | |
states: { | |
failed: { | |
on: { | |
REFRESH: "refreshing", | |
}, | |
}, | |
loaded: { | |
entry: 'updateParent', | |
after: { | |
TTL: "refreshing", | |
}, | |
on: { | |
REFRESH: "refreshing", | |
}, | |
}, | |
loading: { | |
invoke: { | |
id: "loadResource", | |
onDone: { | |
actions: assign({ data: (context, event) => event.data }), | |
target: "loaded", | |
}, | |
onError: { | |
actions: assign({ error: (context, event) => event.data }), | |
target: "failed", | |
}, | |
src: (context, event) => loadResource(context.url), | |
}, | |
}, | |
refreshFailed: { | |
on: { | |
"": [ | |
{ | |
cond: context => context && context.failGracefully, | |
target: "loaded", | |
}, | |
{ target: "failed" }, | |
], | |
}, | |
}, | |
refreshing: { | |
invoke: { | |
id: "refreshResource", | |
onDone: { | |
actions: assign({ | |
data: (context, event) => event.data, | |
error: (context, event) => null, | |
}), | |
target: "loaded", | |
}, | |
onError: { | |
actions: assign({ error: (context, event) => event.data }), | |
target: "refreshFailed", | |
}, | |
src: (context, event) => loadResource(context.url), | |
}, | |
}, | |
}, | |
}, | |
{ | |
delays: { | |
TTL: (context, event) => { | |
return context.ttl | |
}, | |
}, | |
actions: { | |
updateParent: (context, event) => sendParent({type: "UPDATE", data:event.data}) | |
} | |
} | |
) | |
// const resourceManagerMachine = Machine({ | |
// id: 'resourceManager', | |
// type: 'parallel', | |
// states: { | |
// activeIncidents: { | |
// invoke: { | |
// src: resourceMachine, | |
// } | |
// } | |
// } | |
// }) | |
// const sessionMachine = Machine( | |
// { | |
// context: { | |
// incidentTypes: null, | |
// token: null, | |
// }, | |
// id: "session", | |
// initial: "start", | |
// states: { | |
// auth: { | |
// invoke: { | |
// id: "authUser", | |
// onDone: { | |
// actions: assign({ token: (context, event) => event.data }), | |
// target: "main", | |
// }, | |
// onError: { | |
// actions: "handleAuthError", | |
// target: "credentials", | |
// }, | |
// src: (context, event) => authUser(), | |
// }, | |
// }, | |
// credentials: { | |
// on: { | |
// AUTH: { | |
// cond: "isCredentialsSet", | |
// target: "auth", | |
// }, | |
// }, | |
// }, | |
// loaded: { | |
// on: { | |
// "": [ | |
// { cond: "isTokenSet", target: "main" }, | |
// { target: "credentials" }, | |
// ], | |
// }, | |
// }, | |
// loading: { | |
// invoke: { | |
// id: "loadToken", | |
// onDone: { | |
// actions: assign({ token: (context, event) => event.data }), | |
// target: "loaded", | |
// }, | |
// onError: "loading", | |
// src: (context, event) => loadToken(), | |
// }, | |
// }, | |
// main: { | |
// initial: "running", | |
// on: { | |
// RESET: { | |
// actions: assign({ token: () => null }), | |
// target: "credentials", | |
// }, | |
// }, | |
// onDone: "credentials", | |
// states: { | |
// refreshingToken: { | |
// invoke: { | |
// id: "refreshToken", | |
// onDone: { | |
// actions: assign({ token: (context, event) => event.data }), | |
// target: "running", | |
// }, | |
// onError: { | |
// actions: assign({ token: () => null }), | |
// target: "sessionExpired", | |
// }, | |
// src: (context, event) => refreshToken(), | |
// }, | |
// }, | |
// running: { | |
// on: { | |
// REFRESH_TOKEN: "refreshingToken", | |
// }, | |
// states: { | |
// fetchingIncidentTypes: { | |
// invoke: { | |
// data: { | |
// ttl: (context, event) => 600, | |
// url: (context, event) => "/incidentTypes", | |
// }, | |
// id: "incidentTypesFetcher", | |
// src: resourceMachine, | |
// }, | |
// on: { | |
// UPDATE: { | |
// actions: assign({ | |
// incidentTypes: (context, event) => { | |
// event.data | |
// }, | |
// }), | |
// }, | |
// }, | |
// }, | |
// }, | |
// type: "parallel", | |
// }, | |
// sessionExpired: { | |
// type: "final", | |
// }, | |
// }, | |
// }, | |
// start: { | |
// on: { | |
// "": "loading", | |
// }, | |
// }, | |
// }, | |
// }, | |
// { | |
// actions: { handleAuthError, loadToken, refreshToken }, | |
// guards: { isCredentialsSet, isTokenSet }, | |
// } | |
// ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment