Created
May 18, 2023 14:09
-
-
Save leegeunhyeok/6f0da8b5874d54a0aa0f2c405af6c8b9 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
const globalMachine = Machine( | |
{ | |
predictableActionArguments: true, | |
initial: 'idle', | |
context: { | |
user: null, | |
}, | |
states: { | |
idle: { | |
entry: ['onIdle'], | |
on: { | |
AUTO_LOGIN: 'loading', | |
}, | |
}, | |
loading: { | |
id: 'loading', | |
entry: ['onLoading'], | |
after: { | |
DEFAULT_TIMEOUT: 'unauthorized', | |
}, | |
invoke: { | |
src: 'loadUser', | |
onDone: { | |
target: 'authorized', | |
actions: 'setUser', | |
}, | |
onError: 'unauthorized', | |
}, | |
}, | |
authorized: { | |
initial: 'idle', | |
entry: ['onAuthorized'], | |
on: { | |
LOGOUT: 'unauthorized', | |
REFRESH: '.refreshing', | |
EDIT_USER: '.updating', | |
REWARD: '.calculating', | |
}, | |
states: { | |
idle: {}, | |
refreshing: { | |
entry: ['onRefreshing'], | |
invoke: { | |
src: 'loadUser', | |
onDone: { | |
target: 'idle', | |
actions: 'setUser', | |
}, | |
onError: '#unauthorized', | |
}, | |
}, | |
updating: { | |
entry: ['onUpdateUser'], | |
invoke: { | |
src: 'updateUser', | |
onDone: { | |
target: 'idle', | |
actions: 'setUser', | |
}, | |
onError: 'idle', | |
}, | |
}, | |
calculating: { | |
entry: ['onCalculating'], | |
invoke: { | |
src: 'calculateLevel', | |
onDone: { | |
target: 'idle', | |
actions: 'setUser', | |
}, | |
onError: 'idle', | |
}, | |
}, | |
}, | |
}, | |
unauthorized: { | |
id: 'unauthorized', | |
initial: 'idle', | |
entry: ['onUnauthorized', 'removeUser'], | |
invoke: { | |
src: 'cleanup', | |
}, | |
on: { | |
LOGIN: { | |
target: '.validating', | |
}, | |
}, | |
states: { | |
idle: {}, | |
validating: { | |
invoke: { | |
src: 'saveUser', | |
onDone: '#loading', | |
onError: 'idle', | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
setUser: assign({ user: (_context, event) => event.data }), | |
removeUser: assign({ user: (_context, _event) => null }), | |
onIdle: () => { | |
}, | |
onLoading: () => { | |
Logger.debug(TAG, 'actions.onLoading'); | |
}, | |
onAuthorized: (_context, event) => { | |
}, | |
onRefreshing: () => { | |
}, | |
onUpdateUser: (_context, event) => { | |
}, | |
onCalculating: (_context, event) => { | |
}, | |
onUnauthorized: () => { | |
}, | |
}, | |
services: { | |
loadUser: async () => { | |
}, | |
updateUser: async (context, event) => { | |
return event.user; | |
}, | |
saveUser: async (_context, event) => { | |
}, | |
calculateLevel: async (context, event) => { | |
return event.user; | |
}, | |
cleanup: async () => { | |
}, | |
}, | |
delays: { | |
DEFAULT_TIMEOUT: 3 * 1000, | |
}, | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment