Files
certd/packages/core/pipeline/src/registry/registry.ts
T

66 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-05-30 10:12:48 +08:00
import { logger } from "../utils";
2022-10-26 09:02:47 +08:00
export type Registrable = {
name: string;
title: string;
desc?: string;
};
2023-05-24 15:41:35 +08:00
export type RegistryItem<T> = {
2022-12-27 12:32:09 +08:00
define: Registrable;
2023-05-24 15:41:35 +08:00
target: T;
2022-12-27 12:32:09 +08:00
};
2023-05-24 15:41:35 +08:00
export class Registry<T> {
2024-05-30 10:12:48 +08:00
type = "";
2022-10-26 09:02:47 +08:00
storage: {
2023-05-24 15:41:35 +08:00
[key: string]: RegistryItem<T>;
2022-10-26 09:02:47 +08:00
} = {};
2024-05-30 10:12:48 +08:00
constructor(type: string) {
this.type = type;
}
2023-05-24 15:41:35 +08:00
register(key: string, value: RegistryItem<T>) {
2022-10-26 09:02:47 +08:00
if (!key || value == null) {
return;
}
this.storage[key] = value;
2024-05-30 10:12:48 +08:00
logger.info(`注册插件:${this.type}:${key}`);
2022-10-26 09:02:47 +08:00
}
2023-05-24 15:41:35 +08:00
get(name: string): RegistryItem<T> {
2022-10-26 09:02:47 +08:00
if (!name) {
throw new Error("插件名称不能为空");
}
const plugin = this.storage[name];
if (!plugin) {
throw new Error(`插件${name}还未注册`);
}
return plugin;
}
getStorage() {
return this.storage;
}
2022-10-28 16:08:12 +08:00
getDefineList() {
const list = [];
for (const key in this.storage) {
2022-10-31 21:27:32 +08:00
const define = this.getDefine(key);
if (define) {
list.push({ ...define, key });
}
2022-10-28 16:08:12 +08:00
}
return list;
}
2022-10-31 21:27:32 +08:00
getDefine(key: string) {
2022-12-27 12:32:09 +08:00
const item = this.storage[key];
if (!item) {
2022-10-31 21:27:32 +08:00
return;
}
2022-12-27 12:32:09 +08:00
return item.define;
2022-10-31 21:27:32 +08:00
}
2022-10-26 09:02:47 +08:00
}