Created
October 5, 2021 12:09
-
-
Save benediktarnold/3b65f4ca0db4c04dc8f659672bc4bb61 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 fetchStates = (key)=> ({ | |
initial: 'idle', | |
states: { | |
idle: { | |
after: { | |
1: { | |
target: 'success', | |
cond: { | |
type: 'hasData', | |
key | |
} | |
}, | |
2: {target: 'loading'} | |
} | |
}, | |
loading: { | |
on: { | |
RESOLVE: { | |
target:'success', | |
actions: assign({[key]:(context, event) =>"foo"}) | |
}, | |
REJECT: 'failure' | |
} | |
}, | |
success: { | |
type: 'final', | |
}, | |
failure: { | |
on: { | |
RETRY: { | |
target: 'loading' | |
} | |
} | |
} | |
} | |
}) | |
const fetchMachine = Machine({ | |
id: 'fetch', | |
initial: 'a', | |
context: { | |
retries: 0 | |
}, | |
states: { | |
a: { | |
...fetchStates('a'), | |
on: { | |
B: 'b' | |
} | |
}, | |
b: { | |
...fetchStates('b'), | |
on: { | |
A: 'a' | |
} | |
} | |
}, | |
}, { | |
guards: { | |
hasData: (context, event, {cond}) => { | |
return context[cond.key] !== undefined; | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment