mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-07-01 18:52:20 +08:00
feat: improve rerun auto flows
This commit is contained in:
parent
b643c9a330
commit
a8f4ce8d70
@ -64,6 +64,11 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
|
|||||||
*/
|
*/
|
||||||
private _sharedContext: Record<string, any> = {};
|
private _sharedContext: Record<string, any> = {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上一次 applyAutoFlows 的执行参数
|
||||||
|
*/
|
||||||
|
private _lastAutoRunParams: any[] | null = null;
|
||||||
|
|
||||||
constructor(options: FlowModelOptions<Structure>) {
|
constructor(options: FlowModelOptions<Structure>) {
|
||||||
if (options?.flowEngine?.getModel(options.uid)) {
|
if (options?.flowEngine?.getModel(options.uid)) {
|
||||||
// 此时 new FlowModel 并不创建新实例,而是返回已存在的实例,避免重复创建同一个model实例
|
// 此时 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;
|
getStepParams(flowKey: string, stepKey: string): any | undefined;
|
||||||
@ -516,13 +521,34 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
|
|||||||
return autoFlows;
|
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 {FlowExtraContext} [extra] 可选的额外上下文
|
||||||
* @param {boolean} [useCache=true] 是否使用缓存机制,默认为 true
|
* @param {boolean} [useCache=true] 是否使用缓存机制,默认为 true
|
||||||
* @returns {Promise<any[]>} 所有自动应用流程的执行结果数组
|
* @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();
|
const autoApplyFlows = this.getAutoFlows();
|
||||||
|
|
||||||
if (autoApplyFlows.length === 0) {
|
if (autoApplyFlows.length === 0) {
|
||||||
@ -687,9 +713,9 @@ export class FlowModel<Structure extends { parent?: any; subModels?: any } = Def
|
|||||||
shared?: Record<string, any>,
|
shared?: Record<string, any>,
|
||||||
) {
|
) {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
this.mapSubModels(subKey, async (column) => {
|
this.mapSubModels(subKey, async (sub) => {
|
||||||
column.setSharedContext(shared);
|
sub.setSharedContext(shared);
|
||||||
await column.applyAutoFlows(extra);
|
await sub.applyAutoFlows(extra);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user