Skip to content

Instantly share code, notes, and snippets.

@colebemis
Created March 29, 2020 03:13
Show Gist options
  • Save colebemis/e3e947d258daa1df22210f1cf7faf486 to your computer and use it in GitHub Desktop.
Save colebemis/e3e947d258daa1df22210f1cf7faf486 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: 'stateMachine',
initial: 'authenticating',
context: {
accessToken: '',
username: '',
branches: [],
selection: [],
commitMessage: '',
defaultCommitMessage: '',
newBranchName: '',
existingBranchName: '',
error: ''
},
states: {
authenticating: {
entry: ['figmaAuthenticate'],
on: {
SIGNED_IN: {
target: 'fetchingUsername',
actions: ['setAccessToken']
},
NOT_SIGNED_IN: 'unauthenticated'
}
},
unauthenticated: {
on: {
ACCESS_TOKEN_CHANGE: {
actions: ['setAccessToken']
},
SIGN_IN: {
target: 'fetchingUsername',
actions: ['figmaSetAccessToken']
}
}
},
fetchingUsername: {
invoke: {
id: 'fetchUsername',
src: 'fetchUsername',
onDone: {
target: 'fetchingBranches',
actions: ['setUsername']
},
onError: {
target: 'errorFetchingUsername',
actions: ['setError']
}
}
},
errorFetchingUsername: {
exit: 'clearError',
on: {
RETRY: {
target: 'fetchingUsername'
},
SIGN_OUT: {
target: 'unauthenticated',
actions: ['clearAccessToken', 'figmaClearAccessToken']
}
}
},
fetchingBranches: {
invoke: {
id: 'fetchBranches',
src: 'fetchBranches',
onDone: {
target: ['editing.selection.hist', 'editing.branch.hist'],
actions: ['setBranches']
},
onError: {
target: 'errorFetchingBranches',
actions: ['setError']
}
}
},
errorFetchingBranches: {
exit: 'clearError',
on: {
RETRY: 'fetchingBranches',
SIGN_OUT: {
target: 'unauthenticated',
actions: ['clearAccessToken', 'figmaClearAccessToken']
}
}
},
editing: {
type: 'parallel',
entry: ['figmaGetSelection'],
on: {
CHANGE_COMMIT_MESSAGE: {
actions: ['setCommitMessage']
},
COMMIT: [
{
target: 'committingToNewBranch',
in: {editing: {selection: 'valid', branch: 'newBranch'}}
},
{
target: 'committingToExistingBranch',
in: {editing: {selection: 'valid', branch: 'existingBranch'}}
}
],
SIGN_OUT: {
target: 'unauthenticated',
actions: ['clearAccessToken', 'figmaClearAccessToken']
}
},
states: {
selection: {
initial: 'empty',
on: {
SELECTION_CHANGE: [
{
target: '.empty',
cond: 'isEmpty',
actions: ['setSelection', 'setDefaultCommitMessage']
},
{
target: '.valid',
actions: ['setSelection', 'setDefaultCommitMessage']
}
]
},
states: {
hist: {type: 'history'},
empty: {},
valid: {}
}
},
branch: {
initial: 'newBranch',
states: {
hist: {
type: 'history'
},
newBranch: {
on: {
CHANGE_NEW_BRANCH_NAME: {
actions: ['setNewBranchName']
},
SET_EXISTING_BRANCH: 'existingBranch'
}
},
existingBranch: {
on: {
CHANGE_EXISTING_BRANCH_NAME: {
actions: ['setExistingBranchName']
},
SET_NEW_BRANCH: 'newBranch'
}
}
}
}
}
},
committingToNewBranch: {
invoke: {
id: 'commitToNewBranch',
src: 'commitToNewBranch',
onDone: {
target: 'doneCommittingToNewBranch',
actions: ['addNewBranchToBranches']
},
onError: {
target: 'errorCommittingToNewBranch',
actions: ['setError']
}
}
},
committingToExistingBranch: {
invoke: {
id: 'commitToExistingBranch',
src: 'commitToExistingBranch',
onDone: {
target: 'editing.branch.existingBranch',
actions: [
'figmaNotifyDoneCommittingToExistingBranch',
'clearSelection',
'clearCommitMessage',
'clearDefaultCommitMessage',
'figmaClearSelection'
]
},
onError: {
target: 'errorCommittingToExistingBranch',
actions: ['setError']
}
}
},
errorCommittingToNewBranch: {
exit: 'clearError',
on: {
RETRY: {
target: 'committingToNewBranch'
},
SIGN_OUT: {
target: 'unauthenticated',
actions: ['clearAccessToken', 'figmaClearAccessToken']
},
BACK: {
target: ['editing.selection.hist', 'editing.branch.hist']
}
}
},
errorCommittingToExistingBranch: {
exit: 'clearError',
on: {
RETRY: {
target: 'committingToExistingBranch'
},
SIGN_OUT: {
target: 'unauthenticated',
actions: ['clearAccessToken', 'figmaClearAccessToken']
},
BACK: {
target: ['editing.selection.hist', 'editing.branch.hist']
}
}
},
doneCommittingToNewBranch: {
on: {
COPY_TO_CLIPBOARD: {
actions: ['copyToClipboard', 'figmaNotifyCopied']
},
DONE: {
target: 'editing.branch.existingBranch',
actions: [
'moveNewBranchNameToExistingBranchName',
'clearSelection',
'clearCommitMessage',
'clearDefaultCommitMessage',
'figmaClearSelection'
]
},
SIGN_OUT: {
target: 'unauthenticated',
actions: ['clearAccessToken', 'figmaClearAccessToken']
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment