Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /** * Browser Runtime - Full client-side Coherent.js runtime * Works in browsers, Electron, and Tauri */ import { renderToString, withState, memo } from '@coherentjs/core'; import { hydrate, autoHydrate, makeHydratable } from '@coherentjs/client'; import { integrateWithWebComponents, defineCoherentElement } from '@coherentjs/web-components'; export class BrowserRuntime { constructor(options = {}) { this.options = { autoHydrate: true, enableWebComponents: true, enablePerformanceMonitoring: true, routingMode: 'hash', // 'hash', 'history', or 'memory' ...options }; this.componentRegistry = new Map(); this.routeRegistry = new Map(); this.currentRoute = null; this.isInitialized = false; // Performance tracking this.renderMetrics = []; this.startTime = Date.now(); } async initialize() { if (this.isInitialized) return; // Wait for DOM to be ready if (document.readyState === 'loading') { await new Promise(resolve => { document.addEventListener('DOMContentLoaded', resolve); }); } // Initialize Web Components integration if (this.options.enableWebComponents) { await this.initializeWebComponents(); } // Initialize routing if (this.options.routingMode !== 'none') { this.initializeRouting(); } // Auto-hydrate existing components if (this.options.autoHydrate) { await this.autoHydrate(); } // Initialize performance monitoring if (this.options.enablePerformanceMonitoring) { this.initializePerformanceMonitoring(); } this.isInitialized = true; } async initializeWebComponents() { try { await integrateWithWebComponents( Object.fromEntries(this.componentRegistry), this.options.webComponents || {} ); } catch (error) { console.warn('Failed to initialize Web Components:', error); } } initializeRouting() { const handleRouteChange = () => { const newRoute = this.getCurrentRoute(); if (newRoute !== this.currentRoute) { this.handleRouteChange(this.currentRoute, newRoute); this.currentRoute = newRoute; } }; if (this.options.routingMode === 'hash') { window.addEventListener('hashchange', handleRouteChange); } else if (this.options.routingMode === 'history') { window.addEventListener('popstate', handleRouteChange); } // Handle initial route this.currentRoute = this.getCurrentRoute(); handleRouteChange(); } getCurrentRoute() { if (this.options.routingMode === 'hash') { return window.location.hash.slice(1) || '/'; } else if (this.options.routingMode === 'history') { return window.location.pathname; } return '/'; } handleRouteChange(oldRoute, newRoute) { const handler = this.routeRegistry.get(newRoute) || this.routeRegistry.get('*'); if (handler) { try { handler({ route: newRoute, oldRoute, params: this.parseRouteParams(newRoute) }); } catch (error) { console.error('Route handler error:', error); } } } parseRouteParams(route) { // Simple parameter parsing - can be enhanced const params = {}; const parts = route.split('/').filter(Boolean); parts.forEach((part, index) => { if (part.startsWith(':')) { const key = part.slice(1); const value = parts[index]; if (value && !value.startsWith(':')) { params[key] = value; } } }); return params; } initializePerformanceMonitoring() { // Monitor render performance if (typeof window === 'undefined' || typeof window.PerformanceObserver === 'undefined') return; const observer = new window.PerformanceObserver((list) => { for (const entry of list.getEntries()) { if (entry.name.includes('coherent')) { this.renderMetrics.push({ name: entry.name, duration: entry.duration, startTime: entry.startTime, timestamp: Date.now() }); } } }); try { observer.observe({ entryTypes: ['measure', 'mark'] }); } catch (error) { console.warn('Performance monitoring not available:', error); } // Clean up old metrics periodically setInterval(() => { const cutoff = Date.now() - 300000; // 5 minutes this.renderMetrics = this.renderMetrics.filter(m => m.timestamp > cutoff); }, 60000); // Every minute } // Component management registerComponent(name, component, options = {}) { // Make component hydratable const hydratableComponent = makeHydratable(component, { componentName: name, ...options }); this.componentRegistry.set(name, hydratableComponent); // Register as Web Component if enabled if (this.options.enableWebComponents && this.isInitialized) { try { defineCoherentElement(name, hydratableComponent, options); } catch (error) { console.warn(`Failed to register Web Component ${name}:`, error); } } return hydratableComponent; } getComponent(name) { return this.componentRegistry.get(name); } // Routing addRoute(path, handler) { this.routeRegistry.set(path, handler); } navigate(path) { if (this.options.routingMode === 'hash') { window.location.hash = path; } else if (this.options.routingMode === 'history') { window.history.pushState({}, '', path); this.handleRouteChange(this.currentRoute, path); this.currentRoute = path; } } // Rendering async render(component, props = {}, target = null) { const startMark = `coherent-render-start-${Date.now()}`; const endMark = `coherent-render-end-${Date.now()}`; try { performance.mark(startMark); // Resolve component const resolvedComponent = typeof component === 'string' ? this.getComponent(component) : component; if (!resolvedComponent) { throw new Error(`Component not found: ${component}`); } // Render component const vdom = resolvedComponent(props); const html = renderToString(vdom); // Find or create target element let targetElement = target; if (typeof target === 'string') { targetElement = document.querySelector(target); } if (!targetElement) { targetElement = document.body; } // Update DOM targetElement.innerHTML = html; // Hydrate the rendered component const instance = await hydrate(targetElement.firstElementChild, resolvedComponent, props); performance.mark(endMark); performance.measure(`coherent-render-${resolvedComponent.name || 'anonymous'}`, startMark, endMark); return instance; } catch (error) { performance.mark(endMark); console.error('Render error:', error); throw error; } } // Create a complete app async createApp(_options = {}) { await this.initialize(); return { // Component management component: (name, component, opts) => this.registerComponent(name, component, opts), // Routing route: (path, handler) => this.addRoute(path, handler), navigate: (path) => this.navigate(path), // Rendering render: (component, props, target) => this.render(component, props, target), // State management state: withState, memo: memo, // Hydration hydrate: (element, component, props) => hydrate(element, component, props), // Utilities getRuntime: () => this, getCurrentRoute: () => this.currentRoute, getPerformanceMetrics: () => [...this.renderMetrics], // Lifecycle mount: async (component, target = '#app') => { const app = await this.render(component, {}, target); return app; }, unmount: (target = '#app') => { const element = typeof target === 'string' ? document.querySelector(target) : target; if (element) { element.innerHTML = ''; } } }; } // Auto-hydration async autoHydrate() { const componentMap = Object.fromEntries(this.componentRegistry); await autoHydrate(componentMap); } // Static methods for quick setup static async createQuickApp(components = {}, options = {}) { const runtime = new BrowserRuntime(options); // Register all components Object.entries(components).forEach(([name, component]) => { runtime.registerComponent(name, component); }); return await runtime.createApp(options); } static async renderToPage(component, props = {}, target = '#app') { const runtime = new BrowserRuntime({ autoHydrate: false }); await runtime.initialize(); return await runtime.render(component, props, target); } // Performance utilities getPerformanceReport() { const now = Date.now(); const uptime = now - this.startTime; const recentMetrics = this.renderMetrics.filter(m => m.timestamp >= now - 60000); const averageRenderTime = recentMetrics.length > 0 ? recentMetrics.reduce((sum, m) => sum + m.duration, 0) / recentMetrics.length : 0; return { uptime, totalRenders: this.renderMetrics.length, recentRenders: recentMetrics.length, averageRenderTime: Math.round(averageRenderTime * 100) / 100, registeredComponents: this.componentRegistry.size, registeredRoutes: this.routeRegistry.size, currentRoute: this.currentRoute, memoryUsage: this.getMemoryUsage() }; } getMemoryUsage() { if (typeof performance !== 'undefined' && performance.memory) { return { used: Math.round(performance.memory.usedJSHeapSize / 1024 / 1024), total: Math.round(performance.memory.totalJSHeapSize / 1024 / 1024), limit: Math.round(performance.memory.jsHeapSizeLimit / 1024 / 1024) }; } return null; } // Development utilities debug() { return { runtime: this, components: Array.from(this.componentRegistry.keys()), routes: Array.from(this.routeRegistry.keys()), performance: this.getPerformanceReport(), options: this.options }; } } |