-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: data not passed to parentFsm 'onDone' when using parallel FSM #335
Comments
I am facing the same problem: for parallel machines, the metadata of a final state is not available |
Do you plan on fixing this bug? We plan on using xstate for a Production application soon and this bug is preventing us from using parallel FSMs. |
@laurentpierson Yes, I'm prioritizing this. |
Since we don't want to make assumptions on what a "done" event would return for parallel states (what would this look like to you?), you can do this already by using import { Machine, interpret, assign } from "xstate";
const parallelDoneMachine = Machine({
id: "parallelDone",
type: "parallel",
context: {
allData: []
},
states: {
one: {
initial: "foo",
states: {
foo: {
on: { ONE: "bar" }
},
bar: {
type: "final",
data: "one-bar"
}
},
onDone: {
actions: assign({
allData: (ctx, e) => ctx.allData.concat(e.data)
})
}
},
two: {
initial: "foo",
states: {
foo: {
on: { TWO: "bar" }
},
bar: {
type: "final",
data: "two-bar"
}
},
onDone: {
actions: assign({
allData: (ctx, e) => ctx.allData.concat(e.data)
})
}
}
},
onDone: {
actions: ctx => {
console.log(ctx);
}
}
});
const parallelDoneService = interpret(parallelDoneMachine);
parallelDoneService.start();
parallelDoneService.send("ONE");
parallelDoneService.send("TWO");
// => ['one-bar', 'two-bar'] See this pattern in action here: https://codesandbox.io/s/p52nxqzw4m |
Bug or feature request?
Bug
Description:
As explained by @davidkpiano in #224, it is possible to pass data to a parent FSM by putting a
data
property in the final state of the child FSM. The content of thatdata
property can then be accessed in the parent FSM'sonDone
transition.This is working as expected for non-parallel FSMs but not with parallel FSMs where no data is received in parent FSM.
Link to reproduction or proof-of-concept:
Successful unit test with non-parallel FSM:
As you can see if you run the test, the event during condition check in parent FSM contains the data property set in child FSM:
event during condition check: {"type":"done.state.parentFsm.A","data":{"status":"success"}}
Now the failing unit test when using parallel FSMs:
The test will fail because the event doesn't contain the
data
property:event during condition check: {"type":"done.state.parentFsm.A"}
An additional question follows: how do you plan on providing data coming from a parallel FSM? Will
data
be an array in such case? Indeed, parallel FSMs (in this case,parallel1
andparallel2
) could return different data which might be interesting to the parent FSM.The text was updated successfully, but these errors were encountered: