Lifecycle Hooks
Overview
Section titled “Overview”Plugins can respond to lifecycle events through handler functions defined in the manifest. These hooks allow plugins to initialize resources, clean up state, and respond to user actions.
Available Lifecycle Hooks
Section titled “Available Lifecycle Hooks”interface PluginHandlers { onEnable?: () => Promise<void>; onDisable?: () => Promise<void>; onInstall?: () => Promise<void>; onUninstall?: () => Promise<void>;}onEnable
Section titled “onEnable”Called when a user enables the plugin in Settings > Plugins.
When to use:
- Initialize runtime resources
- Set up event listeners
- Load cached data
- Register global handlers
Example:
handlers: { onEnable: async () => { console.log('Timer plugin enabled');
// Initialize storage const timers = localStorage.getItem('aether_timers'); if (!timers) { localStorage.setItem('aether_timers', JSON.stringify({})); }
// Set up event listeners window.addEventListener('beforeunload', handleBeforeUnload);
// Notify user showNotification({ title: 'Timer plugin enabled', message: 'You can now track time on work units', color: 'success', }); }}onDisable
Section titled “onDisable”Called when a user disables the plugin in Settings > Plugins.
When to use:
- Clean up resources
- Remove event listeners
- Save state before disabling
- Archive data
Example:
handlers: { onDisable: async () => { console.log('Timer plugin disabled');
// Stop any running timers const timers = getTimersFromStorage(); Object.keys(timers).forEach(id => { if (timers[id].isRunning) { stopTimer(id); } });
// Remove event listeners window.removeEventListener('beforeunload', handleBeforeUnload);
// Notify user showNotification({ title: 'Timer plugin disabled', message: 'Time tracking has been stopped', color: 'warning', }); }}onInstall
Section titled “onInstall”Called once when the plugin is first installed.
When to use:
- Run database migrations
- Create default configuration
- Set up initial data
- Create plugin-specific tables
onUninstall
Section titled “onUninstall”Called when the plugin is uninstalled.
When to use:
- Remove plugin data
- Clean up database tables
- Delete plugin-specific settings
- Archive data for export
Lifecycle Flow
Section titled “Lifecycle Flow”Installation Flow
Section titled “Installation Flow”- User installs plugin package
- Platform discovers plugin manifest
- onInstall hook called
- Plugin appears in Settings > Plugins (disabled by default)
Enable Flow
Section titled “Enable Flow”- User enables plugin in settings
- onEnable hook called
- Plugin extensions registered
- UI updates with new extensions
Disable Flow
Section titled “Disable Flow”- User disables plugin in settings
- onDisable hook called
- Plugin extensions unregistered
- UI updates to remove extensions
Uninstall Flow
Section titled “Uninstall Flow”- User uninstalls plugin
- onUninstall hook called
- Plugin removed from registry
- UI fully cleaned up
Error Handling
Section titled “Error Handling”Lifecycle hooks should handle errors gracefully:
handlers: { onEnable: async () => { try { await initializePlugin(); showNotification({ title: 'Plugin enabled', color: 'success', }); } catch (error) { console.error('Failed to enable plugin:', error); showNotification({ title: 'Failed to enable plugin', message: error.message, color: 'error', }); throw error; } }}Best Practices
Section titled “Best Practices”1. Keep Hooks Fast
Section titled “1. Keep Hooks Fast”Avoid long-running operations that block the UI. Queue background work instead.
2. Clean Up Resources
Section titled “2. Clean Up Resources”Track and clean up all resources (listeners, timers, etc) in onDisable.
3. Provide User Feedback
Section titled “3. Provide User Feedback”Show notifications to inform users about lifecycle events.
4. Handle State Transitions
Section titled “4. Handle State Transitions”Properly manage initialization state across enable/disable cycles.