feat: improve rerun auto flows

This commit is contained in:
gchust 2025-06-22 13:48:43 +08:00
parent b643c9a330
commit a8f4ce8d70

View File

@ -64,6 +64,11 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
*/
private _sharedContext: Record<string, any> = {};
/**
* applyAutoFlows
*/
private _lastAutoRunParams: any[] | null = null;
constructor(options: FlowModelOptions<Structure>) {
if (options?.flowEngine?.getModel(options.uid)) {
// 此时 new FlowModel 并不创建新实例而是返回已存在的实例避免重复创建同一个model实例
@ -325,7 +330,7 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
}
}
}
this.applyAutoFlows(); // 参数变化时自动重新执行 autoFlows
this._rerunLastAutoRun();
}
getStepParams(flowKey: string, stepKey: string): any | undefined;
@ -516,13 +521,34 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
return autoFlows;
}
/**
* applyAutoFlows
*
* 使 lodash debounce
*/
private _rerunLastAutoRun = _.debounce(async () => {
if (this._lastAutoRunParams) {
try {
await this.applyAutoFlows(...this._lastAutoRunParams);
} catch (error) {
console.error('FlowModel._rerunLastAutoRun: Error during rerun:', error);
}
}
}, 100);
/**
*
* @param {FlowExtraContext} [extra]
* @param {boolean} [useCache=true] 使 true
* @returns {Promise<any[]>}
*/
async applyAutoFlows(extra?: FlowExtraContext, useCache = true): Promise<any[]> {
async applyAutoFlows(extra?: FlowExtraContext, useCache?: boolean): Promise<any[]>;
async applyAutoFlows(...args: any[]): Promise<any[]> {
// 存储本次执行的参数,用于后续重新执行
this._lastAutoRunParams = args;
const [extra, useCache = true] = args;
const autoApplyFlows = this.getAutoFlows();
if (autoApplyFlows.length === 0) {
@ -687,9 +713,9 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
shared?: Record<string, any>,
) {
await Promise.all(
this.mapSubModels(subKey, async (column) => {
column.setSharedContext(shared);
await column.applyAutoFlows(extra);
this.mapSubModels(subKey, async (sub) => {
sub.setSharedContext(shared);
await sub.applyAutoFlows(extra);
}),
);
}