fix: 组件声明周期函数配置中配置数据源自有方法生效

This commit is contained in:
roymondchen 2025-09-10 16:24:44 +08:00
parent 958c78d968
commit e400175ffe
2 changed files with 9 additions and 22 deletions

View File

@ -259,15 +259,15 @@ class App extends EventEmitter {
* @param eventConfig * @param eventConfig
* @returns void * @returns void
*/ */
public async runCode(codeId: Id, params: Record<string, any>, args: any[], flowState?: FlowState) { public async runCode(codeId: Id, params: Record<string, any>, args: any[], flowState?: FlowState, node?: Node) {
if (!codeId || isEmpty(this.codeDsl)) return; if (!codeId || isEmpty(this.codeDsl)) return;
const content = this.codeDsl?.[codeId]?.content; const content = this.codeDsl?.[codeId]?.content;
if (typeof content === 'function') { if (typeof content === 'function') {
try { try {
await content({ app: this, params, eventParams: args, flowState }); await content({ app: this, params, eventParams: args, flowState, node });
} catch (e: any) { } catch (e: any) {
if (this.errorHandler) { 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 { } else {
throw e; throw e;
} }
@ -281,6 +281,7 @@ class App extends EventEmitter {
params: Record<string, any>, params: Record<string, any>,
args: any[], args: any[],
flowState?: FlowState, flowState?: FlowState,
node?: Node,
) { ) {
if (!dsId || !methodName) return; if (!dsId || !methodName) return;
@ -293,13 +294,13 @@ class App extends EventEmitter {
const method = methods.find((item) => item.name === methodName); const method = methods.find((item) => item.name === methodName);
if (method && typeof method.content === 'function') { 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') { } else if (typeof dataSource[methodName] === 'function') {
await dataSource[methodName](); await dataSource[methodName]();
} }
} catch (e: any) { } catch (e: any) {
if (this.errorHandler) { 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 { } else {
throw e; throw e;
} }

View File

@ -18,7 +18,6 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import { DataSource } from '@tmagic/data-source';
import type { EventConfig, MNode } from '@tmagic/schema'; import type { EventConfig, MNode } from '@tmagic/schema';
import { HookCodeType, HookType, NODE_DISABLE_CODE_BLOCK_KEY } 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) { for (const item of hookData.hookData) {
const { codeType = HookCodeType.CODE, codeId, params: itemParams = {} } = item; const { codeType = HookCodeType.CODE, codeId, params: itemParams = {} } = item;
let functionContent: ((...args: any[]) => any) | string | undefined; if (codeType === HookCodeType.CODE && typeof codeId === 'string') {
const functionParams: { app: TMagicApp; node: Node; params: Record<string, any>; dataSource?: DataSource } = { await this.app.runCode(codeId, params || itemParams, [], undefined, this);
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;
} else if (codeType === HookCodeType.DATA_SOURCE_METHOD && Array.isArray(codeId) && codeId.length > 1) { } else if (codeType === HookCodeType.DATA_SOURCE_METHOD && Array.isArray(codeId) && codeId.length > 1) {
const dataSource = this.app.dataSourceManager?.get(codeId[0]); await this.app.runDataSourceMethod(codeId[0], codeId[1], params || itemParams, [], undefined, this);
functionContent = dataSource?.methods.find((method) => method.name === codeId[1])?.content;
functionParams.dataSource = dataSource;
}
if (functionContent && typeof functionContent === 'function') {
await functionContent(functionParams);
} }
} }
} }