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

58 lines
1013 B
TypeScript
Raw Normal View History

2022-10-26 09:02:47 +08:00
export type Registrable = {
name: string;
title: string;
desc?: string;
};
2022-12-27 12:32:09 +08:00
export type RegistryItem = {
define: Registrable;
target: any;
};
export class Registry {
2022-10-26 09:02:47 +08:00
storage: {
2022-12-27 12:32:09 +08:00
[key: string]: RegistryItem;
2022-10-26 09:02:47 +08:00
} = {};
2022-12-27 12:32:09 +08:00
register(key: string, value: RegistryItem) {
2022-10-26 09:02:47 +08:00
if (!key || value == null) {
return;
}
this.storage[key] = value;
}
2023-01-11 20:39:48 +08:00
get(name: string): RegistryItem {
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
}