chore: registry注册到全局里面

This commit is contained in:
xiaojunnuo
2024-11-16 11:01:14 +08:00
parent 18bfcc24ad
commit c6488b58f5
6 changed files with 80 additions and 11 deletions
@@ -1,4 +1,4 @@
import { Registry } from "../registry/index.js";
import { createRegistry } from "../registry/index.js";
// @ts-ignore
export const accessRegistry = new Registry("access");
export const accessRegistry = createRegistry("access");
@@ -1,4 +1,4 @@
import { OnRegisterContext, Registry } from "../registry/index.js";
import { createRegistry, OnRegisterContext } from "../registry/index.js";
import { AbstractTaskPlugin } from "./api.js";
import { pluginGroups } from "./group.js";
@@ -13,4 +13,4 @@ const onRegister = ({ key, value }: OnRegisterContext<AbstractTaskPlugin>) => {
}
}
};
export const pluginRegistry = new Registry<AbstractTaskPlugin>("plugin", onRegister);
export const pluginRegistry = createRegistry<AbstractTaskPlugin>("plugin", onRegister);
@@ -88,3 +88,21 @@ export class Registry<T> {
return item.define;
}
}
export function createRegistry<T>(type: string, onRegister?: OnRegister<T>) {
const pipelineregistrycacheKey = "PIPELINE_REGISTRY_CACHE";
// @ts-ignore
let cached: any = global[pipelineregistrycacheKey];
if (!cached) {
cached = {};
// @ts-ignore
global[pipelineregistrycacheKey] = cached;
}
if (cached[type]) {
return cached[type];
}
const newRegistry = new Registry<T>(type, onRegister);
cached[type] = newRegistry;
return newRegistry;
}