Created
September 1, 2020 17:52
-
-
Save samihult/ba7f19ee7762fd5a50478620bd8eda0f 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
| // Available variables: | |
| // - Machine | |
| // - interpret | |
| // - assign | |
| // - send | |
| // - sendParent | |
| // - spawn | |
| // - raise | |
| // - actions | |
| // - XState (all XState exports) | |
| const fetchMachine = Machine({ | |
| id: "parameterProvider", | |
| context: { | |
| retryCount: 0, | |
| value: null, | |
| error: null | |
| }, | |
| type: "parallel", | |
| states: { | |
| update: { | |
| initial: "initializing", | |
| states: { | |
| initializing: { | |
| entry: assign(context => ({ | |
| ...context, | |
| retryCount: 0 | |
| })), | |
| always: "transit" | |
| }, | |
| error: { | |
| type: "final", | |
| entry: [send("ERROR"), "signal error"] | |
| }, | |
| success: { | |
| type: "final", | |
| entry: send("VALIDATED") | |
| }, | |
| transit: { | |
| after: { | |
| [10000]: { | |
| target: "error", | |
| actions: [ | |
| assign(context => ({ ...context, error: "Hard timeout" })) | |
| ] | |
| } | |
| }, | |
| invoke: { | |
| src: "fetch parameters", | |
| onDone: { | |
| target: "validating", | |
| actions: [ | |
| assign((context, event) => ({ | |
| ...context, | |
| value: event.data | |
| })) | |
| ] | |
| }, | |
| onError: [ | |
| { | |
| target: "coolDown", | |
| cond: context => context.retryCount < 10, | |
| actions: [ | |
| assign(context => ({ | |
| ...context, | |
| retryCount: context.retryCount + 1 | |
| })) | |
| ] | |
| }, | |
| { | |
| target: "error", | |
| actions: [ | |
| assign((context, event) => ({ | |
| ...context, | |
| error: event.data | |
| })) | |
| ] | |
| } | |
| ] | |
| } | |
| }, | |
| coolDown: { | |
| after: { [1000]: "transit" } | |
| }, | |
| validating: { | |
| invoke: { | |
| src: "validate parameters", | |
| onDone: "success", | |
| onError: { | |
| target: "error", | |
| actions: [ | |
| assign((context, event) => ({ | |
| ...context, | |
| error: event.data | |
| })) | |
| ] | |
| } | |
| } | |
| } | |
| }, | |
| on: { | |
| INVALIDATE: "update", | |
| UPDATE: "update" | |
| } | |
| }, | |
| value: { | |
| initial: "uninitialized", | |
| states: { | |
| uninitialized: { | |
| on: { | |
| VALIDATED: "valid", // could have guarded action that sends "actually changed" event | |
| ERROR: "invalid" | |
| } | |
| }, | |
| valid: { | |
| entry: "declare valid", | |
| on: { | |
| ERROR: "invalid", | |
| INVALIDATE: "invalid" | |
| } | |
| }, | |
| invalid: { | |
| entry: "declare invalid", | |
| on: { | |
| VALIDATED: "valid" // could have guarded action that sends "actually changed" event | |
| } | |
| } | |
| } | |
| } | |
| } | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment