Skip to content

Instantly share code, notes, and snippets.

@chanced
Last active August 15, 2021 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanced/042c7d0ff4440390c985dda2289efcdb to your computer and use it in GitHub Desktop.
Save chanced/042c7d0ff4440390c985dda2289efcdb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const authMachine = Machine(
{
id: "auth",
initial: "initializing",
context: {
retries: 0,
user: undefined,
lastUpdated: undefined,
},
states: {
initializing: {
on: {
FETCH: "loading",
UPDATE_CREDENTIALS: {
target: "idle",
actions: ["updateCrednetials"],
},
},
},
loading: {
on: {
RESOLVE: "idle",
REJECT: "failure",
},
},
renewing: {
on: {
FETCH: "loading",
UPDATE_CREDENTIALS: {
target: "loading",
actions: ["updateCrednetials"],
},
},
},
failure: {
on: {
RETRY: {
target: "loading",
actions: assign({
retries: (context, event) => context.retries + 1,
}),
},
},
},
idle: {
after: {
30000: "renewing",
},
},
},
},
{
actions: {
updateCredentials: assign({
user: (ctx, event) => event.user,
}),
},
}
);
const rootMachine = Machine(
{
id: "root",
initial: "initializing",
context: {
authRef: undefined,
},
on: {
INITIALIZE: {
actions: ["createAuthMachine"],
},
},
states: {
initializing: {
on: {
INITIALIZE: {
actions: ["createAuthMachine", "loadPage"],
target: "loading",
},
},
},
loading: {
on: {
LOAD: {
actions: [],
target: "idle",
},
},
},
idle: {},
},
},
{
actions: {
createAuthMachine: assign({
authRef: (ctx, event) => spawn(authMachine, "authService"),
}),
loadPage:()=> {
// spawn (or invoke?) a nested machine for the given page. Also need to determine how to stop/start machines based on routing from sveltekit
}
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment