Skip to content

Instantly share code, notes, and snippets.

@mkarajohn
Created April 2, 2020 17:00
Show Gist options
  • Save mkarajohn/a8b8cbee583e444d654b53f1071c23e9 to your computer and use it in GitHub Desktop.
Save mkarajohn/a8b8cbee583e444d654b53f1071c23e9 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: "pending",
cond: "hasFiles",
},
CLOSE: {
target: "#upload_system.idle",
actions: "removeAllFiles",
},
},
},
pending: {
on: {
RESOLVE: [
{
target: "completed",
actions: "removeAllFiles",
},
],
REJECT: [
{
target: "completed",
actions: "removeAllFiles",
},
],
},
initial: 'uploading',
states: {
uploading: {
on: {
CANCEL: "cancel",
},
},
cancel: {
on: {
YES: {
target: "#upload_system.idle",
actions: "removeAllFiles",
},
NO: "uploading",
},
},
},
},
completed: {
on: {
CLOSE: "#upload_system.idle",
},
},
},
},
},
},
{
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