Skip to content

Instantly share code, notes, and snippets.

@mapicard
Last active March 6, 2020 19:33
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 mapicard/0a02e3d39f7f364cf697a08140089ce8 to your computer and use it in GitHub Desktop.
Save mapicard/0a02e3d39f7f364cf697a08140089ce8 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 getInfoStates = {
initial: 'getCaseName',
states: {
getCaseName: {
entry: ['aReqCaseName'],
on: {
tInputReady: {
//target: 'OpenedIO',
actions: 'aReadToken'
},
tRcvdCaseName: {
actions: ['aSaveCaseName'],
target: 'getCaseSize',
},
},
},
getCaseSize: {
entry: ['aReqCaseSize'],
on: {
tInputReady: {
//target: 'OpenedIO',
actions: 'aReadToken'
},
tRcvdCaseSize: {
actions: ['aSaveCaseSize'],
target: 'end',
},
}
},
end: {
entry: 'Push_tGotInfo',
type: 'final',
}
}
};
const getTracksStates = {
initial: 'getCaseName',
states: {
getCaseName: {
entry: ['aReqCaseName'],
on: {
tInputReady: {
//target: 'OpenedIO',
actions: 'aReadToken'
},
tRcvdCaseName: {
actions: ['aSaveCaseName'],
target: 'getCaseSize',
},
},
},
getCaseSize: {
entry: ['aReqCaseSize'],
on: {
tInputReady: {
//target: 'OpenedIO',
actions: 'aReadToken'
},
tRcvdCaseSize: {
actions: ['aSaveCaseSize'],
target: 'end',
},
}
},
end: {
entry: 'Push_tGotInfo',
type: 'final',
}
}
};
const goalMachine = Machine({
id: 'goal',
initial: 'disconnected',
context: {
retries: 0
},
states: {
disconnected: {
on: {
tGetSrvId: {
target: 'Open',
actions: ['aGetSrvId'],
},
tGetInfo: {
target: 'Open',
actions: 'qGetInfo'
},
tReqTrack: {
target: 'Open',
actions: 'aReqTrack'
},
tGetTracks: {
target: 'Open',
actions: 'qGetTracks'
},
tRetry: {
target: 'Open',
actions: 'aRetry'
},
tTimeout: {
target: 'End',
actions: 'aEndComm'
},
},
},
Open: {
entry: ['aOpen'],
on: {
tOpened: {
target: 'Opened',
actions: 'aOpened'
},
tRetry: {
actions: 'aRetryStartComm'
},
tNetError: {
target: 'End',
actions: 'aStartCommError'
},
tTimeout: {
target: 'End',
actions: 'aStartCommTmout'
},
},
},
Opened: {
on: {
tGetTermId: {
target: 'TermId',
actions: 'aSendTermId'
},
tAutoAccept: {
//target: 'OpenedIO',
},
tServerId: {
//target: 'OpenedIO',
actions: 'aSaveSrvId'
},
tHostVersion: {
//target: 'OpenedIO',
actions: 'aSaveSrvVer'
},
tInputReady: {
//target: 'OpenedIO',
actions: 'aReadToken'
},
tOutputDone: {
//target: 'CommStarted',
actions: 'aOutReady'
},
tNetError: {
target: 'End',
actions: 'aError'
},
}
},
TermId: {
//entry: ['aSendTermId'],
on: {
tGetClientVer: {
target: 'ClientVer',
actions: ['aSendClientVersion'],
},
tAutoAccept: {
//target: 'OpenedIO',
},
tServerId: {
//target: 'OpenedIO',
actions: 'aSaveSrvId'
},
tHostVersion: {
//target: 'OpenedIO',
actions: 'aSaveSrvVer'
},
tInputReady: {
//target: 'OpenedIO',
actions: 'aReadToken'
},
tOutputDone: {
//target: 'CommStarted',
actions: 'aOutReady'
},
tNetError: {
target: 'End',
actions: 'aError'
},
}
},
ClientVer: {
on: {
tOutputDone: {
target: 'Connected',
actions: 'aOutReady'
},
tInputReady: {
//target: 'OpenedIO',
actions: 'aReadToken'
},
tNetError: {
target: 'End',
actions: 'aError'
},
}
},
Connected: {
entry: ['aPopGoal'],
on: {
tGetSrvId: {
//target: 'Open',
actions: ['aGetSrvId'],
},
tGetInfo: 'GetInfo',
tGetTracks: 'GetTracks',
tReqTrack: {
target: 'Open',
actions: 'aReqTrack'
},
}
},
GetInfo: {
on: {
tGotInfo: {
target: 'Connected',
},
tNetError: {
target: 'End',
actions: 'aError'
},
},
...getInfoStates
},
GetTracks: {
on: {
tGotTracks: {
target: 'Connected',
},
tNetError: {
target: 'End',
actions: 'aError'
},
},
...getTracksStates
},
End: {
type: 'final'
},
getSrvId: {
//...connectionStates,
},
start: {
on: {
FETCH: 'loading'
}
},
loading: {
on: {
RESOLVE: 'success',
REJECT: 'failure'
}
},
success: {
type: 'final'
},
failure: {
on: {
RETRY: {
target: 'loading',
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
}
}
},
{
actions: {
// action implementations
commStart: (context, event) => {
console.log('commStarting...');
socket.open("1.2.3.4", 1234,
()=> { //success callback
},
(err)=> { //error callback
}
);
},
activate: (context, event) => {
console.log('activating...');
},
notifyActive: (context, event) => {
console.log('active!');
},
notifyInactive: (context, event) => {
console.log('inactive!');
},
sendTelemetry: (context, event) => {
console.log('time:', Date.now());
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment