Created
March 25, 2021 06:25
-
-
Save Bogidon/e8365acaea78ea78a9e0dffcca9cc2e3 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
/************* | |
Fixed | |
*************/ | |
const STATES = Object.fromEntries(["animalKingdom", "dogOrCat", "petAge", "enrolledInPuppySchool", "recommendedProduct"].map(x => [x, x])) | |
const GUARDS = Object.fromEntries(["isMammal", "isFish", "isDog", "isCat", "petNameValid"].map(x => [x, x])) | |
const TRANSITIONS = Object.fromEntries(["continue", "error"].map(x => [x, x])) | |
/************* | |
Flexible | |
*************/ | |
// SERIALIZED | |
// { | |
// // Question 1: mammal or other? | |
// "animalKingdom": { | |
// "on": { | |
// "continue": [ | |
// // If mammal go to dogOrCat | |
// { | |
// target: "dogOrCat", | |
// cond: { | |
// type: "query", | |
// query: [ | |
// { field: "animalKingdon", type: "eq", value: "mammal" } | |
// ] | |
// } | |
// }, | |
// // Else go to recommendedProduct | |
// { | |
// target: "recommendedProduct", | |
// } | |
// ], | |
// }, | |
// }, | |
// // Question 2: dog or cat? | |
// "dogOrCat": { | |
// "on": { | |
// "continue": "petAge" | |
// }, | |
// }, | |
// // Question 3: pet age (we only ask for mammals) | |
// "petAge": { | |
// "on": { | |
// "continue": [ | |
// { | |
// // If it's a young dog go to enrolledInPuppySchool | |
// target: "enrolledInPuppySchool", | |
// cond: { | |
// type: "query", | |
// query: [ | |
// { field: "dogOrCat", type: "eq", value: "dog" }, | |
// { field: "petAge", type: "lt", value: 4 }, | |
// ] | |
// } | |
// }, | |
// // Else go to recommendedProduct | |
// { | |
// target: "recommendedProduct" | |
// } | |
// ] | |
// }, | |
// }, | |
// // Question 4: enrolled in puppy school? (we only ask for dogs who are young) | |
// "enrolledInPuppySchool": { | |
// "on": { | |
// "continue": "recommendedProduct" | |
// } | |
// }, | |
// // Question 5: | |
// "recommendedProduct": { | |
// type: "final" | |
// } | |
// } | |
const states = { | |
// Question 1: mammal or other? | |
[STATES.animalKingdom]: { | |
on: { | |
[TRANSITIONS.continue]: [ | |
// If mammal go to dogOrCat | |
{ | |
target: STATES.dogOrCat, | |
cond: { | |
type: "query", | |
query: [ | |
{ field: "animalKingdon", type: "eq", value: "mammal" } | |
] | |
} | |
}, | |
// Else go to recommendedProduct | |
{ | |
target: STATES.recommendedProduct, | |
} | |
], | |
}, | |
}, | |
// Question 2: dog or cat? | |
[STATES.dogOrCat]: { | |
on: { | |
[TRANSITIONS.continue]: STATES.petAge | |
}, | |
}, | |
// Question 3: pet age (we only ask for mammals) | |
[STATES.petAge]: { | |
on: { | |
[TRANSITIONS.continue]: [ | |
{ | |
// If it's a young dog go to enrolledInPuppySchool | |
target: STATES.enrolledInPuppySchool, | |
cond: { | |
type: "query", | |
query: [ | |
{ field: "dogOrCat", type: "eq", value: "dog" }, | |
{ field: "petAge", type: "lt", value: 4 }, | |
] | |
} | |
}, | |
// Else go to recommendedProduct | |
{ | |
target: STATES.recommendedProduct | |
} | |
] | |
}, | |
}, | |
// Question 4: enrolled in puppy school? (we only ask for dogs who are young) | |
[STATES.enrolledInPuppySchool]: { | |
on: { | |
[TRANSITIONS.continue]: STATES.recommendedProduct | |
} | |
}, | |
// Question 5: | |
[STATES.recommendedProduct]: { | |
type: "final" | |
} | |
} | |
Machine({ | |
id: 'petFlow', | |
initial: STATES.animalKingdom, | |
states | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment