Skip to content

Instantly share code, notes, and snippets.

@isaacplmann
Created July 30, 2019 02:09
Show Gist options
  • Save isaacplmann/396666f4a465ebe1f6f2ea464fb9e917 to your computer and use it in GitHub Desktop.
Save isaacplmann/396666f4a465ebe1f6f2ea464fb9e917 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const selectionMachine = Machine(
{
id: 'selection',
context: {
selectedIndex: undefined,
},
initial: 'unselected',
states: {
unselected: {
on: {
SELECT: 'selected',
}
},
selected: {
onEntry: [assign((ctx, event) => ({
selectedIndex: event.selectedIndex
})),
sendParent((ctx) => ({
type: 'SUBMIT',
selectedIndex: ctx.selectedIndex
}))
]
}
}
}
);
const matchingMachine = Machine(
{
id: 'matching',
context: {
choiceOneRef: null,
choiceTwoRef: null
},
initial: 'answering',
states: {
answering: {
entry: assign({
choiceOneRef: () => spawn(selectionMachine),
choiceTwoRef: () => spawn(selectionMachine)
}),
on: {
CHOOSE_ONE: {
actions: send('SELECT', {
to: ctx => ctx.choiceOneRef
})
},
CHOOSE_TWO: {
actions: send('SELECT', {
to: ctx => ctx.choiceTwoRef
})
},
SUBMIT: 'submitted',
}
},
submitted: {
on: {
CLEAR: 'answering'
}
}
}
},
{}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment