Created
September 2, 2020 18:36
-
-
Save dkundel/8fa3f2629df8d05b7a24bbdb461ddd69 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
function assignResult(name) { | |
return assign({ | |
results: (context, event) => { | |
console.log(event); | |
return { ...context.results, [name]: event.data }; | |
}, | |
}); | |
} | |
const log = (context, event) => { | |
if (process.env.DEBUG_SIGNAL) { | |
console.log('Context %o', context); | |
console.log('Event %o', event); | |
} | |
}; | |
const assignDirectory = assign({ | |
directory: (context, event) => event.data.directory, | |
}); | |
const handleError = assign({ | |
error: (context, event) => [...context.error, event.data], | |
}); | |
const installerMachine = Machine({ | |
id: 'installer', | |
initial: 'askingForDirectory', | |
context: { | |
directory: undefined, | |
repoUrl: undefined, | |
language: undefined, | |
isEmptyDirectory: undefined, | |
results: {}, | |
error: [], | |
}, | |
states: { | |
idle: { | |
on: { | |
START: 'askingForDirectory', | |
}, | |
}, | |
askingForDirectory: { | |
on: { | |
PROVIDE_DIRECTORY: { | |
actions: [assignDirectory], | |
target: 'creatingDirectory', | |
}, | |
}, | |
}, | |
creatingDirectory: { | |
invoke: { | |
id: 'createDirectory', | |
src: 'createDirectory', | |
onDone: { | |
target: 'confirmingDownload', | |
actions: ['updateDirectory', assignResult('createDirectory')], | |
}, | |
onError: { | |
target: 'failure', | |
actions: [handleError], | |
}, | |
}, | |
}, | |
confirmingDownload: { | |
on: { | |
CONFIRM: 'downloading', | |
DENY: { | |
target: 'failure.cancelled', | |
}, | |
}, | |
}, | |
downloading: { | |
invoke: { | |
id: 'download', | |
src: 'download', | |
onDone: [ | |
{ | |
target: 'confirmInstallingDependencies', | |
cond: { type: 'hasInstallStep' }, | |
actions: [assignResult('download')], | |
}, | |
{ | |
target: 'confirmConfiguring', | |
cond: { type: 'hasSetupStep' }, | |
actions: [assignResult('download')], | |
}, | |
{ | |
target: 'success', | |
actions: [log, assignResult('download')], | |
}, | |
], | |
onError: { | |
target: 'failure', | |
actions: [handleError], | |
}, | |
}, | |
}, | |
confirmInstallingDependencies: { | |
on: { | |
CONFIRM: 'installingDependencies', | |
DENY: { | |
target: 'confirmConfiguring', | |
}, | |
}, | |
}, | |
installingDependencies: { | |
invoke: { | |
id: 'installDependencies', | |
src: 'installDependencies', | |
onDone: [ | |
{ | |
cond: { type: 'hasSetupStep' }, | |
target: 'confirmConfiguring', | |
actions: [assignResult('installDependencies')], | |
}, | |
{ target: 'success', actions: [assignResult('installDependencies')] }, | |
], | |
onError: { | |
target: 'failure', | |
actions: [handleError], | |
}, | |
}, | |
}, | |
confirmConfiguring: { | |
on: { | |
CONFIRM: 'configuring', | |
DENY: { | |
target: 'success', | |
}, | |
}, | |
}, | |
configuring: { | |
invoke: { | |
id: 'configure', | |
src: 'configure', | |
onDone: { | |
target: 'success', | |
actions: [assignResult('configure')], | |
}, | |
onError: { | |
target: 'failure', | |
actions: [handleError], | |
}, | |
}, | |
}, | |
success: { | |
type: 'final', | |
data: { | |
directory: (context) => context.directory, | |
}, | |
meta: { message: 'Your sample has been installed. Enjoy!' }, | |
}, | |
failure: { | |
initial: 'error', | |
states: { | |
error: { | |
meta: { message: 'Something went wrong.' }, | |
actions: [handleError], | |
type: 'final', | |
}, | |
cancelled: { | |
meta: { message: 'Feel free to start again.' }, | |
on: { | |
RESTART: '#installer.askingForDirectory', | |
}, | |
}, | |
}, | |
meta: { message: 'Did not finish creating sample.' }, | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment