Files
Plugin-Example/index.js

86 lines
2.1 KiB
JavaScript

/**
* Example Plugin - Demonstrates plugin capabilities
*
* This plugin shows:
* - Activation/Deactivation lifecycle
* - Hook registration and execution
* - Event handling
* - Configuration access
* - Logging
*/
let intervalId = null;
/**
* Called when the plugin is loaded
* @param {Object} context - Plugin context API
*/
export async function activate(context) {
context.log('Plugin activated!');
const greeting = context.getConfig('greeting');
context.log(greeting);
// Register a hook that other plugins or the host can call
context.registerHook('on-startup', async () => {
context.log('Startup hook called!');
return { status: 'ready', plugin: 'example-plugin' };
});
// Register a data transformation hook
context.registerHook('transform-data', async (data) => {
context.log('Transforming data:', data);
return {
...data,
processedBy: 'example-plugin',
timestamp: new Date().toISOString()
};
});
// Listen to host events
context.on('plugin-loaded', (plugin) => {
context.log(`Another plugin was loaded: ${plugin.name}`);
});
// Example: Periodic task
intervalId = setInterval(() => {
context.log('Periodic task running...');
}, 30000); // Every 30 seconds
// Example: Run a hook that other plugins might listen to
const results = await context.runHook('example-event', { message: 'Hello!' });
context.log('Hook results:', results);
}
/**
* Called when the plugin is unloaded
* @param {Object} context - Plugin context API
*/
export async function deactivate(context) {
context.log('Plugin deactivating...');
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
}
context.log('Plugin deactivated!');
}
/**
* Example exported function that can be called directly
*/
export function doSomething(data) {
console.log('doSomething called with:', data);
return { success: true, data };
}
/**
* Example async function
*/
export async function fetchData(url) {
console.log('Fetching data from:', url);
// In a real plugin, you might fetch from an API
return { data: 'example data', url };
}