Skip to content

Instantly share code, notes, and snippets.

@James-Byrne
Last active March 5, 2021 16:29
Show Gist options
  • Save James-Byrne/4195d567b3f2199ef59640b61f3bae1c to your computer and use it in GitHub Desktop.
Save James-Byrne/4195d567b3f2199ef59640b61f3bae1c 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 collectRecordsMachine = Machine({
id: 'child',
initial: 'working',
context: {
records: []
},
on: {
ADD_RECORD: [
{
cond: c => c.records.length < 3,
actions: [
assign({
records: (c, e) => [...c.records, e.record]
}),
]
},
{
target: 'done'
}
],
},
states: {
working: {},
done: {
type: 'final',
data: {
records: c => c.records
}
}
}
});
const parentMachine = Machine({
id: 'parent',
initial: 'idle',
states: {
idle: {
on: {
START: 'collectRecords'
}
},
collectRecords: {
invoke: {
id: 'child',
src: collectRecordsMachine,
onDone: 'success',
onError: 'idle'
},
on: {
ADD_RECORD: {
actions: send(
{
type: 'ADD_RECORD',
record: Math.random()
},
{
to: 'child'
}
)
}
}
},
success: {
entry: (c, e) => console.log('collected records', e.data.records)
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment