Code review for xstate code for an upcoming YouTube video #1375
-
I'm doing a multi-part YouTube series on building a JackBox TV style game using xstate and GraphQL subscriptions. It all works, but I don't feel like the xstate machine is what it should be. In particular I don't like this code: https://github.com/jherr/firsties/blob/master/server/game.ts#L42-L48 I just need to set a response and then go on to the next state if all of the responses are filled in. Seems simple enough, but I've tried a few different techniques and nothing has worked. What I have now is the cleanest I've been able to get to. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
For reference, here's the code: updateResponse: {
actions: ["updateResponse", send("checkResponses")],
},
checkResponses: {
target: "VOTE",
cond: "everyoneResponded",
}, You can also do this: updateResponse: [
{ target: 'VOTE', cond: 'everyoneResponded', actions: 'updateResponse' },
{ actions: 'updateResponse' }
] Which reads like:
You might feel uneasy about the repetition in the
You can always create a helper function to remove the repetition: function alwaysDoActions(transitions, actions) {
return transitions.map(transition => ({
...transition,
actions
}).concat({ actions });
}
// ...
updateResponse: alwaysDoActions(
[{ target: 'VOTE', cond: 'everyoneResponded' }],
'updateResponse'),
// ... IMO I'd rather just have the repetition instead of adding another abstraction. |
Beta Was this translation helpful? Give feedback.
For reference, here's the code:
You can also do this:
Which reads like:
You might feel uneasy about the repetition in the
'updateResponse'
action, but it's more clear/explicit than:You can always create a helper function to remove the repetition:
function