Last active
March 5, 2021 16:29
-
-
Save James-Byrne/4195d567b3f2199ef59640b61f3bae1c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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