Created
May 27, 2021 20:24
-
-
Save xomaczar/2d9ad8e5b3b1dd2e11f23272c4bbde6c 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) | |
const states = { | |
active: { | |
onEntry: ['handleActive'], | |
on: { | |
USER_IDLE: 'idle', | |
USER_ACTIVE: 'active', | |
TOGGLE: 'idle', | |
LOGOUT: 'inactive', | |
}, | |
}, | |
idle: { | |
onEntry: ['handleIdleChange'], | |
on: { | |
RENEW: { | |
target: 'active', | |
actions: ['handleRenew'], | |
cond: 'validateToken', | |
}, | |
EXIT: 'inactive', | |
LOGOUT: { | |
actions: ['handleLogout'], | |
target: 'inactive', | |
}, | |
}, | |
}, | |
inactive: { | |
actions: assign({ | |
validToken: ({ context }) => (context.validToken = false), | |
}), | |
}, | |
}; | |
const aMachine = Machine({ | |
initial: 'active', | |
states, | |
actions: { | |
// predefine methods to handle transitions between states | |
// args to expect are (context, event) | |
handleIdleChange() {}, | |
handleActive() {}, | |
handleLogout() {}, | |
handleRenew() {}, | |
}, | |
guards: { | |
validateToken() {}, | |
}, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment