Created
July 16, 2020 16:06
-
-
Save Zlass/253dac69825c6bd60e49156bbb8b7b64 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
// Internal Actions | |
var HearingAidActions; | |
(function (HearingAidActions) { | |
HearingAidActions["UpdateConnectionStatus"] = "UPDATE_HEARING_AID_CONNECTION"; | |
HearingAidActions["UpdateBestSide"] = "UPDATE_BEST_SIDE"; | |
HearingAidActions["UpdateInstrumentInfo"] = "update_instrument_info"; | |
HearingAidActions["UpdateInstrumentSettings"] = "update_instrument_settings"; | |
HearingAidActions["UpdateProgramList"] = "UPDATE_PROGRAM_LIST"; | |
HearingAidActions["UpdateProgram"] = "CURRENT_PROGRAM"; | |
HearingAidActions["InitializeHearingAidManager"] = "INITIALIZE_HEARING_AID_MANAGER"; | |
HearingAidActions["StartHearingAidListener"] = "START_HEARING_AID_LISTENERS"; | |
HearingAidActions["ChangeCurrentProgram"] = "CHANGE_PROGRAM"; | |
HearingAidActions["NotifyReady"] = "HEARING_AIDS_READY"; | |
})(HearingAidActions || (HearingAidActions = {})); | |
var HearingAidGuards; | |
(function (HearingAidGuards) { | |
HearingAidGuards["HearingAidsReady"] = "HA_READY"; | |
})(HearingAidGuards || (HearingAidGuards = {})); | |
var HearingAidStates; | |
(function (HearingAidStates) { | |
HearingAidStates["Initialize"] = "init"; | |
HearingAidStates["Connecting"] = "connecting"; | |
HearingAidStates["Connected"] = "connected"; | |
HearingAidStates["Disconnected"] = "disconnected"; | |
})(HearingAidStates || (HearingAidStates = {})); | |
var HearingAidEventTypes; | |
(function (HearingAidEventTypes) { | |
HearingAidEventTypes["CheckReady"] = "CHECK_HEARING_AIDS_READY"; | |
HearingAidEventTypes["Ready"] = "HEARING_AIDS_READY"; | |
HearingAidEventTypes["SetProgram"] = "SET_PROGRAM"; | |
HearingAidEventTypes["CurrentProgramChanged"] = "CURRENT_PROGRAM_CHANGED"; | |
HearingAidEventTypes["ProgramListChanged"] = "PROGRAM_LIST_UPDATE"; | |
HearingAidEventTypes["InstrumentUpdate"] = "INSTRUMENT_UPDATE"; | |
HearingAidEventTypes["ConnectionUpdate"] = "CONNECTION_UPDATE"; | |
HearingAidEventTypes["Connect"] = "CONNECT"; | |
HearingAidEventTypes["Disconnect"] = "DISCONNECT"; | |
})(HearingAidEventTypes || (HearingAidEventTypes = {})); | |
var HearingAidSDKEventTypes; | |
(function (HearingAidSDKEventTypes) { | |
HearingAidSDKEventTypes["UpdateInstrumentInfo"] = "UPDATE_INSTRUMENT_INFORMATION"; | |
HearingAidSDKEventTypes["UpdateInstrumentSettings"] = "UPDATE_INSTRUMENT_SETTINGS"; | |
HearingAidSDKEventTypes["UpdateInformation"] = "UPDATE_SHARED_INFORMATION"; | |
})(HearingAidSDKEventTypes || (HearingAidSDKEventTypes = {})); | |
var ProgramSwitcherEventTypes; | |
(function (ProgramSwitcherEventTypes) { | |
ProgramSwitcherEventTypes["CarouselMount"] = "CAROUSEL_MOUNT"; | |
ProgramSwitcherEventTypes["CarouselNext"] = "CAROUSEL_NEXT"; | |
ProgramSwitcherEventTypes["CarouselPrevious"] = "CAROUSEL_PREVIOUS"; | |
ProgramSwitcherEventTypes["CarouselSnap"] = "CAROUSEL_SNAP"; | |
ProgramSwitcherEventTypes["Error"] = "UNKNOWN_MESSAGE"; | |
})(ProgramSwitcherEventTypes || (ProgramSwitcherEventTypes = {})); | |
var ProgramSwitcherState; | |
(function (ProgramSwitcherState) { | |
ProgramSwitcherState["Mounting"] = "Mounting"; | |
ProgramSwitcherState["Idle"] = "Idle"; | |
ProgramSwitcherState["Selecting"] = "Selecting"; | |
ProgramSwitcherState["Error"] = "Error"; | |
})(ProgramSwitcherState || (ProgramSwitcherState = {})); | |
var Actions; | |
(function (Actions) { | |
Actions["SelectProgram"] = "SELECT_PROGRAM"; | |
Actions["SetHearingAidProgram"] = "APPLY_SELECTED_PROGRAM"; | |
Actions["SnapToDeviceProgram"] = "SNAP_TO_DEVICE_PROGRAM"; | |
Actions["SnapToProgram"] = "SNAP_TO_PROGRAM"; | |
Actions["SnapToNext"] = "NEXT"; | |
Actions["SnapToPrevious"] = "PREVIOUS"; | |
Actions["UpdateActiveProgram"] = "UPDATE_ACTIVE_PROGRAM"; | |
Actions["UpdateProgramList"] = "UPDATE_PROGRAM_LIST"; | |
})(Actions || (Actions = {})); | |
var Guard; | |
(function (Guard) { | |
Guard["IsApplied"] = "IS_APPLIED"; | |
Guard["CarouselOutOfSync"] = "CAROUSEL_OUT_OF_SYNC"; | |
Guard["CarouselCanGoBack"] = "CAROUSEL_CAN_GO_BACK"; | |
Guard["CarouselCanGoForward"] = "CAROUSEL_CAN_GO_FORWARD"; | |
})(Guard || (Guard = {})); | |
const DEFAULT_PROGRAM_KEY = 0; | |
const SWITCHING_TIMEOUT = 750; | |
const ERROR_TIMEOUT = 5000; | |
function hearingAidIndexToCarouselSlideIndex(programList, hearingAidProgramKey) { | |
return programList.findIndex(({ key }) => key === hearingAidProgramKey); | |
} | |
const ProgramCarouselMachine = Machine({ | |
id: 'ProgramCarouselMachine', | |
initial: ProgramSwitcherState.Mounting, | |
context: { | |
programList: [], | |
// !!! Note these indexes match the index on the Hearing Aids !!! | |
carouselProgram: DEFAULT_PROGRAM_KEY, | |
deviceProgram: DEFAULT_PROGRAM_KEY, | |
}, | |
on: { | |
[HearingAidEventTypes.ProgramListChanged]: [ | |
{ | |
actions: [Actions.UpdateProgramList], | |
}, | |
{ | |
cond: Guard.CarouselOutOfSync, | |
actions: [Actions.SnapToDeviceProgram], | |
}, | |
], | |
[HearingAidEventTypes.CurrentProgramChanged]: { | |
target: ProgramSwitcherState.Idle, | |
actions: [Actions.UpdateActiveProgram, Actions.SnapToProgram], | |
}, | |
}, | |
states: { | |
[ProgramSwitcherState.Mounting]: { | |
on: { | |
[HearingAidEventTypes.CurrentProgramChanged]: { | |
target: ProgramSwitcherState.Mounting, | |
}, | |
[ProgramSwitcherEventTypes.CarouselMount]: { | |
target: ProgramSwitcherState.Idle, | |
actions: [ | |
assign({ carousel: (_, event) => event.carousel }), | |
assign({ carouselProgram: (context) => context.deviceProgram }), | |
Actions.SnapToDeviceProgram, | |
], | |
}, | |
}, | |
}, | |
[ProgramSwitcherState.Idle]: { | |
on: { | |
[ProgramSwitcherEventTypes.CarouselSnap]: { | |
target: ProgramSwitcherState.Selecting, | |
actions: Actions.SelectProgram, | |
}, | |
[ProgramSwitcherEventTypes.CarouselNext]: { | |
target: ProgramSwitcherState.Selecting, | |
cond: Guard.CarouselCanGoForward, | |
actions: Actions.SnapToNext, | |
}, | |
[ProgramSwitcherEventTypes.CarouselPrevious]: { | |
target: ProgramSwitcherState.Selecting, | |
cond: Guard.CarouselCanGoBack, | |
actions: Actions.SnapToPrevious, | |
}, | |
[HearingAidEventTypes.SetProgram]: [ | |
{ actions: [Actions.SnapToProgram] }, | |
{ | |
cond: (context, event) => context.carouselProgram !== event.programKey, | |
actions: [Actions.SnapToDeviceProgram], | |
}, | |
], | |
}, | |
}, | |
[ProgramSwitcherState.Selecting]: { | |
on: { | |
[ProgramSwitcherEventTypes.CarouselSnap]: { | |
target: ProgramSwitcherState.Selecting, | |
actions: Actions.SelectProgram, | |
internal: false, | |
}, | |
[ProgramSwitcherEventTypes.CarouselNext]: { | |
target: ProgramSwitcherState.Selecting, | |
cond: Guard.CarouselCanGoForward, | |
internal: false, | |
actions: Actions.SnapToNext, | |
}, | |
[ProgramSwitcherEventTypes.CarouselPrevious]: { | |
target: ProgramSwitcherState.Selecting, | |
cond: Guard.CarouselCanGoBack, | |
internal: false, | |
actions: Actions.SnapToPrevious, | |
}, | |
[HearingAidEventTypes.SetProgram]: { | |
target: ProgramSwitcherState.Idle, | |
cond: (context, event) => context.carouselProgram !== event.programKey, | |
actions: [Actions.SnapToProgram], | |
}, | |
[ProgramSwitcherEventTypes.Error]: ProgramSwitcherState.Error, | |
}, | |
after: { | |
[SWITCHING_TIMEOUT]: { | |
cond: Guard.CarouselOutOfSync, | |
actions: Actions.SetHearingAidProgram, | |
}, | |
[ERROR_TIMEOUT]: { | |
cond: Guard.CarouselOutOfSync, | |
target: ProgramSwitcherState.Error, | |
}, | |
}, | |
}, | |
[ProgramSwitcherState.Error]: { | |
on: { | |
[ProgramSwitcherEventTypes.CarouselSnap]: { | |
target: ProgramSwitcherState.Selecting, | |
actions: Actions.SelectProgram, | |
}, | |
[ProgramSwitcherEventTypes.CarouselNext]: { | |
target: ProgramSwitcherState.Selecting, | |
cond: Guard.CarouselCanGoForward, | |
actions: Actions.SnapToNext, | |
}, | |
[ProgramSwitcherEventTypes.CarouselPrevious]: { | |
target: ProgramSwitcherState.Selecting, | |
cond: Guard.CarouselCanGoBack, | |
actions: Actions.SnapToPrevious, | |
}, | |
[HearingAidEventTypes.SetProgram]: { | |
actions: Actions.SnapToProgram, | |
}, | |
}, | |
}, | |
}, | |
}, { | |
guards: { | |
[Guard.IsApplied]: (context) => { | |
return context.deviceProgram === context.carouselProgram; | |
}, | |
[Guard.CarouselOutOfSync]: (context) => { | |
return context.deviceProgram !== context.carouselProgram; | |
}, | |
[Guard.CarouselCanGoBack]: (context) => context.carouselProgram > 0, | |
[Guard.CarouselCanGoForward]: (context) => context.carouselProgram < context.programList.length, | |
}, | |
actions: { | |
[Actions.SelectProgram]: assign((context, event) => { | |
if (event.type === ProgramSwitcherEventTypes.CarouselSnap) { | |
return { carouselProgram: context.programList[event.slideIndex].key }; | |
} | |
return context; | |
}), | |
[Actions.SetHearingAidProgram]: sendParent((context) => ({ | |
type: HearingAidEventTypes.SetProgram, | |
programKey: context.carouselProgram, | |
})), | |
[Actions.UpdateActiveProgram]: assign((context, event) => { | |
if (event.type === HearingAidEventTypes.CurrentProgramChanged && context.deviceProgram !== event.programKey) { | |
return { deviceProgram: event.programKey }; | |
} | |
return context; | |
}), | |
[Actions.UpdateProgramList]: assign((context, event) => { | |
if (event.type === HearingAidEventTypes.ProgramListChanged) { | |
return { | |
programList: event.programs.filter((program) => program.available), | |
}; | |
} | |
return context; | |
}), | |
[Actions.SnapToNext]: (context) => { | |
if (context.carousel) { | |
context.carousel.snapToNext(true); | |
const currentProgramIndex = context.programList.findIndex(({ key }) => key === context.carouselProgram); | |
const nextProgram = context.programList[currentProgramIndex + 1].key; | |
return assign({ carouselProgram: nextProgram }); | |
} | |
}, | |
[Actions.SnapToPrevious]: (context) => { | |
if (context.carousel) { | |
context.carousel.snapToPrev(true); | |
const currentProgramIndex = context.programList.findIndex(({ key }) => key === context.carouselProgram); | |
const prevProgram = context.programList[currentProgramIndex - 1].key; | |
return assign({ carouselProgram: prevProgram }); | |
} | |
}, | |
[Actions.SnapToDeviceProgram]: (context) => { | |
if (context.carousel) { | |
context.carousel.snapToItem(hearingAidIndexToCarouselSlideIndex(context.programList, context.deviceProgram), false, false); | |
return assign({ carouselIndex: context.deviceProgram }); | |
} | |
}, | |
[Actions.SnapToProgram]: (context, event) => { | |
if (context.carousel && | |
(event.type === HearingAidEventTypes.SetProgram || event.type === HearingAidEventTypes.CurrentProgramChanged)) { | |
if (context.carouselProgram === event.programKey) { | |
return; | |
} | |
context.carousel.snapToItem(hearingAidIndexToCarouselSlideIndex(context.programList, event.programKey), false, false); | |
return assign({ carouselProgram: event.programKey }); | |
} | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment