Last active
April 15, 2020 21:47
-
-
Save cazzer/287fce1cdc761ecf7461d01664e97ce9 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
async function authorizeCharge() { | |
return true | |
} | |
const canAddCharge = (context) => { | |
return context.batteryLevel < context.batteryCapacity | |
} | |
const cantAddCharge = (context) => { | |
return context.batteryLevel === context.batteryCapacity | |
} | |
const resetBattery = assign({ | |
batteryCapacity: 0, | |
batteryLevel: 0 | |
}) | |
const setBattery = assign({ | |
batteryCapacity: (context, event) => event.batteryCapacity, | |
batteryLevel: (context, event) => event.batteryLevel | |
}) | |
const addCharge = assign({ | |
batteryLevel: (context) => context.batteryLevel + 1 | |
}) | |
const chargePointStateMachine = Machine({ | |
id: 'status', | |
initial: 'Available', | |
context: { | |
batteryCapacity: 0, | |
batteryLevel: 0 | |
}, | |
states: { | |
Available: { | |
entry: resetBattery, | |
on: { | |
PLUGIN: { | |
target: 'Preparing', | |
actions: setBattery | |
}, | |
} | |
}, | |
Preparing: { | |
on: { | |
UNPLUG: 'Available', | |
START_CHARGE: 'Charging', | |
ERROR: 'Faulted' | |
}, | |
invoke: { | |
id: 'Authorize', | |
src: authorizeCharge, | |
onDone: 'Charging' | |
} | |
}, | |
Charging: { | |
after: { | |
60000: [{ | |
target: 'Charging', | |
actions: addCharge, | |
cond: 'canAddCharge' | |
}, { | |
target: 'Finishing', | |
cond: 'cantAddCharge' | |
}] | |
}, | |
on: { | |
UNPLUG: 'Available', | |
ERROR: 'Faulted' | |
} | |
}, | |
Finishing: { | |
on: { | |
UNPLUG: 'Available', | |
ERROR: 'Faulted' | |
}, | |
}, | |
Faulted: { | |
on: { | |
UNPLUG: 'Available' | |
} | |
} | |
} | |
}, { | |
guards: { | |
canAddCharge, | |
cantAddCharge | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment