Xstate: Curious if this is possible

Created on 7 May 2019  路  5Comments  路  Source: davidkpiano/xstate

const rideRequestMachine = Machine ({
id: "Ride_Request",
strict: true,
initial: "noRide",
states: {
noRide: {
on: {
tripendpointsset: {
target: "#Ride_Request.rideLocationSet"
},
suggestedaddresschosen : {
target: "noRide"
},
clearaddresshistory: {
target: "noRide"
}
}
},
rideLocationSet: {
initial: "ridelocationset",
states: {
ridelocationset: {
on: {
vehicletypeset : {
target: "#Ride_Request.vehicleTypeSet"
},
tripendpointsset: {
target: "ridelocationset"
},
suggestedaddresschosen : {
target: "#Ride_Request.noRide"
},
clearaddresshistory: {
target: "#Ride_Request.noRide"
}
}
}
}
},
vehicleTypeSet : {
initial: "vehicletypeset",
states: {
vehicletypeset: {
on: {
creditcardpaymenttokenset: {
target: "#Ride_Request.creditcardPaymentTokenSet"
},
tripendpointsset: {
target: "#Ride_Request.noRide"
},
suggestedaddresschosen : {
target: "#Ride_Request.noRide"
},
clearaddresshistory: {
target: "#Ride_Request.noRide"
}
}
}

            }
        },      
        creditcardPaymentTokenSet: {
            initial: "creditcardpaymenttokenset",
            states: {
                creditcardpaymenttokenset : {
                    on: {
                        riderequested : {
                            target: "#Ride_Request.rideRequested"
                        }, 
                        tripendpointsset: {
                            target: "#Ride_Request.noRide"
                        },  
                        suggestedaddresschosen : {
                            target: "#Ride_Request.noRide"
                        }, 
                        clearaddresshistory: {
                            target: "#Ride_Request.noRide"
                        }
                    }
                }
            }
        },
        rideRequested: {
            initial: "riderequested",
            states: {
                riderequested: {
                    on: {
                        riderequestcancelled : {
                            target: "#Ride_Request.rideRequestIssue.riderequestcancelled"
                        }, 
                        preauthfailed: {
                            target: "#Ride_Request.rideRequestIssue.preauthfailed"
                        },  
                        customeralertednoridesavailable: {
                            target: "#Ride_Request.rideRequestIssue.customeralertednoridesavailable"
                        },
                        riderequested : {
                            target: "riderequested"
                        }
                    }
                }

            }
        },
        rideRequestIssue: {
            states: {
                riderequestcancelled : {
                   onEntry: "riderequestcancelled",
                },
                preauthfailed : {
                    onEntry: "preauthfailed"
                },
                customeralertednoridesavailable : {
                    onEntry: "customeralertednoridesavailable"
                }
            }

        }

    }

},
{
    actions: {
        riderequestcancelled : (ctx, event) => {
            console.log("rideRequestCancelled");
        },
        preauthfailed : (ctx, event) => {
            console.log("preauthfailed");
        },
        customeralertednoridesavailable : (ctx, event) => {
            console.log("customeralertednoridesavailable");
        }

    }
});

Hi there. I am using x-state to test a project of mine that has many different compound state transitions. Within each one of these states, there are multiple events that can be triggered each resulting in its own transition or action either within its own state or into another state. I am trying to set up a guard that checks that given the certain state if an event comes in that does not exist within that state an error is thrown. I would like to stress that setting strict: true is not the solution I am looking for. I need to check in individual states that only the specified events are the ones coming through.. if not I need to catch it. I have tried onEntry, and setting an action but since the event doesn't exist in that state it never makes it that far.

question

Most helpful comment

The code is unformatted and hard to read. I think it might be necessary. 馃槈

All 5 comments

Can you please put your code in a https://codesandbox.io so it's easier to read and reproduce?

I don't know if that's necessary

let us take a small piece of it

creditcardPaymentTokenSet: {
initial: "creditcardpaymenttokenset",
states: {
creditcardpaymenttokenset : {
on: {
riderequested : {
target: "#Ride_Request.rideRequested"
},
tripendpointsset: {
target: "#Ride_Request.noRide"
},
suggestedaddresschosen : {
target: "#Ride_Request.noRide"
},
clearaddresshistory: {
target: "#Ride_Request.noRide"
}
}
}
}
},

lets say my machine is currently at this state : creditcardPaymentTokenSet

now say I had an event come through this state that is present in the entire machine but is not present in this current state. is there a way to catcht that event

The code is unformatted and hard to read. I think it might be necessary. 馃槈

its pull from the above code. can look at the riderequested state

Yes, events bubble up to parent states and are "caught" by those states until a transition matches.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carlbarrdahl picture carlbarrdahl  路  3Comments

bradwoods picture bradwoods  路  3Comments

pke picture pke  路  3Comments

bradwoods picture bradwoods  路  3Comments

laurentpierson picture laurentpierson  路  3Comments