diff --git a/packages/core/flow-engine/src/flowEngine.ts b/packages/core/flow-engine/src/flowEngine.ts index f2e7352055..3d3bb1a39b 100644 --- a/packages/core/flow-engine/src/flowEngine.ts +++ b/packages/core/flow-engine/src/flowEngine.ts @@ -309,4 +309,13 @@ export class FlowEngine { } return modelClasses; } + + static generateApplyFlowCacheKey( + prefix: string, + flowKey: string, + modelUid: string, + params: Record, + ): string { + return `${prefix}:${flowKey}:${modelUid}:${JSON.stringify(params)}`; + } } diff --git a/packages/core/flow-engine/src/hooks/useApplyFlow.ts b/packages/core/flow-engine/src/hooks/useApplyFlow.ts index 0b42e4c681..585f9a194d 100644 --- a/packages/core/flow-engine/src/hooks/useApplyFlow.ts +++ b/packages/core/flow-engine/src/hooks/useApplyFlow.ts @@ -13,77 +13,7 @@ import { FlowModel } from '../models'; import { useFlowEngine } from '../provider'; import { FlowExtraContext } from '../types'; import { uid } from 'uid/secure'; - -// 生成稳定的缓存键 -function generateCacheKey(prefix: string, flowKey: string, modelUid: string): string { - return `${prefix}:${flowKey}:${modelUid}`; -} - -// 安全序列化对象的函数 -function safeStringify(obj: any, visited = new Set(), depth = 0, maxDepth = 5): string { - // 深度限制,防止过深递归 - if (depth > maxDepth) { - return '[MaxDepthExceeded]'; - } - - // 处理基本类型 - if (obj === null || obj === undefined) { - return String(obj); - } - - if (typeof obj !== 'object' && typeof obj !== 'function') { - return String(obj); - } - - // 处理函数 - if (typeof obj === 'function') { - // 对于函数,可以使用函数名或为匿名函数生成一个标识符 - return `function:${obj.name || 'anonymous'}`; - } - - // 处理循环引用 - if (visited.has(obj)) { - return '[Circular]'; - } - - // 添加到已访问集合 - visited.add(obj); - - // 处理数组 - if (Array.isArray(obj)) { - try { - // 限制处理的数组元素数量 - const maxItems = 100; - const items = obj.slice(0, maxItems); - const result = - '[' + - items.map((item) => safeStringify(item, visited, depth + 1, maxDepth)).join(',') + - (obj.length > maxItems ? ',...' : '') + - ']'; - return result; - } catch (e) { - return '[Array]'; - } - } - - // 处理普通对象 - try { - const keys = Object.keys(obj).sort(); // 排序确保稳定性 - // 限制处理的属性数量 - const maxKeys = 50; - const limitedKeys = keys.slice(0, maxKeys); - - const pairs = limitedKeys.map((key) => { - const value = obj[key]; - return `${key}:${safeStringify(value, visited, depth + 1, maxDepth)}`; - }); - - return '{' + pairs.join(',') + (keys.length > maxKeys ? ',...' : '') + '}'; - } catch (e) { - // 如果无法序列化,返回一个简单的标识 - return '[Object]'; - } -} +import { FlowEngine } from '../flowEngine'; /** * 通用的流程执行 Hook @@ -105,8 +35,8 @@ function useFlowExecutor( ): T { const engine = useFlowEngine(); const cacheKey = useMemo( - () => generateCacheKey(model['forkId'] ?? cacheKeyPrefix, flowKey, model.uid), - [cacheKeyPrefix, flowKey, model.uid, model['forkId']], + () => FlowEngine.generateApplyFlowCacheKey(model['forkId'] ?? cacheKeyPrefix, flowKey, model.uid, model.stepParams), + [cacheKeyPrefix, flowKey, model.uid, model['forkId'], model.stepParams], ); const [, forceUpdate] = useState({}); const isMounted = useRef(false); diff --git a/packages/core/flow-engine/src/models/flowModel.tsx b/packages/core/flow-engine/src/models/flowModel.tsx index b0aa0a1edf..e10a6520b8 100644 --- a/packages/core/flow-engine/src/models/flowModel.tsx +++ b/packages/core/flow-engine/src/models/flowModel.tsx @@ -325,6 +325,7 @@ export class FlowModel} 所有自动应用流程的执行结果数组 */ - async applyAutoFlows(extra?: FlowExtraContext): Promise { + async applyAutoFlows(extra?: FlowExtraContext, useCache = true): Promise { const autoApplyFlows = this.getAutoFlows(); if (autoApplyFlows.length === 0) { @@ -528,18 +530,65 @@ export class FlowModel => { + const results: any[] = []; + for (const flow of autoApplyFlows) { + try { + const result = await this.applyFlow(flow.key, extra); + results.push(result); + } catch (error) { + console.error(`FlowModel.applyAutoFlows: Error executing auto-apply flow '${flow.key}':`, error); + throw error; + } + } + return results; + }; + + // 如果不使用缓存,直接执行 + if (!cacheKey || !this.flowEngine) { + return await executeAutoFlows(); + } + + // 使用缓存机制 + const promise = executeAutoFlows() + .then((result) => { + this.flowEngine.applyFlowCache.set(cacheKey, { + status: 'resolved', + data: result, + promise: Promise.resolve(result), + }); + return result; + }) + .catch((err) => { + this.flowEngine.applyFlowCache.set(cacheKey, { + status: 'rejected', + error: err, + promise: Promise.reject(err), + }); + throw err; + }); + + this.flowEngine.applyFlowCache.set(cacheKey, { status: 'pending', promise }); + return await promise; } /**