我正在尝试创建一个脚本来自动化 xstate FSM 的可视化。因为现在有一种方法可以向 xstate-visualiser 发出 http 请求。我是xstate-plantuml
用来做图的。因为它需要 JSON 作为输入,所以我JSON.stringify()
在我的机器对象上使用并将代码传递给visualize
.
import visualize from "xstate-plantuml";
import encoder from "plantuml-encoder";
import machine from "./machine.json";
const puml = visualize(machine);
const url = "http://www.plantuml.com/plantuml/svg/" + encoder.encode(puml);
const img = document.createElement("img");
img.src = url;
document.querySelector("#app").appendChild(img);
console.log(puml);
我不断收到TypeError: value.replace is not a function
错误,我怀疑这是我的 JSON 结构的问题,因为它似乎可以与它附带的原始示例一起使用。不幸的是,除了 npm 包中的一行之外,该错误没有提及位置。
{
"id": "runner",
"initial": "setup",
"strict": true,
"states": {
"setup": {
"entry": [],
"on": {
"ERROR": {
"target": "error"
},
"TERMINATED": {
"target": "terminated"
},
"RUNNING": {
"target": "running"
},
"HANDLER_EXIT": {
"target": "handleExit"
}
},
"id": "runTaskFSM",
"initial": "pullPackage",
"states": {
"pullPackage": {
"entry": ["reportMachineStart", "pullPackage"],
"on": {
"MAKE_IO_DIR": {
"target": "mkIODir"
}
}
},
"mkIODir": {
"entry": ["mkIODir"],
"on": {
"WRITE_TASK_JSON": {
"target": "writeTaskDataJSON"
}
}
},
"writeTaskDataJSON": {
"entry": ["writeTaskDataJSON"],
"on": {
"DOWNLOAD_INPUTS": {
"target": "downloadInputs"
}
}
},
"downloadInputs": {
"entry": ["downloadInputs"],
"on": {
"LINK_INPUTS": {
"target": "linkInputs"
}
}
},
"linkInputs": {
"entry": ["linkInputs"],
"on": {
"DOWNLOAD_RESOURCES": {
"target": "downloadResources"
}
}
},
"downloadResources": {
"entry": ["downloadResources"],
"on": {
"TASKDIR_PERMISSIONS": {
"target": "chmodTaskDir"
}
}
},
"chmodTaskDir": {
"entry": ["chmodTaskDir"],
"on": {
"JOB_TIMEOUT": {
"target": "jobTimeout"
}
}
},
"jobTimeout": {
"entry": ["jobTimeout"],
"on": {
"INIT_HANDLER": {
"target": "initHandler"
}
}
},
"initHandler": {
"entry": ["initHandler"],
"on": {
"START_JOB": {
"target": "startJob"
}
}
},
"startJob": {
"entry": ["reportUserStart", "taskRunning", "startJob"],
"on": {
"TASK_RUNNING": {
"target": "taskRunning"
}
}
},
"taskRunning": {
"entry": [{
"type": "xstate.send",
"event": {
"type": "RUNNING"
},
"id": "RUNNING"
}]
}
}
},
"running": {
"entry": [],
"on": {
"HANDLER_EXIT": {
"target": "handleExit"
},
"ERROR": {
"target": "error"
},
"TERMINATED": {
"target": "terminated"
}
}
},
"handleExit": {
"entry": ["reportUserStop", "taskHandleExit"],
"on": {
"COMPLETE": {
"target": "complete"
},
"ERROR": {
"target": "error"
}
}
},
"complete": {
"entry": ["taskComplete"],
"on": {
"DESTROY": {
"target": "destroy"
}
}
},
"error": {
"entry": ["taskError"],
"on": {
"DESTROY": {
"target": "destroy"
}
}
},
"terminated": {
"entry": ["reportUserStop", "terminate"],
"on": {
"DESTROY": {
"target": "destroy"
}
}
},
"destroy": {
"entry": ["reportMachineStop", "destroyTask"],
"type": "final"
}
}
}