fix: fork model

This commit is contained in:
chenos 2025-06-19 13:54:45 +08:00
parent 4d0ebc10a4
commit aa4ea33698

View File

@ -7,11 +7,11 @@
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import { action, define, observable } from '@formily/reactive';
import type { IModelComponentProps } from '../types';
import { FlowModel } from './flowModel';
import { action, define, observable } from '@formily/reactive';
import type { IModelComponentProps } from '../types';
import { FlowModel } from './flowModel';
/**
/**
* ForkFlowModel FlowModel
* - master FlowModel
* - props (localProps) master
@ -20,7 +20,7 @@
* - setter this fork
* - FlowEngine.modelInstances uid master
*/
export class ForkFlowModel<TMaster extends FlowModel = FlowModel> {
export class ForkFlowModel<TMaster extends FlowModel = FlowModel> {
/** 与 master 相同的 UID用于日志调试 */
public readonly uid: string;
/** 调试标识,便于在日志或断言中快速识别 */
@ -38,6 +38,9 @@
/** fork 在 master.forks 中的索引 */
public readonly forkId: number;
/** 用于共享上下文的对象,存储跨 fork 的共享数据 */
private _sharedContext: Record<string, any> = {};
constructor(master: TMaster, initialProps: IModelComponentProps = {}, forkId = 0) {
this.master = master;
this.uid = master.uid;
@ -117,6 +120,20 @@
});
}
public setSharedContext(ctx: Record<string, any>) {
this._sharedContext = ctx;
}
public getSharedContext() {
if (this.async || !this.parent) {
return this._sharedContext;
}
return {
...this.parent?.getSharedContext(),
...this._sharedContext, // 当前实例的 context 优先级最高
};
}
/**
*
*/
@ -191,7 +208,8 @@
getProps(): IModelComponentProps {
return { ...this.master.getProps(), ...this.localProps };
}
}
}
// 类型断言:让 ForkFlowModel 可以被当作 FlowModel 使用
export interface ForkFlowModel<TMaster extends FlowModel = FlowModel> extends FlowModel {}
// 类型断言:让 ForkFlowModel 可以被当作 FlowModel 使用
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ForkFlowModel<TMaster extends FlowModel = FlowModel> extends FlowModel {}