Skip to content

Instantly share code, notes, and snippets.

@yleflour
Last active April 23, 2020 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yleflour/14bd869adec563073e2b7ff989ed3dbf to your computer and use it in GitHub Desktop.
Save yleflour/14bd869adec563073e2b7ff989ed3dbf to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: 'library',
initial: 'hydration',
strict: true,
context: {
gameUrl: undefined,
games: [],
selectedGames: [],
},
states: {
hydration: {
invoke: {
id: 'async.fetchLibrary',
src: 'async.fetchLibrary',
onDone: {
target: 'shareHydration',
actions: 'ctx.addGames',
},
onError: 'shareHydration',
},
},
shareHydration: {
invoke: {
id: 'async.getInitialShare',
src: 'async.getInitialShare',
onDone: [
{
cond: 'cond.emptyUrl',
target: 'idle',
},
{
target: 'addition.downloading',
actions: [
'ctx.setGameUrl'
],
},
],
onError: 'idle',
},
},
idle: {
on: {
CONFIRM: 'addition',
LONGPRESS_CART: {
target: 'deletion',
actions: 'ctx.selectCart',
},
PRESS_CART: {
actions: ['se.goToCart'],
},
},
},
addition: {
initial: 'idle',
invoke: {
id: 'add-back-press',
src: 'handleBackPress',
},
states: {
idle: {
on: {
CONFIRM: {
cond: 'cond.urlNotEmpty',
target: 'downloading',
},
},
},
downloading: {
invoke: {
id: 'fetch-cart',
src: 'fetchCart',
onDone: {
target: 'success',
actions: [
'ctx.addGame'
],
},
onError: {
target: 'idle',
actions: [
'se.displayError'
],
},
},
},
success: {
type: 'final',
},
},
on: {
WRITE: {
actions: 'ctx.setGameUrl',
},
CANCEL: {
target: 'idle',
actions: 'ctx.resetUrl',
},
LONGPRESS_CART: {
target: 'deletion',
actions: ['ctx.selectCart'],
},
PRESS_CART: {
target: 'idle',
actions: [
'ctx.resetUrl',
'se.goToCart',
],
},
},
onDone: {
target: 'idle',
actions: ['se.persist'],
},
},
deletion: {
invoke: {
id: 'svc.backPressListener',
src: 'svc.backPressListener',
},
on: {
PRESS_CART: {
actions: 'ctx.selectCart',
},
LONGPRESS_CART: {
actions: 'ctx.selectCart',
},
CONFIRM: {
target: 'idle',
actions: ['ctx.removeSelectedCarts', 'se.persist'],
},
CANCEL: {
target: 'idle',
actions: ['ctx.resetSelectedCarts'],
},
},
},
},
invoke: {
id: 'listen-share',
src: 'svc.shareListener',
},
on: {
SHARE: {
target: 'addition.downloading',
actions: [
'ctx.setGameUrl',
],
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment