Skip to content

Instantly share code, notes, and snippets.

@Krutie
Last active April 21, 2021 05:41
Show Gist options
  • Save Krutie/c9a0e82b422e164c27414a6be1c5e194 to your computer and use it in GitHub Desktop.
Save Krutie/c9a0e82b422e164c27414a6be1c5e194 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// TRAFFIC-LIGHT MACHINE
// ===================
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const alertsMachine = Machine({
id: 'alerts',
initial: 'green',
context: {
yellowRetries: 0,
redRetries: 0,
greenRetries: 0
},
states: {
green: {
on: {
// WARN: 'yellow',
WARN: {
target: 'yellow',
actions: assign({
yellowRetries: (context, event) =>
context.yellowRetries + 1
})
},
PANIC: {
target: 'red',
actions: assign({
redRetries: (context, event) => context.redRetries + 1
})
}
}
},
yellow: {
on: {
PANIC: {
target: 'red',
actions: assign({
redRetries: (context, event) => context.redRetries + 1
})
},
CLEAR: {
target: 'green',
actions: assign({
greenRetries: (context, event) => context.greenRetries + 1
})
}
}
},
red: {
on: {
CALM: {
target: 'yellow',
actions: assign({
yellowRetries: (context, event) =>
context.yellowRetries + 1
})
},
CLEAR: {
target: 'success',
actions: assign({
greenRetries: (context, event) => context.greenRetries + 1
})
}
}
},
success: {
type: 'final'
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment