Skip to content

Instantly share code, notes, and snippets.

@Zlass
Last active June 19, 2020 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zlass/9f5f84e94007a0abbc31963c048d98ea to your computer and use it in GitHub Desktop.
Save Zlass/9f5f84e94007a0abbc31963c048d98ea to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const ProgramChangeEvents = {
UserChanged: "USER_CHANGED",
DeviceChanged: "DEVICE_CHANGED",
Error: "UNKNOWN_MESSAGE",
}
const {log} = actions;
const initialDeviceState = "All-Around";
const initialAppState = "All-Around";
const ProgramSyncMachine = Machine({
id: "ProgramSyncMachine",
initial: "idle",
context: {
device: initialDeviceState,
app: initialAppState,
},
states: {
idle: {
on: {
[ProgramChangeEvents.UserChanged]: 'pending',
[ProgramChangeEvents.DeviceChanged]: {
target: "idle",
actions: ["assignDevice", "notifyDeviceChanged"],
}
},
},
pending: {
entry: ['assignApp', "updateDevice"],
on: {
[ProgramChangeEvents.UserChanged]: {
target: 'pending', // external will re-trigger entry
},
[ProgramChangeEvents.DeviceChanged]: {
target: 'committed',
actions: 'assignDevice'
},
[ProgramChangeEvents.Error]: 'failed',
}
},
committed: {
on: {
"": [{
target: 'idle',
cond: "stateSynced",
}, {
target: 'pending',
cond: "stateMismatch",
}]
}
},
failed: {
on: {
[ProgramChangeEvents.UserChanged]: 'pending',
[ProgramChangeEvents.DeviceChanged]: {
target: "idle",
actions: ["notifyDeviceChanged", "assignDevice"],
}
}
},
},
}, {
guards: {
stateMismatch: (context) => {
return context.app !== context.device
},
stateSynced: (context => {
return context.app === context.device
}),
}, actions: {
assignDevice: assign((context, event) => {
console.log("assignDevice", event);
if (event.type === ProgramChangeEvents.DeviceChanged) {
return ({device: event.program});
}
return context;
}),
assignApp: assign((context, event) => {
console.log("assignApp", event);
if (event.type === ProgramChangeEvents.UserChanged) {
return ({app: event.program});
}
return context;
}),
updateDevice: (context, event) => {
// TODO use hearing aid to set program
console.log("updateDevice", event);
if (context.device !== context.app) {
console.log(`Set device program: ${context.app}`);
// HearingAidBridge.setProgram(context.app);
new LivelyBridgeManager().setProgram(context.app)
}
},
notifyDeviceChanged: log((context, event) => {
// TODO use event.program to notify the device is out of sync with app
if (event.type === ProgramChangeEvents.DeviceChanged) {
return `Device changed: ${context.device} -> ${event.program}`;
}
return "No change"
}),
},
});
const ProgramSwitchEvents ={
SwitchProgram : "SWITCH_PROGRAM",
ApplyProgram : "APPLY_PROGRAM",
DeviceChanged : "DEVICE_CHANGED_PROGRAM",
Error : "UNKNOWN_MESSAGE",
}
const State ={
Active : "Active",
Selected : "Selected",
Selecting : "Selecting",
WaitingForDevice : "WaitingForDevice",
DeviceChanged : "DeviceChanged",
DeviceDisconnected : "DeviceDisconnected",
FailedToApply : "FailedToApply",
}
const Actions ={
ApplySelectedProgram : "ApplySelectedProgram",
UpdateActiveProgram : "UpdateActiveProgram",
}
const Guard = {
NotAppliedYet : "NotAppliedYet",
InSync : "InSync",
OutOfSync : "OutOfSync",
}
const ProgramSwitchingMachine = Machine({
id: "ProgramSwitchMachine",
initial: "idle",
context: {
device: initialDeviceState,
app: initialAppState,
},
invoke: {
id: "syncProgram",
src: () => ProgramSyncMachine,
},
states: {
idle: {},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment