Last active
August 15, 2021 19:45
-
-
Save chanced/042c7d0ff4440390c985dda2289efcdb 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
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