/** * Shared Configuration between PHP and JavaScript * * Provides unified configuration management for LiveComponents * that can be synchronized between server and client. */ export class SharedConfig { constructor() { this.config = { // Request settings requestTimeout: 30000, // 30 seconds retryAttempts: 3, retryDelays: [1000, 2000, 5000], // Progressive backoff // State settings stateVersioning: true, stateValidation: true, // Performance settings requestDeduplication: true, requestCacheTimeout: 1000, // 1 second batchFlushDelay: 50, // 50ms // Error handling errorBoundary: true, autoRetry: true, showErrorUI: true, // DevTools devToolsEnabled: false }; } /** * Load configuration from server * * @param {Object} serverConfig - Configuration from server */ loadFromServer(serverConfig) { if (serverConfig && typeof serverConfig === 'object') { this.config = { ...this.config, ...serverConfig }; } } /** * Load configuration from DOM * * Looks for data-livecomponent-config attribute on document or body */ loadFromDOM() { const configElement = document.querySelector('[data-livecomponent-config]') || document.body; const configJson = configElement.dataset.livecomponentConfig; if (configJson) { try { const parsed = JSON.parse(configJson); this.loadFromServer(parsed); } catch (error) { console.warn('[SharedConfig] Failed to parse config from DOM:', error); } } } /** * Get configuration value * * @param {string} key - Configuration key * @param {*} defaultValue - Default value if key not found * @returns {*} Configuration value */ get(key, defaultValue = null) { return this.config[key] ?? defaultValue; } /** * Set configuration value * * @param {string} key - Configuration key * @param {*} value - Configuration value */ set(key, value) { this.config[key] = value; } /** * Get all configuration * * @returns {Object} Full configuration object */ getAll() { return { ...this.config }; } /** * Merge configuration * * @param {Object} newConfig - Configuration to merge */ merge(newConfig) { this.config = { ...this.config, ...newConfig }; } } // Create singleton instance export const sharedConfig = new SharedConfig(); // Auto-load from DOM on initialization if (typeof document !== 'undefined') { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => { sharedConfig.loadFromDOM(); }); } else { sharedConfig.loadFromDOM(); } }