Skip to content

Instantly share code, notes, and snippets.

@pociej
Created October 18, 2023 07:22
Show Gist options
  • Save pociej/070ad987f307e424013940441eb8a73b to your computer and use it in GitHub Desktop.
Save pociej/070ad987f307e424013940441eb8a73b to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const machine = Machine({
types: {},
id: "simpleMachine",
initial: "initial",
context: { value: 0 },
states: {
initial: {
on: {
NEXT: { target: "oracle" },
},
},
oracle: {
on: {
NEXT: {
target: "choose_your_name",
actions: assign({ value: () => Math.random() }),
},
},
},
choose_your_name: {
on: {
NEXT: [
{
target: "john",
guard: ({ context }) => {
return context.value > 0.5;
},
},
{
target: "michael",
guard: ({ context }) => {
return context.value < 0.5;
},
},
],
},
},
john: {
on: {
NEXT: "choose_your_color",
},
},
michael: {
on: {
NEXT: "choose_your_color",
},
},
choose_your_color: {
on: {
NEXT: [
{
target: "blue",
guard: ({ context }) => {
return context.value > 0.5;
},
},
{
target: "red",
guard: ({ context }) => {
return context.value < 0.5;
},
},
],
},
},
blue: {
on: {
NEXT: "success",
},
},
red: {
on: {
NEXT: "success",
},
},
success: {
type: "final",
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment