Skip to content

Instantly share code, notes, and snippets.

@NetCZ
Last active April 7, 2021 12:52
Show Gist options
  • Save NetCZ/1e5483f1c676bbe49902c32a97633965 to your computer and use it in GitHub Desktop.
Save NetCZ/1e5483f1c676bbe49902c32a97633965 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: "PaymentForm",
initial: "idle",
states: {
idle: {
on: {
SUBMIT: "loading",
},
},
loading: {
invoke: {
src: "doPayment",
onDone: {
target: "success",
},
onError: {
target: "error",
},
},
on: {
PAYMENT_RECEIVED: "success",
PAYMENT_FAILED: "error",
},
},
error: {
on: {
SUBMIT: "loading",
},
},
success: {
type: "final",
},
},
strict: true,
}, {
services: {
async doPayment() {
if (Math.random() < 0.5) {
throw new Error("Payment failed");
}
return true;
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment