mirror of
https://github.com/certd/certd.git
synced 2026-04-28 07:57:25 +08:00
fix: 修复插件修改名字和删除后没有注销注册的bug
This commit is contained in:
@@ -22,4 +22,15 @@ const onRegister = ({ key, value }: OnRegisterContext<AbstractTaskPlugin>) => {
|
|||||||
}
|
}
|
||||||
pluginGroups.other.plugins.push(value.define);
|
pluginGroups.other.plugins.push(value.define);
|
||||||
};
|
};
|
||||||
export const pluginRegistry = createRegistry<AbstractTaskPlugin>("plugin", onRegister);
|
|
||||||
|
const onUnRegister = ({ key }: OnRegisterContext<AbstractTaskPlugin>) => {
|
||||||
|
for (const group of Object.values(pluginGroups)) {
|
||||||
|
const index = group.plugins.findIndex(plugin => plugin.name === key);
|
||||||
|
if (index > -1) {
|
||||||
|
group.plugins.splice(index, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const pluginRegistry = createRegistry<AbstractTaskPlugin>("plugin", onRegister, onUnRegister);
|
||||||
|
|||||||
@@ -27,10 +27,12 @@ export class Registry<T = any> {
|
|||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
onRegister?: OnRegister<T>;
|
onRegister?: OnRegister<T>;
|
||||||
|
onUnRegister?: OnRegister<T>;
|
||||||
|
|
||||||
constructor(type: string, onRegister?: OnRegister<T>) {
|
constructor(type: string, onRegister?: OnRegister<T>, onUnRegister?: OnRegister<T>) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.onRegister = onRegister;
|
this.onRegister = onRegister;
|
||||||
|
this.onUnRegister = onUnRegister;
|
||||||
}
|
}
|
||||||
|
|
||||||
register(key: string, value: RegistryItem<T>) {
|
register(key: string, value: RegistryItem<T>) {
|
||||||
@@ -49,6 +51,13 @@ export class Registry<T = any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unRegister(key: string) {
|
unRegister(key: string) {
|
||||||
|
if (this.onUnRegister) {
|
||||||
|
this.onUnRegister({
|
||||||
|
registry: this,
|
||||||
|
key,
|
||||||
|
value: this.storage[key],
|
||||||
|
});
|
||||||
|
}
|
||||||
delete this.storage[key];
|
delete this.storage[key];
|
||||||
logger.info(`反注册插件:${this.type}:${key}`);
|
logger.info(`反注册插件:${this.type}:${key}`);
|
||||||
}
|
}
|
||||||
@@ -108,7 +117,7 @@ export class Registry<T = any> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createRegistry<T>(type: string, onRegister?: OnRegister<T>): Registry<T> {
|
export function createRegistry<T>(type: string, onRegister?: OnRegister<T>, onUnRegister?: OnRegister<T>): Registry<T> {
|
||||||
const pipelineregistrycacheKey = "PIPELINE_REGISTRY_CACHE";
|
const pipelineregistrycacheKey = "PIPELINE_REGISTRY_CACHE";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
let cached: any = global[pipelineregistrycacheKey];
|
let cached: any = global[pipelineregistrycacheKey];
|
||||||
@@ -121,7 +130,7 @@ export function createRegistry<T>(type: string, onRegister?: OnRegister<T>): Reg
|
|||||||
if (cached[type]) {
|
if (cached[type]) {
|
||||||
return cached[type];
|
return cached[type];
|
||||||
}
|
}
|
||||||
const newRegistry = new Registry<T>(type, onRegister);
|
const newRegistry = new Registry<T>(type, onRegister, onUnRegister);
|
||||||
cached[type] = newRegistry;
|
cached[type] = newRegistry;
|
||||||
return newRegistry;
|
return newRegistry;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -305,7 +305,7 @@ export class PluginService extends BaseService<PluginEntity> {
|
|||||||
throw new Error(`插件${param.author}/${param.name}已存在`);
|
throw new Error(`插件${param.author}/${param.name}已存在`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await this.unRegisterById(param.id);
|
||||||
const res = await super.update(param);
|
const res = await super.update(param);
|
||||||
|
|
||||||
await this.registerById(param.id);
|
await this.registerById(param.id);
|
||||||
@@ -413,7 +413,7 @@ export class PluginService extends BaseService<PluginEntity> {
|
|||||||
delete item.metadata;
|
delete item.metadata;
|
||||||
delete item.content;
|
delete item.content;
|
||||||
delete item.extra;
|
delete item.extra;
|
||||||
if (item.author) {
|
if (item.author && !item.name.startsWith(`${item.author}/`)) {
|
||||||
item.name = item.author + "/" + item.name;
|
item.name = item.author + "/" + item.name;
|
||||||
}
|
}
|
||||||
let name = item.name
|
let name = item.name
|
||||||
@@ -527,10 +527,11 @@ export class PluginService extends BaseService<PluginEntity> {
|
|||||||
|
|
||||||
|
|
||||||
async deleteByIds(ids: any[]) {
|
async deleteByIds(ids: any[]) {
|
||||||
await super.delete(ids);
|
|
||||||
for (const id of ids) {
|
for (const id of ids) {
|
||||||
await this.unRegisterById(id)
|
await this.unRegisterById(id)
|
||||||
|
await this.delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user