@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
183 lines (182 loc) • 6.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PluginManager = void 0;
const LazyUtil_1 = require("../../utils/LazyUtil");
/**
* Class representing a manager for handling plugins.
*/
class PluginManager {
/**
* A map storing installed plugins with their names as keys.
*/
plugins;
/**
* Whether debugging is enabled.
*/
debug;
/**
* Reference to the LazyCanvas instance.
*/
canvas;
/**
* Constructs a new PluginManager instance.
* @param {LazyCanvas} [canvas] - The LazyCanvas instance.
* @param {Object} [opts] - Optional settings for the PluginManager.
* @param {boolean} [opts.debug] - Whether debugging is enabled.
*/
constructor(canvas, opts) {
this.plugins = new Map();
this.debug = opts?.debug || false;
this.canvas = canvas;
}
/**
* Registers a plugin.
* @param {ILazyCanvasPlugin} [plugin] - The plugin to register.
* @throws {LazyError} If a plugin with the same name is already registered.
*/
register(plugin) {
if (this.plugins.has(plugin.name)) {
throw new LazyUtil_1.LazyError(`Plugin '${plugin.name}' is already registered`);
}
// Check dependencies
if (plugin.dependencies) {
for (const dependency of plugin.dependencies) {
if (!this.plugins.has(dependency)) {
throw new LazyUtil_1.LazyError(`Plugin '${plugin.name}' requires dependency '${dependency}' which is not installed`);
}
}
}
try {
const result = plugin.install(this.canvas);
this.plugins.set(plugin.name, plugin);
if (this.debug) {
LazyUtil_1.LazyLog.log('info', `Plugin '${plugin.name}' v${plugin.version} registered successfully`);
}
}
catch (error) {
throw new LazyUtil_1.LazyError(`Failed to install plugin '${plugin.name}': ${error}`);
}
}
/**
* Unregisters a plugin by name.
* @param {string} [pluginName] - The name of the plugin to unregister.
* @throws {LazyError} If the plugin is not found or if other plugins depend on it.
*/
unregister(pluginName) {
const plugin = this.plugins.get(pluginName);
if (!plugin) {
throw new LazyUtil_1.LazyError(`Plugin '${pluginName}' is not registered`);
}
// Check if other plugins depend on this one
this.plugins.forEach((p, name) => {
if (name !== pluginName && p.dependencies?.includes(pluginName)) {
throw new LazyUtil_1.LazyError(`Cannot unregister plugin '${pluginName}' because plugin '${name}' depends on it`);
}
});
try {
if (plugin.uninstall) {
plugin.uninstall(this.canvas);
}
this.plugins.delete(pluginName);
if (this.debug) {
LazyUtil_1.LazyLog.log('info', `Plugin '${pluginName}' unregistered successfully`);
}
}
catch (error) {
throw new LazyUtil_1.LazyError(`Failed to uninstall plugin '${pluginName}': ${error}`);
}
}
/**
* Gets a plugin by name.
* @param {string} [pluginName] - The name of the plugin.
* @returns The plugin or undefined if not found.
*/
get(pluginName) {
return this.plugins.get(pluginName);
}
/**
* Lists all registered plugin names.
* @returns Array of plugin names.
*/
list() {
return Array.from(this.plugins.keys());
}
/**
* Checks if a plugin is registered.
* @param {string} [pluginName] - The name of the plugin.
* @returns True if the plugin is registered, false otherwise.
*/
has(pluginName) {
return this.plugins.has(pluginName);
}
/**
* Executes a hook for all plugins that implement it.
* @param {keyof IPluginHooks} [hookName] - The name of the hook to execute.
* @param {any} [args] - Arguments to pass to the hook.
*/
executeHook(hookName, ...args) {
this.plugins.forEach(plugin => {
try {
const hook = plugin.hooks?.[hookName];
if (hook) {
hook(...args);
}
}
catch (error) {
if (this.debug) {
LazyUtil_1.LazyLog.log('error', `Error executing hook '${hookName}' for plugin '${plugin.name}': ${error}`);
}
// Execute onError hook for all plugins when a hook fails
this.executeErrorHook(error);
}
});
}
/**
* Executes the onError hook for all plugins when an error occurs.
* @param error - The error that occurred.
*/
executeErrorHook(error) {
this.plugins.forEach(plugin => {
try {
const errorHook = plugin.hooks?.onError;
if (errorHook) {
errorHook(this.canvas, error);
}
}
catch (hookError) {
if (this.debug) {
LazyUtil_1.LazyLog.log('error', `Error in onError hook for plugin '${plugin.name}': ${hookError}`);
}
}
});
}
/**
* Gets plugin information.
* @returns Array of plugin information objects.
*/
getPluginInfo() {
return Array.from(this.plugins.values()).map(plugin => ({
name: plugin.name,
version: plugin.version,
description: plugin.description,
dependencies: plugin.dependencies
}));
}
/**
* Clears all plugins.
*/
clear() {
const pluginNames = this.list();
for (const name of pluginNames) {
try {
this.unregister(name);
}
catch (error) {
if (this.debug) {
LazyUtil_1.LazyLog.log('error', `Error unregistering plugin '${name}': ${error}`);
}
}
}
}
}
exports.PluginManager = PluginManager;