I am currently working on an application using xstate, I have a parent machine that spawns into two different children's machines, the children machines make a fetch to different API endpoint and they all send back to the parent a resolve or reject event depending on the status of the API call , I need help with how to make sure that all fetches are done before transitioning to the idle state on the parent machine.
fetchMachine:
const fetchMachine: FetchMachine =(
fetchFunction
) => (
{
id: 'fetch',
initial: States.Initialize,
context: {
response: null,
error: null
},
states: {
[States.Initialize]: {
on: {
'FETCH.REQUEST': {
target: States.Pending,
}
}
},
[States.Pending]: {
invoke: {
src: 'fetch',
onDone: {
target: States.Success,
actions: ['updateResponse']
},
onError: {
target: States.Failure,
actions: ['updateError']
}
},
},
[States.Success]: {
entry: ['fetchSuccess'],
on: {
'FETCH.REQUEST': States.Pending
}
},
[States.Failure]: {
entry: ['fetchFailure'],
on: {
'FETCH.REQUEST': States.Pending
}
}
}
}
The machine above sends the request of the event back to the parent.
The issue now is that the parent machines utilize this machine parallelly, I need help with how to make sure that all the fetches are done before transitioning to the idle state on the parent machine.