/** * This file is part of the NocoBase (R) project. * Copyright (c) 2020-2024 NocoBase Co., Ltd. * Authors: NocoBase Team. * * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. * For more information, please refer to: https://www.nocobase.com/agreement. */ /** * 模型类级动作注册表(每个 FlowModel 子类一个)。 * - 仅维护“当前类自有”的动作; * - 通过父类注册表实现继承合并,并在本类内部做缓存; * - 内部有查询缓存,避免重复计算。 */ import type { ActionDefinition } from '../types'; import type { FlowModel } from '../models'; import type { FlowContext } from '../flowContext'; import { BaseActionRegistry } from './BaseActionRegistry'; export class ModelActionRegistry extends BaseActionRegistry { private readonly modelClass: typeof FlowModel; private readonly parentRegistry: ModelActionRegistry | null; // 本类变更标记(变更即更新引用,用于缓存命中判断) private changeMarker: object = {}; // 合并缓存:父类合并快照引用 + 本类标记 + 合并后的 Map private mergedCache?: { parentSnapshot?: Map; localMarker: object; mergedMap: Map; }; constructor(modelClass: typeof FlowModel, parentRegistry?: ModelActionRegistry | null) { super(); this.modelClass = modelClass; this.parentRegistry = parentRegistry ?? null; } // 标记本类有变更(用于缓存命中判断) onActionRegistered() { this.changeMarker = {}; } /** * 获取“包含继承”的合并动作(父 → 子),内部带缓存。 */ getActions(): Map< string, ActionDefinition > { const parentMap = this.parentRegistry?.getActions(); if ( this.mergedCache && this.mergedCache.parentSnapshot === parentMap && this.mergedCache.localMarker === this.changeMarker ) { return this.mergedCache.mergedMap as Map>; } const mergedMap = new Map(parentMap ?? []); for (const [k, v] of this.actions) mergedMap.set(k, v); this.mergedCache = { parentSnapshot: parentMap, localMarker: this.changeMarker, mergedMap }; return mergedMap as Map>; } /** * 解析指定名称的动作(优先本类,未命中递归父类)。 */ getAction( name: string, ): ActionDefinition | undefined { const own = this.actions.get(name); if (own) return own as ActionDefinition; return this.parentRegistry?.getAction(name) as ActionDefinition | undefined; } }