Last active
December 19, 2022 07:57
-
-
Save VinayakBagaria/17762ee472f7d08f166fe0b7cc144368 to your computer and use it in GitHub Desktop.
Transition states
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 machine = { | |
initial: 'cart', | |
states: { | |
cart: { | |
on: { | |
CHECKOUT: 'shipping', | |
}, | |
}, | |
shipping: { | |
on: { | |
NEXT: 'contact', | |
}, | |
}, | |
contact: { | |
on: {}, | |
}, | |
}, | |
}; |
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
function transition(state, event) { | |
return machine.states[state].on?.[event.type] ?? state; | |
} | |
console.log(transition('cart', { type: 'CHECKOUT' })); | |
// shipping | |
console.log(transition('cart', { type: 'UNKNOWN' })); | |
// cart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment