From e400175ffe89ab3105e50d2ffcd702d9d2a12970 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Wed, 10 Sep 2025 16:24:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BB=84=E4=BB=B6=E5=A3=B0=E6=98=8E?= =?UTF-8?q?=E5=91=A8=E6=9C=9F=E5=87=BD=E6=95=B0=E9=85=8D=E7=BD=AE=E4=B8=AD?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=95=B0=E6=8D=AE=E6=BA=90=E8=87=AA=E6=9C=89?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/App.ts | 11 ++++++----- packages/core/src/Node.ts | 20 +++----------------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/packages/core/src/App.ts b/packages/core/src/App.ts index f7722625..53d46d11 100644 --- a/packages/core/src/App.ts +++ b/packages/core/src/App.ts @@ -259,15 +259,15 @@ class App extends EventEmitter { * @param eventConfig 代码动作的配置 * @returns void */ - public async runCode(codeId: Id, params: Record, args: any[], flowState?: FlowState) { + public async runCode(codeId: Id, params: Record, args: any[], flowState?: FlowState, node?: Node) { if (!codeId || isEmpty(this.codeDsl)) return; const content = this.codeDsl?.[codeId]?.content; if (typeof content === 'function') { try { - await content({ app: this, params, eventParams: args, flowState }); + await content({ app: this, params, eventParams: args, flowState, node }); } catch (e: any) { if (this.errorHandler) { - this.errorHandler(e, undefined, { type: 'run-code', codeId, params, eventParams: args, flowState }); + this.errorHandler(e, undefined, { type: 'run-code', codeId, params, eventParams: args, flowState, node }); } else { throw e; } @@ -281,6 +281,7 @@ class App extends EventEmitter { params: Record, args: any[], flowState?: FlowState, + node?: Node, ) { if (!dsId || !methodName) return; @@ -293,13 +294,13 @@ class App extends EventEmitter { const method = methods.find((item) => item.name === methodName); if (method && typeof method.content === 'function') { - await method.content({ app: this, params, dataSource, eventParams: args, flowState }); + await method.content({ app: this, params, dataSource, eventParams: args, flowState, node }); } else if (typeof dataSource[methodName] === 'function') { await dataSource[methodName](); } } catch (e: any) { if (this.errorHandler) { - this.errorHandler(e, dataSource, { type: 'data-source-method', params, eventParams: args, flowState }); + this.errorHandler(e, dataSource, { type: 'data-source-method', params, eventParams: args, flowState, node }); } else { throw e; } diff --git a/packages/core/src/Node.ts b/packages/core/src/Node.ts index fa326e38..942ef1b0 100644 --- a/packages/core/src/Node.ts +++ b/packages/core/src/Node.ts @@ -18,7 +18,6 @@ import { EventEmitter } from 'events'; -import { DataSource } from '@tmagic/data-source'; import type { EventConfig, MNode } from '@tmagic/schema'; import { HookCodeType, HookType, NODE_DISABLE_CODE_BLOCK_KEY } from '@tmagic/schema'; @@ -141,23 +140,10 @@ class Node extends EventEmitter { for (const item of hookData.hookData) { const { codeType = HookCodeType.CODE, codeId, params: itemParams = {} } = item; - let functionContent: ((...args: any[]) => any) | string | undefined; - const functionParams: { app: TMagicApp; node: Node; params: Record; dataSource?: DataSource } = { - app: this.app, - node: this, - params: params || itemParams, - }; - - if (codeType === HookCodeType.CODE && typeof codeId === 'string' && this.app.codeDsl?.[codeId]) { - functionContent = this.app.codeDsl[codeId].content; + if (codeType === HookCodeType.CODE && typeof codeId === 'string') { + await this.app.runCode(codeId, params || itemParams, [], undefined, this); } else if (codeType === HookCodeType.DATA_SOURCE_METHOD && Array.isArray(codeId) && codeId.length > 1) { - const dataSource = this.app.dataSourceManager?.get(codeId[0]); - functionContent = dataSource?.methods.find((method) => method.name === codeId[1])?.content; - functionParams.dataSource = dataSource; - } - - if (functionContent && typeof functionContent === 'function') { - await functionContent(functionParams); + await this.app.runDataSourceMethod(codeId[0], codeId[1], params || itemParams, [], undefined, this); } } }