Skip to content

Instantly share code, notes, and snippets.

@schpet
Created January 14, 2020 05:58
Show Gist options
  • Select an option

  • Save schpet/ec2c82ca2bae6b226272ef1e70d47b71 to your computer and use it in GitHub Desktop.

Select an option

Save schpet/ec2c82ca2bae6b226272ef1e70d47b71 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
var CommandArgument = {
file: "file",
nearest: "nearest",
none: "none",
}
const findCommandErrorActions = assign({
workspace: (_context, event) => {
return event.data.workspace;
},
errorMessage: (_context, event) => event.data.message,
});
const commandMachine = Machine({
id: "commandFinder",
initial: "started",
strict: true,
context: {
argument: CommandArgument.none,
config: undefined,
workspace: undefined,
errorMessage: undefined,
id: -1,
},
states: {
started: {
invoke: {
src: "configMachine",
onDone: [
{
target: "configured",
cond: (_context, event) => {
return !!(event.data && event.data.config && event.data.workspace);
},
actions: assign({
config: (_context, event) => event.data && event.data.config,
workspace: (_context, event) => event.data && event.data.workspace,
}),
},
{
target: "complete",
},
],
},
},
configured: {
invoke: {
src: "findCommand",
onDone: {
target: "complete",
},
onError: [
{
target: "matchNotFound",
cond: function matchNotFound(_context, event) {
return event.data && event.data.problem === "match_not_found";
},
actions: findCommandErrorActions,
},
{
target: "noEditor",
cond: function noEditor(_context, event) {
return event.data && event.data.problem === "no_editor";
},
actions: assign({
errorMessage: (_context, event) => event.data.message,
}),
},
{
target: "basicError",
actions: findCommandErrorActions,
},
],
},
},
matchNotFound: {
invoke: { src: "renderMatchNotFound" },
on: {
DISMISS: "complete",
EDIT: "edit",
},
},
edit: {
entry: "EDIT_CONFIG",
on: {
"": "complete",
},
},
noEditor: {
invoke: { src: "renderNoEditor" },
on: {
DISMISS: "complete",
},
},
basicError: {
invoke: { src: "renderBasicError" },
on: {
DISMISS: "complete",
},
},
complete: {
type: "final",
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment