Last active
August 12, 2019 11:04
-
-
Save mikaelkaron/6f8dd23a3cb383958609af88d4c03303 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
// 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