Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Available variables:
- // - Machine
- // - interpret
- // - assign
- // - send
- // - sendParent
- // - spawn
- // - raise
- // - actions
- // - XState (all XState exports)
- const fetchMachine = Machine({
- id: 'fetch',
- initial: 'idle',
- context: {
- url: null,
- data: null,
- retries: 0,
- maxRetries: 3
- },
- states: {
- idle: {
- on: {
- FETCH: 'loading',
- }
- },
- loading: {
- on: {
- RESOLVE: {
- target: 'success',
- actions: assign({
- })
- },
- REJECT: 'failure'
- }
- },
- success: {
- on: {
- REFETCH: 'loading',
- }
- },
- failure: {
- on: {
- RETRY: {
- target: 'loading',
- cond: 'canRetry',
- actions: 'incrementRetry'
- }
- }
- }
- }
- }, {
- guards: {
- canRetry: (context, event) => {
- return context.retries < context.maxRetries;
- }
- },
- actions: {
- incrementRetry: assign({
- retries: (context, event) => context.retries + 1
- })
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement