@soonflow/core
Advanced tools
Comparing version 0.2.8 to 0.2.9
@@ -1,179 +0,1 @@ | ||
import { Signal } from '@robotlegsjs/signals'; | ||
function findNodeRefEdges(nodeId, outputHandleId) { | ||
const edges = this.edges.filter((edge) => edge.sourceNodeId === nodeId && | ||
edge.sourceNodeHandleId === outputHandleId); | ||
return edges; | ||
} | ||
class CommonHandler { | ||
/** | ||
* 对流程执行特定操作 | ||
* @param this SoonFlowParse 对象 or SoonFlowUnregister 对象 | ||
* @param handler 要执行的函数 | ||
* @returns void | ||
*/ | ||
async _runFlowHandler(handler) { | ||
const startNodes = this.nodes.filter(({ isStartNode }) => isStartNode); | ||
if (startNodes.length === 0) | ||
return Promise.reject('In SoonFlow: CommonHandler _runFlowHandler not find startNode'); | ||
for (let i = 0; i < startNodes.length; i++) { | ||
for (let j = 0; j < startNodes[i].outputs.length; j++) { | ||
await handler.call(this, startNodes[i], startNodes[i].outputs[j]); | ||
} | ||
} | ||
} | ||
/** | ||
* 对流程节点进行操作 | ||
* @param this SoonFlowParse 对象 or SoonFlowUnregister 对象 | ||
* @param optType 解析或注销 | ||
* @param node 当前节点 | ||
* @param outputHandle 输出项 | ||
* @returns void | ||
*/ | ||
async _operateFlowNode(optType, node, outputHandle) { | ||
const edges = findNodeRefEdges.call(this, node.id, outputHandle.id); | ||
if (edges.length === 0) | ||
return; | ||
const targetNodeIds = edges.map((edge) => edge.targetNodeId); | ||
for (let index = 0; index < targetNodeIds.length; index++) { | ||
const id = targetNodeIds[index]; | ||
const nextNode = this.nodes.find((node) => node.id === id); | ||
if (!nextNode) | ||
return Promise.reject('In SoonFlow: CommonHandler _operateFlowNode not find nextNode'); | ||
const { type } = nextNode; | ||
const nodeTypeMethod = this.nodeTypeMethods[type]; | ||
if (!nodeTypeMethod) | ||
return Promise.reject('In SoonFlow: CommonHandler _operateFlowNode not find nodeTypeMethod'); | ||
await nodeTypeMethod[optType]?.call(this, { | ||
prevNode: node, | ||
node: nextNode, | ||
inputData: outputHandle.data, | ||
}); | ||
} | ||
} | ||
} | ||
class SoonFlowParse extends CommonHandler { | ||
nodes; | ||
edges; | ||
nodeTypeMethods; | ||
outInstances; | ||
#signalMap = new Map(); | ||
/** | ||
* 流程解析 | ||
* @param nodes 流程节点 | ||
* @param edges 流程连接线 | ||
* @param nodeTypeMethods 节点方法 | ||
* @param outInstances 外部实例,e.g. spp 对象等 | ||
*/ | ||
constructor(nodes, edges, nodeTypeMethods, outInstances) { | ||
super(); | ||
this.nodes = nodes; | ||
this.edges = edges; | ||
this.nodeTypeMethods = nodeTypeMethods; | ||
this.outInstances = outInstances; | ||
// this.#runParse() | ||
} | ||
// async #start() { | ||
// await this.#runReady() | ||
// this.#runParse() | ||
// } | ||
getOutInstance(key) { | ||
return this.outInstances[key]; | ||
} | ||
getPrevNodeList(targetNodeHandleId) { | ||
const edges = this.edges.filter((edge) => edge.targetNodeHandleId === targetNodeHandleId); | ||
const findNode = []; | ||
edges.forEach((edge) => { | ||
this.nodes.forEach((node) => { | ||
if (node.outputs.some((output) => output.id === edge.sourceNodeHandleId)) { | ||
if (!findNode.find((fn) => fn.id === node.id)) { | ||
findNode.push(node); | ||
} | ||
} | ||
}); | ||
}); | ||
return findNode; | ||
} | ||
// async #runReady() { | ||
// for (let index = 0; index < this.nodes.length; index++) { | ||
// const node = this.nodes[index] | ||
// const typeMethod = this.nodeTypeMethods[node.type] | ||
// await typeMethod?.ready?.(node) | ||
// } | ||
// } | ||
async runParse() { | ||
return this._runFlowHandler(this.parseFlowNode); | ||
} | ||
async parseFlowNode(node, outputHandle) { | ||
return this._operateFlowNode('parse', node, outputHandle); | ||
} | ||
addSignal(key, fn) { | ||
const signal = new Signal(); | ||
signal.add(fn); | ||
this.#signalMap.set(key, signal); | ||
} | ||
dispatchSignal(key) { | ||
this.#signalMap.get(key)?.dispatch(); | ||
} | ||
} | ||
class SoonFlowUnregister extends CommonHandler { | ||
nodes; | ||
edges; | ||
nodeTypeMethods; | ||
outInstances; | ||
/** | ||
* 流程注销 | ||
* @param nodes 流程节点 | ||
* @param edges 流程连接线 | ||
* @param nodeTypeMethods 节点方法 | ||
* @param outInstances 外部实例,e.g. spp 对象等 | ||
*/ | ||
constructor(nodes, edges, nodeTypeMethods, outInstances) { | ||
super(); | ||
this.nodes = nodes; | ||
this.edges = edges; | ||
this.nodeTypeMethods = nodeTypeMethods; | ||
this.outInstances = outInstances; | ||
this.#unregisterFlow(); | ||
} | ||
#unregisterFlow() { | ||
this._runFlowHandler(this.unregisterFlowNode); | ||
} | ||
async unregisterFlowNode(node, outputHandle) { | ||
return this._operateFlowNode('unregister', node, outputHandle); | ||
} | ||
} | ||
const error = (message, ...args) => { | ||
console.error(`In SoonFlow: ${message}`, ...args); | ||
}; | ||
class SoonFlow { | ||
nodeTypeMethods; | ||
outInstances; | ||
constructor(nodeTypeMethods, outInstances) { | ||
this.nodeTypeMethods = nodeTypeMethods; | ||
this.outInstances = outInstances; | ||
} | ||
/** | ||
* 解析流程 | ||
* @param nodes 流程节点 | ||
* @param edges 流程连接线 | ||
*/ | ||
async parseFlow(nodes, edges) { | ||
const parse = new SoonFlowParse(nodes, edges, this.nodeTypeMethods, this.outInstances); | ||
return parse.runParse(); | ||
} | ||
/** | ||
* 注销流程 | ||
* @param nodes 流程节点 | ||
* @param edges 流程连接线 | ||
*/ | ||
unregisterFlow(nodes, edges) { | ||
new SoonFlowUnregister(nodes, edges, this.nodeTypeMethods, this.outInstances); | ||
} | ||
} | ||
export { SoonFlowParse, SoonFlowUnregister, SoonFlow as default, error }; | ||
import{Signal as e}from"@robotlegsjs/signals";function t(e,t){return this.edges.filter((s=>s.sourceNodeId===e&&s.sourceNodeHandleId===t))}class s{async _runFlowHandler(e){const t=this.nodes.filter((({isStartNode:e})=>e));if(0===t.length)return Promise.reject("In SoonFlow: CommonHandler _runFlowHandler not find startNode");for(let s=0;s<t.length;s++)for(let o=0;o<t[s].outputs.length;o++)await e.call(this,t[s],t[s].outputs[o])}async _operateFlowNode(e,s,o){const n=t.call(this,s.id,o.id);if(0===n.length)return;const r=n.map((e=>e.targetNodeId));for(let t=0;t<r.length;t++){const n=r[t],d=this.nodes.find((e=>e.id===n));if(!d)return Promise.reject("In SoonFlow: CommonHandler _operateFlowNode not find nextNode");const{type:i}=d,a=this.nodeTypeMethods[i];if(!a)return Promise.reject("In SoonFlow: CommonHandler _operateFlowNode not find nodeTypeMethod");a[e]?.call(this,{prevNode:s,node:d,inputData:o.data})}}}class o extends s{nodes;edges;nodeTypeMethods;outInstances;#e=new Map;constructor(e,t,s,o){super(),this.nodes=e,this.edges=t,this.nodeTypeMethods=s,this.outInstances=o}getOutInstance(e){return this.outInstances[e]}getPrevNodeList(e){const t=this.edges.filter((t=>t.targetNodeHandleId===e)),s=[];return t.forEach((e=>{this.nodes.forEach((t=>{t.outputs.some((t=>t.id===e.sourceNodeHandleId))&&(s.find((e=>e.id===t.id))||s.push(t))}))})),s}async runParse(){return this._runFlowHandler(this.parseFlowNode)}async parseFlowNode(e,t){return this._operateFlowNode("parse",e,t)}addSignal(t,s){const o=new e;o.add(s),this.#e.set(t,o)}dispatchSignal(e){this.#e.get(e)?.dispatch()}}class n extends s{nodes;edges;nodeTypeMethods;outInstances;constructor(e,t,s,o){super(),this.nodes=e,this.edges=t,this.nodeTypeMethods=s,this.outInstances=o,this.#t()}#t(){this._runFlowHandler(this.unregisterFlowNode)}async unregisterFlowNode(e,t){return this._operateFlowNode("unregister",e,t)}}const r=(e,...t)=>{console.error(`In SoonFlow: ${e}`,...t)};class d{nodeTypeMethods;outInstances;constructor(e,t){this.nodeTypeMethods=e,this.outInstances=t}async parseFlow(e,t){return new o(e,t,this.nodeTypeMethods,this.outInstances).runParse()}unregisterFlow(e,t){new n(e,t,this.nodeTypeMethods,this.outInstances)}}export{o as SoonFlowParse,n as SoonFlowUnregister,d as default,r as error}; |
{ | ||
"name": "@soonflow/core", | ||
"version": "0.2.8", | ||
"version": "0.2.9", | ||
"description": "Ssoonflow Core", | ||
@@ -17,3 +17,3 @@ "main": "dist/index.esm.js", | ||
}, | ||
"gitHead": "b5f1024a262891e3977e83affed5b22a756d7699" | ||
"gitHead": "b07a4f680e3b08db730236fe3b4dcbf832c05f0d" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19745
10
139