Skip to content

Instantly share code, notes, and snippets.

@bfillmer
Created May 27, 2020 16:20
Show Gist options
  • Save bfillmer/343d97ee81658d78a704e2b909a6de9d to your computer and use it in GitHub Desktop.
Save bfillmer/343d97ee81658d78a704e2b909a6de9d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const glanceMachine = Machine(
{
id: 'glanceMachine',
initial: 'idle',
context: {
expanded: 'open',
criteria: {},
stats: {},
},
states: {
idle: {
entry: ['clearStats'],
on: {
FETCH: {
target: 'pending',
cond: 'isExpanded',
},
OPEN: {
actions: ['setExpandedOpen'],
target: 'pending'
}
},
},
pending: {
entry: ['setCriteria'],
invoke: {
src: () => null,
onDone: {
target: 'success',
actions: assign({
stats: (_, event) => transformStats(event.data),
}),
},
onError: {
target: 'failure',
},
},
},
success: {
on: {
FETCH: {
target: 'pending',
cond: 'isExpanded',
},
OPEN: {
target: 'pending',
actions: ['setExpandedOpen'],
},
CLOSED: {
target: 'idle',
actions: ['setExpandedClosed'],
},
},
},
failure: {
on: {
FETCH: {
target: 'pending',
cond: 'isExpanded',
},
OPEN: {
target: 'pending',
actions: ['setExpandedOpen'],
},
CLOSED: {
target: 'idle',
actions: ['setExpandedClosed'],
},
},
},
},
},
{
actions: {
setCriteria: assign({
criteria: (context, event) => ({ ...context.criteria, ...event.criteria }),
}),
setExpandedOpen: assign({ expanded: 'open' }),
setExpandedClosed: assign({ expanded: 'closed' }),
clearStats: assign({ stats: {} }),
},
guards: {
isExpanded: context => context.expanded === 'open',
},
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment