Created
January 20, 2022 21:20
-
-
Save ahamid/cb72b4656ce8f440b2f1704b4e33a99b 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 fetchMachine = Machine({ | |
id: 'fetch', | |
initial: 'idle', | |
context: { | |
retries: 0 | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: { | |
// actions: ['catchMe'], | |
target: 'loading' | |
}, | |
'xstate.error': { | |
target: 'internal_error' | |
}, | |
'error.execution': { | |
target: 'internal_error' | |
} | |
} | |
}, | |
loading: { | |
entry: ['catchMe2'], | |
on: { | |
RESOLVE: 'success', | |
REJECT: 'failure', | |
'xstate.error': { | |
target: 'internal_error' | |
}, | |
'error.execution': { | |
target: 'internal_error' | |
} | |
} | |
}, | |
success: { | |
type: 'final' | |
}, | |
failure: { | |
on: { | |
RETRY: { | |
target: 'loading', | |
actions: assign({ | |
retries: (context, event) => context.retries + 1 | |
}) | |
} | |
} | |
}, | |
internal_error: { | |
type: 'final' | |
} | |
} | |
}, { | |
actions: { | |
catchMe: () => { | |
throw new Error('event: catch me if you can xD') | |
}, | |
catchMe2: () => { | |
throw new Error('entry: catch me if you can xD') | |
} | |
} | |
}); | |
// const interpreter = interpret(fetchMachine); | |
// console.log('fetch action', interpreter.send('FETCH')); | |
// console.log('state', interpreter.state); | |
// setTimeout(() => { | |
// console.log('state', interpreter.state); | |
// }, 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment