Last active
July 13, 2021 04:26
-
-
Save sugasaki/a66c00840d4457c8c213f12d88f286f7 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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
const childMachine1 = Machine( | |
{ | |
id: "child1", | |
initial: "offline", | |
context: { | |
data: null, | |
error: null, | |
url: "https://api.ipify.org?format=json" | |
}, | |
states: { | |
offline: { | |
on: { | |
RUN: { target: "loading" } | |
} | |
}, | |
loading: { | |
invoke: { | |
src: "fetch", | |
onDone: { | |
target: "success", | |
actions: ["setData", "notifyData"] | |
}, | |
onError: { | |
target: "failure", | |
actions: ["setError", "notifyError"] | |
} | |
} | |
}, | |
success: { | |
entry: sendParent("COMPLETE", { | |
delay: 2000 | |
}) | |
}, | |
failure: { | |
type: "final" | |
} | |
} | |
}, | |
{ | |
actions: { | |
setData: assign({ data: (_, event) => event.data }), | |
notifyData: () => {}, | |
setError: assign({ error: (_, event) => event.data }), | |
notifyError: () => {} | |
}, | |
services: { | |
fetch: (context) => axios(context.url).then((response) => response.data) | |
} | |
} | |
); | |
const childMachine2 = Machine( | |
{ | |
id: "child2", | |
initial: "offline", | |
context: { | |
data: null, | |
error: null, | |
url: "https://api.ipify.org?format=json" | |
}, | |
states: { | |
offline: { | |
on: { | |
RUN: "loading" | |
} | |
}, | |
loading: { | |
invoke: { | |
src: "fetch", | |
onDone: { | |
target: "success", | |
actions: ["setData", "notifyData"] | |
}, | |
onError: { | |
target: "failure", | |
actions: ["setError", "notifyError"] | |
} | |
} | |
}, | |
success: { | |
entry: sendParent("COMPLETE", { | |
delay: 2000 | |
}) | |
}, | |
failure: { | |
type: "final" | |
} | |
} | |
}, | |
{ | |
actions: { | |
setData: assign({ data: (_, event) => event.data }), | |
notifyData: () => {}, | |
setError: assign({ error: (_, event) => event.data }), | |
notifyError: () => {} | |
}, | |
services: { | |
fetch: (context) => axios(context.url).then((response) => response.data) | |
} | |
} | |
); | |
const parentMachine = Machine( | |
{ | |
id: "parent-machine", | |
initial: "idle", | |
context: { | |
child1: null, | |
child2: null | |
}, | |
states: { | |
idle: { | |
entry: assign({ | |
child1: () => spawn(childMachine1), | |
child2: () => spawn(childMachine2) | |
}), | |
on: { | |
"running.start": { target: "child1" } | |
} | |
}, | |
child1: { | |
initial: "run", | |
states: { | |
run: { | |
// entry: send({ type: "RUN" }, { to: (context) => context.child1 }), | |
// entry: context.child1.send("RUN"), | |
entry: (context) => context.child1.send("RUN"), | |
on: { | |
COMPLETE: { target: "completed" } | |
} | |
}, | |
completed: { | |
after: { | |
2000: { target: "#parent-machine.child2" } | |
} | |
} | |
} | |
}, | |
child2: { | |
initial: "run", | |
states: { | |
run: { | |
entry: send({ type: "RUN" }, { to: (context) => context.child2 }), | |
on: { | |
COMPLETE: { target: "completed" } | |
} | |
}, | |
completed: { | |
after: { | |
2000: { target: "#parent-machine.completed" } | |
} | |
} | |
} | |
}, | |
completed: { | |
type: "final" | |
} | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment