@flowlab/all
Version:
A cool library focusing on handling various flows
54 lines (47 loc) • 1.49 kB
text/typescript
// --- src/core/node-registry.ts ---
import { INode } from '../types/index.js';
/**
* @class NodeRegistry
* @description 节点注册表,用于管理所有可用的自定义任务节点
*/
export class NodeRegistry {
private nodes: Map<string, INode> = new Map();
/**
* 注册一个节点
* @param node - 要注册的节点实例
* @throws 如果节点 ID 已存在,则抛出错误
*/
register(node: INode): void {
if (this.nodes.has(node.id)) {
console.warn(`[NodeRegistry] 节点 ID "${node.id}" 已存在,将被覆盖。`);
// 或者可以抛出错误: throw new Error(`Node with id "${node.id}" already registered.`);
}
this.nodes.set(node.id, node);
console.log(`[NodeRegistry] 节点 "${node.id}" 已注册。`);
}
/**
* 根据 ID 获取节点
* @param nodeId - 节点 ID
* @returns 返回节点实例,如果未找到则返回 undefined
*/
get(nodeId: string): INode | undefined {
return this.nodes.get(nodeId);
}
/**
* 检查节点是否存在
* @param nodeId - 节点 ID
* @returns 如果节点存在则返回 true,否则返回 false
*/
has(nodeId: string): boolean {
return this.nodes.has(nodeId);
}
/**
* 获取所有已注册节点的 ID 列表
* @returns 节点 ID 数组
*/
listNodeIds(): string[] {
return Array.from(this.nodes.keys());
}
}
// 创建一个全局或可注入的节点注册表实例
export const nodeRegistry = new NodeRegistry();