Created
October 21, 2023 21:40
-
-
Save wesen/c0e4104ed37ef6eb99af6ad5e7d193ab to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 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