UNPKG

@oxog/delay

Version:

A comprehensive, zero-dependency delay/timeout utility library with advanced timing features

155 lines 4.92 kB
export class PluginManager { constructor() { this.plugins = new Map(); this.delayInstance = null; } setDelayInstance(instance) { this.delayInstance = instance; } register(plugin) { if (this.plugins.has(plugin.name)) { throw new Error(`Plugin with name "${plugin.name}" is already registered`); } this.plugins.set(plugin.name, plugin); if (this.delayInstance && plugin.init) { plugin.init(this.delayInstance); } } unregister(pluginName) { const plugin = this.plugins.get(pluginName); if (!plugin) { throw new Error(`Plugin with name "${pluginName}" is not registered`); } if (plugin.destroy) { plugin.destroy(); } this.plugins.delete(pluginName); } get(pluginName) { return this.plugins.get(pluginName); } has(pluginName) { return this.plugins.has(pluginName); } list() { return Array.from(this.plugins.values()); } clear() { for (const plugin of this.plugins.values()) { if (plugin.destroy) { try { plugin.destroy(); } catch (error) { console.error(`Error destroying plugin ${plugin.name}:`, error); } } } this.plugins.clear(); } initializeAll() { if (!this.delayInstance) { throw new Error('Delay instance not set'); } for (const plugin of this.plugins.values()) { if (plugin.init) { try { plugin.init(this.delayInstance); } catch (error) { console.error(`Error initializing plugin ${plugin.name}:`, error); } } } } } export function createLoggingPlugin() { return { name: 'logging', version: '1.0.0', init(delay) { const originalDelay = delay.bind({}); // Override the main delay function to add logging Object.assign(delay, function (ms, options) { console.log(`[delay] Starting delay of ${ms}ms`); const start = Date.now(); return originalDelay(ms, options).then(() => { const actual = Date.now() - start; console.log(`[delay] Delay completed in ${actual}ms (target: ${ms}ms)`); }); }); }, destroy() { console.log('[delay] Logging plugin destroyed'); }, }; } export function createMetricsPlugin() { const metrics = { totalDelays: 0, totalTime: 0, averageDelay: 0, minDelay: Infinity, maxDelay: 0, }; return { name: 'metrics', version: '1.0.0', init(delay) { const originalDelay = delay.bind({}); Object.assign(delay, function (ms, options) { const start = Date.now(); metrics.totalDelays++; metrics.minDelay = Math.min(metrics.minDelay, ms); metrics.maxDelay = Math.max(metrics.maxDelay, ms); return originalDelay(ms, options).then(() => { const actual = Date.now() - start; metrics.totalTime += actual; metrics.averageDelay = metrics.totalTime / metrics.totalDelays; }); }); // Add metrics accessor to delay instance delay.getMetrics = () => ({ ...metrics }); }, destroy() { // Reset metrics Object.assign(metrics, { totalDelays: 0, totalTime: 0, averageDelay: 0, minDelay: Infinity, maxDelay: 0, }); }, }; } export function createDebugPlugin() { return { name: 'debug', version: '1.0.0', init(delay) { // Add debug information to the delay instance delay.debug = { isDebugMode: true, logLevel: 'info', setLogLevel(level) { this.logLevel = level; }, log(level, message, data) { if (this.isDebugMode) { if (data !== undefined) { console[level]?.(`[delay:${level}] ${message}`, data); } else { console[level]?.(`[delay:${level}] ${message}`); } } }, }; }, destroy() { console.log('[delay] Debug plugin destroyed'); }, }; } //# sourceMappingURL=plugin-manager.js.map