Skip to content

Instantly share code, notes, and snippets.

@dannyrb
Created June 12, 2020 20:56
Show Gist options
  • Save dannyrb/452eaad79a778a1846fc4c1d68e83f0c to your computer and use it in GitHub Desktop.
Save dannyrb/452eaad79a778a1846fc4c1d68e83f0c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const machineConfiguration = {
id: 'measurementTracking',
initial: 'idle',
context: {
trackedStudy: '',
trackedSeries: [],
},
states: {
off: {
type: 'final',
},
idle: {
entry: 'clearContext',
on: {
TRACK_SERIES: 'promptBeginTracking',
},
},
promptBeginTracking: {
invoke: {
src: 'promptBeginTracking',
onDone: [
{
target: 'tracking',
actions: ['setTrackedStudyAndSeries'],
cond: 'promptAccepted',
},
{
target: 'off',
cond: 'promptDeclined',
},
{
target: 'idle',
},
],
onError: {
target: 'idle',
},
},
},
tracking: {
on: {
TRACK_SERIES: [
{
target: 'promptTrackNewStudy',
cond: 'isNewStudy',
},
{
target: 'promptTrackNewSeries',
cond: 'isNewSeries',
},
],
UNTRACK_SERIES: [
{
target: 'tracking',
actions: ['removeTrackedSeries'],
cond: 'hasRemainingTrackedSeries',
},
{
target: 'idle',
},
],
},
},
promptTrackNewStudy: {
invoke: {
src: 'promptTrackNewStudy',
onDone: [
{
target: 'tracking',
actions: ['setTrackedStudyAndSeries'],
cond: 'promptAccepted',
},
{
target: 'tracking',
},
],
onError: {
target: 'idle',
},
},
},
promptTrackNewSeries: {
invoke: {
src: 'promptTrackNewSeries',
onDone: [
{
target: 'tracking',
actions: ['addTrackedSeries'],
cond: 'promptAccepted',
},
{
target: 'tracking',
},
],
onError: {
target: 'idle',
},
},
},
},
strict: true,
};
const defaultOptions = {
services: {
promptBeginTracking: (ctx, evt) => {
// return { userResponse, StudyInstanceUID, SeriesInstanceUID }
},
promptTrackNewStudy: (ctx, evt) => {
// return { userResponse, StudyInstanceUID, SeriesInstanceUID }
},
promptTrackNewSeries: (ctx, evt) => {
// return { userResponse, StudyInstanceUID, SeriesInstanceUID }
},
},
actions: {
clearContext: assign({
trackedStudy: '',
trackedSeries: [],
}),
// Promise resolves w/ `evt.data.*`
setTrackedStudyAndSeries: assign((ctx, evt) => ({
trackedStudy: evt.data.StudyInstanceUID,
trackedSeries: [evt.data.SeriesInstanceUID],
})),
addTrackedSeries: assign((ctx, evt) => ({
trackedSeries: [...ctx.trackedSeries, evt.data.SeriesInstanceUID],
})),
removeTrackedSeries: assign((ctx, evt) => ({
trackedSeries: ctx.trackedSeries
.slice()
.filter(ser => ser !== evt.SeriesInstanceUID),
})),
},
guards: {
promptAccepted: (ctx, evt) => evt.data && evt.data.userResponse === 1,
promptCanceled: (ctx, evt) => evt.data && evt.data.userResponse === 0,
promptDeclined: (ctx, evt) => evt.data && evt.data.userResponse === -1,
// Has more than 1, or SeriesInstanceUID is not in list
// --> Post removal would have non-empty trackedSeries array
hasRemainingTrackedSeries: (ctx, evt) =>
ctx.trackedSeries.length > 1 ||
!ctx.trackedSeries.includes(evt.SeriesInstanceUID),
isNewStudy: (ctx, evt) => ctx.trackedStudy !== evt.StudyInstanceUID,
isNewSeries: (ctx, evt) =>
!ctx.trackedSeries.includes(evt.SeriesInstanceUID),
},
};
const measurementTrackingMachine = Machine( machineConfiguration,
defaultOptions
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment