Created
March 30, 2020 20:29
-
-
Save colebemis/5d279362bb1e59067a0a27f85fa229b4 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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 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: { | |
| entry: ['clearAccessToken', 'figmaClearAccessToken'], | |
| 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: 'fetchingUsername', | |
| SIGN_OUT: 'unauthenticated' | |
| } | |
| }, | |
| 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: 'unauthenticated' | |
| } | |
| }, | |
| editing: { | |
| type: 'parallel', | |
| entry: ['figmaGetSelection'], | |
| on: { | |
| COMMIT_MESSAGE_CHANGE: { | |
| actions: ['setCommitMessage'] | |
| }, | |
| COMMIT: [ | |
| { | |
| target: 'committingToNewBranch', | |
| in: {editing: {selection: 'valid', branch: 'newBranch'}} | |
| }, | |
| { | |
| target: 'committingToExistingBranch', | |
| in: {editing: {selection: 'valid', branch: 'existingBranch'}} | |
| } | |
| ], | |
| SIGN_OUT: 'unauthenticated' | |
| }, | |
| 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: { | |
| NEW_BRANCH_NAME_CHANGE: { | |
| actions: ['setNewBranchName'] | |
| }, | |
| SWITCH_TO_EXISTING_BRANCH: 'existingBranch' | |
| } | |
| }, | |
| existingBranch: { | |
| on: { | |
| EXISTING_BRANCH_NAME_CHANGE: { | |
| actions: ['setExistingBranchName'] | |
| }, | |
| SWITCH_TO_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', | |
| 'figmaClearSelection', | |
| 'clearSelection', | |
| 'clearCommitMessage', | |
| 'clearDefaultCommitMessage' | |
| ] | |
| }, | |
| onError: { | |
| target: 'errorCommittingToExistingBranch', | |
| actions: ['setError'] | |
| } | |
| } | |
| }, | |
| errorCommittingToNewBranch: { | |
| exit: ['clearError'], | |
| on: { | |
| RETRY: 'committingToNewBranch', | |
| SIGN_OUT: 'unauthenticated', | |
| BACK: { | |
| target: ['editing.selection.hist', 'editing.branch.hist'] | |
| } | |
| } | |
| }, | |
| errorCommittingToExistingBranch: { | |
| exit: ['clearError'], | |
| on: { | |
| RETRY: 'committingToExistingBranch', | |
| SIGN_OUT: 'unauthenticated', | |
| BACK: { | |
| target: ['editing.selection.hist', 'editing.branch.hist'] | |
| } | |
| } | |
| }, | |
| doneCommittingToNewBranch: { | |
| on: { | |
| COPY_TO_CLIPBOARD: { | |
| actions: ['copyToClipboard', 'figmaNotifyCopied'] | |
| }, | |
| DONE: { | |
| target: 'editing.branch.existingBranch', | |
| actions: [ | |
| 'moveNewBranchNameToExistingBranchName', | |
| 'figmaClearSelection', | |
| 'clearSelection', | |
| 'clearCommitMessage', | |
| 'clearDefaultCommitMessage' | |
| ] | |
| }, | |
| SIGN_OUT: 'unauthenticated' | |
| } | |
| } | |
| } | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment