Skip to content

Instantly share code, notes, and snippets.

@wesen
Created October 21, 2023 21:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesen/c0e4104ed37ef6eb99af6ad5e7d193ab to your computer and use it in GitHub Desktop.
Save wesen/c0e4104ed37ef6eb99af6ad5e7d193ab to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchMachine = Machine({
id: 'telemetry',
initial: 'A_OFF',
context: {
timestampStart: null,
currentADuration: 0,
currentCounter: 0,
},
states: {
A_OFF: {
on: {
'A-ON': {
target: 'A_ON',
actions: ['setTimestampStart']
}
}
},
A_ON: {
on: {
'A-OFF': {
target: 'A_OFF',
actions: ['calculateDuration', 'resetIfNecessary']
},
B: {
actions: ['incrementCounter']
}
}
}
}
},
{
actions: {
setTimestampStart: XState.assign({
timestampStart: (context, event) => event.timestamp
}),
calculateDuration: XState.assign({
currentADuration: (context, event) => context.currentADuration + (event.timestamp - context.timestampStart)
}),
incrementCounter: XState.assign({
currentCounter: (context) => context.currentCounter + 1
}),
resetIfNecessary: XState.assign({
currentADuration: (context) => context.currentADuration >= 3600 ? 0 : context.currentADuration,
currentCounter: (context) => context.currentADuration >= 3600 ? 0 : context.currentCounter
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment