import { isDev, logger } from "../utils/index.js"; export type Registrable = { name: string; title: string; desc?: string; group?: string; deprecated?: string; }; export type RegistryItem = { define: Registrable; target: T; }; export type OnRegisterContext = { registry: Registry; key: string; value: RegistryItem; }; export type OnRegister = (ctx: OnRegisterContext) => void; export class Registry { type = ""; storage: { [key: string]: RegistryItem; } = {}; onRegister?: OnRegister; constructor(type: string, onRegister?: OnRegister) { this.type = type; this.onRegister = onRegister; } register(key: string, value: RegistryItem) { if (!key || value == null) { return; } this.storage[key] = value; if (this.onRegister) { this.onRegister({ registry: this, key, value, }); } logger.info(`注册插件:${this.type}:${key}`); } get(name: string): RegistryItem { if (!name) { throw new Error("插件名称不能为空"); } const plugin = this.storage[name]; if (!plugin) { throw new Error(`插件${name}还未注册`); } return plugin; } getStorage() { return this.storage; } getDefineList() { const list = []; for (const key in this.storage) { const define = this.getDefine(key); if (define) { if (define?.deprecated) { continue; } if (!isDev() && define.name.startsWith("demo")) { continue; } list.push({ ...define, key }); } } return list; } getDefine(key: string) { const item = this.storage[key]; if (!item) { return; } return item.define; } }