Skip to content

Instantly share code, notes, and snippets.

@dannyrb
Created May 26, 2020 20:45
Show Gist options
  • Save dannyrb/eee290479f92e0c7e23567ea495e0549 to your computer and use it in GitHub Desktop.
Save dannyrb/eee290479f92e0c7e23567ea495e0549 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// {
// "type": "TRACK_SERIES",
// "StudyInstanceUID": "stu",
// "SeriesInstanceUID": "ser"
// }
const config = {
id: 'measurementTracking',
initial: 'notTracking',
context: {
prevTrackedStudy: '',
prevTrackedSeries: [],
trackedStudy: '',
trackedSeries: [],
},
states: {
notTracking: {
on: {
TRACK_SERIES: {
target: 'userPromptShouldTrack',
cond: 'eventHasStudyAndSeriesUIDs',
actions: ['trackSeries'],
},
}
},
userPromptShouldTrack: {
on: {
YES: 'tracking',
CANCEL: 'notTracking',
NEVER: 'neverTracking',
},
},
neverTracking: {
type: 'final',
},
tracking: {
on: {
'': [
{ target: 'tracking', cond: 'hasNewSeriesForTrackedStudy', actions: 'trackSeries' },
{ target: 'userPromptTrackingConflict', cond: 'hasConflictingStudyInstanceUID', actions: ['setPrevTracked', 'trackSeries'] },
],
// target: 'tracking',
// cond: 'trackNewSeriesInTrackedStudy',
// actions: ['logStuff', 'trackSeries'],
UNTRACK_SERIES: 'tracking',
},
},
userPromptTrackingConflict: {
on: {
TRACK_NEW_STUDY: 'tracking',
},
},
},
strict: true,
};
// Actions:
// - entry
// - exit
// - transition (on)
// Activities:
// - can return a "clean-up" function to call on exit
// actions, guards, services, activities, delays
const options = {
actions: {
logStuff: (context, event) => { console.log(context, event ) },
setPrevTracked: assign(context => ({
prevTrackedStudy: context.trackedStudy,
prevTrackedSeries: context.prevTrackedSeries.slice(),
})),
trackSeries: assign((context, event) =>
{
console.log(context, event);
const prevTrackedSeries = context.trackedSeries.slice();
return {
trackedStudy: event.StudyInstanceUID || '',
trackedSeries: [...prevTrackedSeries, event.SeriesInstanceUID],
}
}),
},
guards: {
eventHasStudyAndSeriesUIDs: (context, event) =>
event.StudyInstanceUID &&
event.SeriesInstanceUID,
hasConflictingStudyInstanceUID: (context, event) =>
event.StudyInstanceUID &&
event.SeriesInstanceUID &&
context.StudyInstanceUID !== context.trackedStudy,
hasNewSeriesForTrackedStudy: (context, event) =>
event.StudyInstanceUID &&
event.SeriesInstanceUID &&
event.StudyInstanceUID === context.trackedStudy &&
!context.trackedSeries.includes(event.SeriesInstanceUID),
/**
* Combine multiple guard conditions
*/
every: (ctx, event, { guard }) => {
const { guards } = guard;
return guards.every(guardKey => guards[guardKey](ctx, event));
}
},
}
const measurementTrackingMachine = Machine(config, options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment