90 lines
2.2 KiB
JavaScript
90 lines
2.2 KiB
JavaScript
/**
|
|
* Example Plugin 2 - Another demonstration plugin
|
|
*
|
|
* This plugin shows different patterns:
|
|
* - Data filtering
|
|
* - Configuration usage
|
|
* - Multiple hook patterns
|
|
*/
|
|
|
|
let taskInterval = null;
|
|
|
|
/**
|
|
* Called when the plugin is loaded
|
|
* @param {Object} context - Plugin context API
|
|
*/
|
|
export async function activate(context) {
|
|
context.log('Example Plugin 2 activated!');
|
|
|
|
const enabled = context.getConfig('enabled');
|
|
const interval = context.getConfig('interval');
|
|
|
|
context.log(`Configuration: enabled=${enabled}, interval=${interval}ms`);
|
|
|
|
// Register a data filtering hook
|
|
context.registerHook('filter-data', async (data) => {
|
|
context.log('Filtering data...');
|
|
// Filter out any items with null values
|
|
return data.filter(item => item !== null && item !== undefined);
|
|
});
|
|
|
|
// Register a validation hook
|
|
context.registerHook('validate-input', async (input) => {
|
|
context.log('Validating input:', input);
|
|
if (!input || typeof input !== 'string') {
|
|
throw new Error('Invalid input: must be a non-empty string');
|
|
}
|
|
return { valid: true, input };
|
|
});
|
|
|
|
// Register a transform hook
|
|
context.registerHook('transform-text', async (text) => {
|
|
context.log('Transforming text...');
|
|
return text.toUpperCase();
|
|
});
|
|
|
|
// Start periodic task if enabled
|
|
if (enabled) {
|
|
taskInterval = setInterval(() => {
|
|
context.log('Periodic check running...');
|
|
}, interval);
|
|
}
|
|
|
|
// Listen for other plugins loading
|
|
context.on('plugin-loaded', (plugin) => {
|
|
if (plugin.id !== 'example-plugin-2') {
|
|
context.log(`Detected plugin: ${plugin.name}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Called when the plugin is unloaded
|
|
* @param {Object} context - Plugin context API
|
|
*/
|
|
export async function deactivate(context) {
|
|
context.log('Example Plugin 2 deactivating...');
|
|
|
|
if (taskInterval) {
|
|
clearInterval(taskInterval);
|
|
taskInterval = null;
|
|
}
|
|
|
|
context.log('Example Plugin 2 deactivated!');
|
|
}
|
|
|
|
/**
|
|
* Process data array
|
|
*/
|
|
export function processData(data) {
|
|
console.log('Processing data array:', data);
|
|
return data.map(item => ({ ...item, processed: true }));
|
|
}
|
|
|
|
/**
|
|
* Format output
|
|
*/
|
|
export function formatOutput(data) {
|
|
return JSON.stringify(data, null, 2);
|
|
}
|