alpha-module-manager
v1.0.36
Published
The Alpha Plugins Manager is a JavaScript utility for managing plugins in a web application. It consists of three main classes: Logger, PluginsRegistry, and AlphaPluginsManager, each serving a specific purpose.
Readme
ALPHA PLUGINS MANAGAR
The Alpha Plugins Manager is a JavaScript utility for managing plugins in a web application. It consists of three main classes: Logger, PluginsRegistry, and AlphaPluginsManager, each serving a specific purpose.
HOW TO USE
Initialization
A variable alpha is initialized globally, which has all the essential functions for the Alpha Plugins Manager, Logger and PluginsRegistry.
Registering a Plugin
Use the registerPlugins method to register a plugin with the Alpha Plugins Manager.
const myPlugin = {
// ...plugin properties and methods...
onRegister: () => {
// Optional callback when the plugin is registered
console.log('My plugin has been registered!');
},
};
// Register the plugin with a unique name
alpha.registerPlugins('myPlugin', myPlugin);Unregistering a Plugin
Use the deregisterPlugin method to unregister a previously registered plugin.
// Unregister the previously registered plugin by name
alpha.deregisterPlugin('myPlugin');Subscribing to Events
Subscribe to application event lifecycle hooks using the on method.
// Subscribe to a custom event
const unsubscribe = alpha.on('customEvent', data => {
console.log('Custom event triggered with data:', data);
});
// Later, to unsubscribe
unsubscribe();Triggering Events
Trigger application events using the trigger method.
// Trigger a custom event with data
alpha.trigger('customEvent', { key: 'value' });Unsubscribing from Events
Use the off method to unsubscribe from an application event lifecycle hook.
// Define a callback function
const customEventHandler = data => {
console.log('Custom event triggered with data:', data);
};
// Subscribe to a custom event
const unsubscribe = alpha.on('customEvent', customEventHandler);
// Later, to unsubscribe and stop receiving events
alpha.off('customEvent', customEventHandler);Accessing Registered Plugins
Retrieve the list of registered plugins using the plugins property.
// Get the list of registered plugins
const registeredPlugins = alpha.plugins;
console.log('Registered Plugins:', registeredPlugins);Accessing Functions of a Plugin
const myPlugin = {
// ...plugin properties and methods...
onRegister: () => {
// Optional callback when the plugin is registered
console.log('My plugin has been registered!');
},
};
// Accessing plugin functions
alpha.plugins.myPlugin.onRegister();BASE-SERVICE DOCUMENTATION
Logger Class
The Logger class provides a versatile logging mechanism with customizable styles. It supports both logging and error messages, playing a crucial role in providing informative output throughout the plugin manager.
Methods
- constructor(label = "[Logger]") Description: Initializes a new Logger instance with an optional label. Parameters: label (optional): Custom label for the logger. Default is "[Logger]".
- log(...args) Description: Logs messages to the console with the specified label and style. Parameters: args: Any number of arguments to be logged.
- error(...args) Description: Logs error messages to the console with the specified label and style. Parameters: args: Any number of arguments to be logged as an error.
PluginsRegistry Class
The PluginsRegistry class acts as a registry for managing plugins. It allows the addition, removal, and retrieval of plugins, providing error handling for scenarios such as duplicate or nonexistent plugins.
Methods
- add(name, instance) Description: Adds a new plugin to the registry. Parameters: name: Unique name of the plugin. instance: The plugin instance to be added. Returns: true if the addition is successful.
- remove(name) Description: Removes a plugin from the registry. Parameters: name: Unique name of the plugin to be removed. Returns: The removed plugin instance if successful.
- get(name) Description: Retrieves a plugin from the registry. Parameters: name: Unique name of the plugin to be retrieved. Returns: The plugin instance if found.
AlphaPluginsManager Class
The AlphaPluginsManager class orchestrates the registration and unregistration of plugins. Additionally, it provides an event system for communication between different parts of the application and dispatches an "alpha_ready" event when the application is ready.
Methods
constructor(logger, registry) Initializes a new AlphaPluginsManager instance with a logger and a plugin registry. Parameters: logger: An instance of the Logger class for logging messages. registry: An instance of the PluginsRegistry class for managing plugins.
onReady(callback) Registers a callback to be executed when the application is ready. Parameters: callback: The function to be executed on readiness.
registerPlugins(name, instance) Registers a plugin with the AlphaPluginsManager. Parameters: name: Unique name of the plugin. instance: The plugin instance to be registered.
deregisterPlugin(name) Unregisters a plugin from the AlphaPluginsManager. Parameters: name: Unique name of the plugin to be unregistered.
on(eventName, fn) Subscribes to an App event lifecycle hook. Parameters: eventName: Unique name of the event. fn: The function to be executed on the event. Returns: A function for unsubscribing from the event.
trigger(eventName, data) Publishes/triggers an App event lifecycle hook. Parameters: eventName: Unique name of the event. data: Data to be passed to the event handlers.
off(eventName, fn) Unsubscribes from an App event lifecycle hook. Parameters: eventName: Unique name of the event. fn: The function to be unsubscribed.
get plugins() Retrieves the list of registered plugins.
Initialization
The provided code initializes the Logger, PluginsRegistry, and AlphaPluginsManager instances. It also dispatches an "alpha_ready" event after a short delay to signal that the application is ready.
const logger = new Logger('[AlphaPluginsManager]');
const registry = new PluginsRegistry(logger);
const alpha = new AlphaPluginsManager(logger, registry);
// Simulating the readiness of the application
setTimeout(() => {
const alphaReadyEvent = new Event('alpha_ready');
window.dispatchEvent(alphaReadyEvent);
}, 0);