Last active
March 26, 2020 01:14
-
-
Save colebemis/4d31868e8fadb1edaa5d0ab2e8bc6094 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 commitMachine = Machine({ | |
id: 'commit', | |
initial: 'editing', | |
context: { | |
newBranchName: '', | |
existingBranchName: '', | |
selection: [], | |
error: '' | |
}, | |
states: { | |
editing: { | |
type: 'parallel', | |
states: { | |
selection: { | |
initial: 'empty', | |
on: { | |
CHANGE_SELECTION: [ | |
{ | |
target: '.empty', | |
cond: 'isEmpty', | |
actions: ['setSelection'] | |
}, | |
{ | |
target: '.valid', | |
actions: ['setSelection'] | |
} | |
] | |
}, | |
states: { | |
hist: { | |
type: 'history' | |
}, | |
empty: {}, | |
valid: {}, | |
} | |
}, | |
branch: { | |
initial: 'newBranch', | |
states: { | |
hist: { | |
type: 'history', | |
history: 'deep' | |
}, | |
newBranch: { | |
initial: 'empty', | |
on: { | |
SET_EXISTING_BRANCH: [ | |
{ | |
target: 'existingBranch.empty', | |
cond: 'isExistingBranchNameEmpty' | |
}, | |
{ | |
target: 'existingBranch.valid' | |
} | |
], | |
CHANGE_NEW_BRANCH_NAME: [ | |
{ | |
target: '.empty', | |
cond: 'isEmpty', | |
actions: ['setNewBranchName'] | |
}, | |
{ | |
target: '.valid', | |
actions: ['setNewBranchName'] | |
} | |
] | |
}, | |
states: { | |
empty: {}, | |
valid: {} | |
} | |
}, | |
existingBranch: { | |
initial: 'empty', | |
on: { | |
SET_NEW_BRANCH: [ | |
{ | |
target: 'newBranch.empty', | |
cond: 'isNewBranchNameEmpty' | |
}, | |
{ | |
target: 'newBranch.valid' | |
} | |
], | |
CHANGE_EXISTING_BRANCH_NAME: [ | |
{ | |
target: '.empty', | |
cond: 'isEmpty', | |
actions: ['setExistingBranchName'] | |
}, | |
{ | |
target: '.valid', | |
actions: ['setExistingBranchName'] | |
} | |
] | |
}, | |
states: { | |
empty: {}, | |
valid: {} | |
} | |
} | |
} | |
} | |
}, | |
on: { | |
SUBMIT: [ | |
{ | |
target: 'committingToNewBranch', | |
in: {editing: {selection: 'valid', branch: {newBranch: 'valid'}}}, | |
actions: ['clearError'] | |
}, | |
{ | |
target: 'committingToExistingBranch', | |
in: {editing: {selection: 'valid', branch: {existingBranch: 'valid'}}}, | |
actions: ['clearError'] | |
} | |
] | |
} | |
}, | |
committingToNewBranch: { | |
on: { | |
SUCCESS: 'success', | |
ERROR: { | |
target: ['editing.selection.hist', 'editing.branch.hist'], | |
actions: ['setError'] | |
} | |
} | |
}, | |
committingToExistingBranch: { | |
on: { | |
SUCCESS: { | |
target: ['editing.selection', 'editing.branch.existingBranch.valid'], | |
actions: ['clearSelection', 'notifySuccess'] | |
}, | |
ERROR: { | |
target: ['editing.selection.hist', 'editing.branch.hist'], | |
actions: ['setError'] | |
} | |
} | |
}, | |
success: { | |
on: { | |
COPY_TO_CLIPBOARD: { | |
actions: ['copyToClipboard', 'notifyCopied'] | |
}, | |
DONE: { | |
target: ['editing.selection', 'editing.branch.existingBranch.valid'], | |
actions: ['clearSelection', 'moveNewBranchNameToExistingBranchName'] | |
} | |
} | |
} | |
} | |
}, | |
{ | |
actions: { | |
setNewBranchName: assign({ | |
newBranchName: (context, event) => event.value | |
}), | |
setExistingBranchName: assign({ | |
existingBranchName: (context, event) => event.value | |
}), | |
setSelection: assign({ | |
selection: (context, event) => event.value | |
}), | |
setError: assign({ | |
error: (context, event) => event.value | |
}), | |
clearError: assign({ | |
error: '' | |
}), | |
moveNewBranchNameToExistingBranchName: assign({ | |
existingBranchName: context => context.newBranchName, | |
newBranchName: '' | |
}) | |
}, | |
guards: { | |
isEmpty: (context, event) => { | |
if (typeof event.value === 'string') { | |
return event.value === '' | |
} | |
if (Array.isArray(event.value)) { | |
return event.value.length === 0 | |
} | |
return true | |
}, | |
isNewBranchNameEmpty: context => context.newBranchName === '', | |
isExistingBranchNameEmpty: context => context.existingBranchName === '' | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment