Skip to content

Instantly share code, notes, and snippets.

@mkarajohn
Last active April 1, 2020 04:26
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 mkarajohn/4bf804fd3792790a373e8dcbb4c91b52 to your computer and use it in GitHub Desktop.
Save mkarajohn/4bf804fd3792790a373e8dcbb4c91b52 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: 'upload_system',
initial: 'idle',
context: {
files: 0,
},
states: {
idle: {
on: {
OPEN: 'active',
},
},
active: {
initial: 'waitForFiles',
states: {
waitForFiles: {
on: {
ADD: {
target: 'waitForFiles',
actions: 'addFile',
},
UPLOAD: {
target: 'uploading',
cond: 'hasFiles',
},
CLOSE: {
target: '#upload_system.idle',
actions: 'removeAllFiles',
},
},
},
uploading: {
type: 'parallel',
states: {
inProgress: {
on: {
RESOLVE: [
{
target: '#upload_system.active.completed',
actions: 'removeAllFiles',
},
],
REJECT: [
{
target: '#upload_system.active.completed',
actions: 'removeAllFiles',
},
],
},
},
cancel: {
on: {
YES: {
target: '#upload_system.idle',
actions: 'removeAllFiles',
},
NO: 'cancel',
},
},
},
},
completed: {
on: {
CLOSE: '#upload_system.idle',
},
},
},
},
// success: {
// on: {
// '': {
// target: 'active.uploading.inProgress',
// cond: 'hasFiles',
// },
// '': {
// target: 'completed',
// cond: 'hasNoFiles',
// },
// },
// },
// error: {
// on: {
// '': {
// target: 'active.uploading.inProgress',
// cond: 'hasFiles',
// },
// '': {
// target: 'completed',
// cond: 'hasNoFiles',
// },
// },
// },
},
},
{
guards: {
hasFiles: (context, event) => {
return context.files > 0;
},
hasNoFiles: (context, event) => {
return context.files === 0;
},
},
actions: {
addFile: assign({
files: (context, event) => {
const newValue = context.files + 1;
console.log('Added file');
console.log('total files', newValue);
console.log('event', event);
return newValue;
},
}),
removeFile: assign({
files: (context, event) => context.files - 1,
}),
removeAllFiles: assign({
files: 0,
}),
},
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment