Last active
March 21, 2020 14:29
-
-
Save jaetask/d3899db027fdb91d937a25a61561d580 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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 G99_REQUIRED_THRESHOLD = 3.68; | |
const assignByValue = (context, event) => event.value | |
const updateExistingPv = assign({existingPv: assignByValue}); | |
const updateProposedPv = assign({proposedPv: assignByValue}); | |
const updateBatteryOutput = assign({batteryOutput: assignByValue}); | |
const updateBatteryEnabled = assign({isBatteryEnabled: assignByValue}); | |
const updateG99DecisionOverridden = assign({hasG99DecisionBeenOverridden: assignByValue}); | |
const calcTotalOnSiteGeneration = assign({ | |
totalOnSiteGeneration: (context, event) => { | |
let result = context.existingPv + context.proposedPv; | |
if (context.isBatteryEnabled) { | |
result += context.batteryOutput | |
} | |
return result | |
} | |
}) | |
const calcIsG99Required = assign({ | |
isG99Required: (context, event) => | |
context.totalOnSiteGeneration >= G99_REQUIRED_THRESHOLD | |
}) | |
const calcDecision = assign({ | |
decision: (context, event) => | |
context.hasG99DecisionBeenOverridden ? true : context.isG99Required | |
}) | |
const logContext = (context, event) => { | |
console.log(context) | |
} | |
const tabG99Machine = Machine({ | |
id: 'tab_g99', | |
initial: 'idle', | |
context: { | |
existingPv: 0, | |
proposedPv: 0, | |
batteryOutput: 0, | |
isBatteryEnabled: false, | |
totalOnSiteGeneration: 0, | |
isG99Required: false, | |
hasG99DecisionBeenOverridden: false, | |
decision: false, | |
}, | |
states: { | |
'idle': { | |
on: { | |
EXISTING_PV: { | |
target: 'calculating', | |
actions: 'updateExistingPv' | |
}, | |
PROPOSED_PV: { | |
target: 'calculating', | |
actions: 'updateProposedPv' | |
}, | |
BATTERY: { | |
target: 'calculating', | |
actions: 'updateBatteryOutput' | |
}, | |
BATTERY_ENABLED: { | |
target: 'calculating', | |
actions: 'updateBatteryEnabled' | |
}, | |
OVERRIDE_DECISION: { | |
target: 'calculating', | |
actions: 'updateG99DecisionOverridden' | |
} | |
} | |
}, | |
'calculating': { | |
on: { | |
'': { // Transient transition | |
target: 'idle', | |
actions: [ | |
'calcTotalOnSiteGeneration', | |
'calcIsG99Required', | |
'calcDecision', | |
'logContext' | |
] | |
} | |
} | |
}, | |
} | |
}, | |
{ | |
actions: { | |
updateExistingPv, | |
updateProposedPv, | |
updateBatteryOutput, | |
updateBatteryEnabled, | |
updateG99DecisionOverridden, | |
calcTotalOnSiteGeneration, | |
calcIsG99Required, | |
calcDecision, | |
logContext | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment