Skip to content

Instantly share code, notes, and snippets.

@mikaelkaron
Last active August 12, 2019 11:04
Show Gist options
  • Save mikaelkaron/6f8dd23a3cb383958609af88d4c03303 to your computer and use it in GitHub Desktop.
Save mikaelkaron/6f8dd23a3cb383958609af88d4c03303 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 config = {
id: "form",
initial: "idle",
context: {},
states: {
idle: {
initial: "unknown",
states: {
unknown: {
on: {
"": [{ target: "valid", cond: "valid" }, { target: "invalid" }]
}
},
valid: { on: { SUBMIT: "#form.pending" } },
invalid: {}
},
on: { INPUT: { target: "idle.unknown", actions: "input" } }
},
pending: {
entry: "pending",
invoke: {
src: "submit",
onDone: "success",
onError: { target: "failure", actions: "error" }
},
after: { TIMEOUT: { target: "failure", actions: "timeout" } }
},
success: { type: "final" },
failure: { on: { "": "idle" } }
}
};
const options = {
actions: {
input: assign((context, event) => ({
...context,
...event
})),
pending: assign({ error: undefined }),
error: assign({
error: (_context, event) => event.data.message
}),
timeout: assign({ error: 'Timeout: No response from backend' })
},
guards: {
valid: context => context.valid,
success: () => true
},
delays: {
TIMEOUT: 2000
}
};
const machine = Machine(config, options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment