kist
Version:
Lightweight Package Pipeline Processor with Plugin Architecture
83 lines • 3.07 kB
JavaScript
import { coreActions } from "../../actions/CoreActions.js";
import { AbstractProcess } from "../abstract/AbstractProcess.js";
import { PluginManager } from "../plugin/PluginManager.js";
export class ActionRegistry extends AbstractProcess {
constructor() {
super();
this.registry = new Map();
this.registerCoreActions();
this.registerPluginActions();
this.logInfo("ActionRegistry initialized.");
}
static initialize() {
if (ActionRegistry.instance) {
throw new Error("ActionRegistry has already been initialized.");
}
ActionRegistry.instance = new ActionRegistry();
}
static getInstance() {
if (!ActionRegistry.instance) {
ActionRegistry.instance = new ActionRegistry();
}
return ActionRegistry.instance;
}
static resetInstance() {
ActionRegistry.instance = null;
}
registerAction(actionClass) {
const actionInstance = new actionClass();
const name = actionInstance.name;
if (!name || typeof name !== "string") {
throw new Error(`[ActionRegistry] Action class must have a valid 'name' property.`);
}
if (this.registry.has(name)) {
throw new Error(`[ActionRegistry] Action "${name}" is already registered.`);
}
this.registry.set(name, actionClass);
this.logInfo(`Action "${name}" registered successfully.`);
}
getAction(name) {
if (!name || typeof name !== "string") {
this.logWarn(`Invalid action name requested: "${name}".`);
return undefined;
}
const action = this.registry.get(name);
if (!action) {
this.logWarn(`Action "${name}" not found in the registry.`);
}
else {
this.logDebug(`Retrieved action "${name}" from the registry.`);
}
return action;
}
listRegisteredActions() {
this.logDebug("Listing all registered actions.");
return Array.from(this.registry.keys());
}
registerCoreActions() {
Object.values(coreActions).forEach((actionClass) => {
this.registerAction(actionClass);
});
this.logInfo("Core actions registered successfully.");
}
registerPluginActions() {
const pluginManager = PluginManager.getInstance();
const pluginActions = pluginManager.getPluginActions();
for (const [actionName, actionClass] of pluginActions.entries()) {
try {
this.registerAction(actionClass);
this.logDebug(`Registered plugin action: ${actionName}`);
}
catch (error) {
this.logError(`Failed to register plugin action ${actionName}:`, error);
}
}
this.logInfo(`Registered ${pluginActions.size} actions from plugins.`);
}
clearRegistry() {
this.registry.clear();
this.logInfo("Registry cleared.");
}
}
ActionRegistry.instance = null;
//# sourceMappingURL=ActionRegistry.js.map