Skip to content

Instantly share code, notes, and snippets.

@massimoselvi
Last active December 20, 2020 08:06
Show Gist options
  • Save massimoselvi/31505a4986fab5d82190d55eec826648 to your computer and use it in GitHub Desktop.
Save massimoselvi/31505a4986fab5d82190d55eec826648 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/*
* Entity CRUD
* preview: https://xstate.js.org/viz/?gist=31505a4986fab5d82190d55eec826648
*/
const machine = Machine({
id: 'handler',
initial: 'idle',
states: {
idle: {
type: 'atomic',
on: {
INVOKE: 'entity',
},
},
// pending: {
// type: 'parallel',
// states: {
// resource1: {
// type: 'compound',
// initial: 'pending',
// states: {
// pending: {
// on: {
// 'FULFILL.resource1': 'success',
// },
// },
// success: {
// type: 'final',
// },
// },
// },
// resource2: {
// type: 'compound',
// initial: 'pending',
// states: {
// pending: {
// on: {
// 'FULFILL.resource2': 'success',
// },
// },
// success: {
// type: 'final',
// },
// },
// },
// },
// onDone: 'loopThroughList',
// },
entity: {
// type: 'compound',
initial: 'list',
states: {
list: {
on: {
'ITEM.CLICK': 'item',
CREATE: 'create',
},
},
create: {
initial: 'form',
context: {
retries: 0,
},
states: {
form: {
on: {
ADD: 'saving',
},
},
saving: {
on: {
RESOLVE: 'success',
REJECT: 'failure',
},
},
success: {
type: 'final',
},
failure: {
on: {
RETRY: {
target: 'saving',
actions: assign({
retries: (context, event) => context.retries + 1,
}),
},
},
},
},
},
item: {
initial: 'show',
states: {
show: {
on: {
// BACK_TO_LIST: 'list',
EDIT: 'edit',
DELETE: 'delete',
},
},
edit: {
initial: 'form',
context: {
retries: 0,
},
states: {
form: {
on: {
ADD: 'saving',
},
},
saving: {
on: {
RESOLVE: 'success',
REJECT: 'failure',
},
},
success: {
type: 'final',
},
failure: {
on: {
RETRY: {
target: 'saving',
actions: assign({
retries: (context, event) => context.retries + 1,
}),
},
},
},
},
// on: {
// CANCEL: 'show',
// UPDATE: 'show' // async/remote
// }
},
delete: {
initial: 'form',
context: {
retries: 0,
},
states: {
form: {
on: {
ADD: 'saving',
},
},
saving: {
on: {
RESOLVE: 'success',
REJECT: 'failure',
},
},
success: {
type: 'final',
},
failure: {
on: {
RETRY: {
target: 'saving',
actions: assign({
retries: (context, event) => context.retries + 1,
}),
},
},
},
},
// on: {
// CANCEL: 'show',
// DELETE: 'show' // async/remote
// }
},
},
},
// on: {
// BACK_TO_LIST: 'list',
// ADD: 'list' // async/remote
// }
},
// show: {
// on: {
// BACK_TO_LIST: 'list',
// EDIT: 'edit' // async/remote
// }
// },
// edit: {
// on: {
// CANCEL: 'show',
// UPDATE: 'show' // async/remote
// }
// },
// delete: {
// on: {
// CANCEL: 'show',
// DELETE: 'show' // async/remote
// }
// }
},
// hist: {
// type: 'history',
// history: 'shallow',
// },
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment