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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 | 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 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 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 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 | /** * Coherent.js - Object-Based Rendering Framework * A pure JavaScript framework for server-side and client-side rendering * using natural object syntax without function wrappers like h() or m() * * @version 1.0.0 * @author Coherent Framework Team * @license MIT */ // Core rendering engine import { renderToString, renderBatch, renderToChunks, renderWithTiming, precompileComponent, getCache, resetCache, getRenderingStats } from './rendering/html-renderer.js'; // Cache management import { createCacheManager, CacheManager } from './performance/cache-manager.js'; import { ComponentCache, createComponentCache, memoize } from './performance/component-cache.js'; // Enhanced error handling import { globalErrorHandler, ErrorHandler } from './utils/error-handler.js'; // Reactive state management import { ReactiveState, createReactiveState, observable, computed, stateUtils } from './state/reactive-state.js'; // Component lifecycle and events import { ComponentLifecycle, eventSystem, componentUtils, withLifecycle } from './components/lifecycle.js'; // Advanced routing import { Router, createRouter, routeGuards, routeComponents } from './routing/router.js'; // Form validation and binding import { FormValidator, createForm, validationRules, binding, formComponents } from './forms/validation.js'; // Streaming capabilities import { createStreamingRenderer, streamingUtils } from './rendering/streaming-renderer.js'; // DOM rendering capabilities import { renderToDOM, updateDOM } from './rendering/dom-renderer.js'; // Import hydration functions import { hydrate, makeHydratable, autoHydrate, hydrateAll, hydrateBySelector, enableClientEvents } from './client/hydration.js'; // Configuration utilities import { createHtmlConfig, createStreamingConfig, createDomConfig, createDevConfig, createProdConfig, validateConfig, mergeConfigs, getConfigPreset, CONFIG_PRESETS } from './rendering/renderer-config.js'; // Performance monitoring and optimization import { performanceMonitor } from './performance/monitor.js'; // NOTE: Database functionality moved to @coherentjs/database package // import { DatabaseManager, ... } from '@coherentjs/database'; // Core utilities import { validateComponent, isCoherentObject, extractProps, hasChildren, normalizeChildren, deepClone, mergeProps, getNestedValue, setNestedValue } from './core/object-utils.js'; import { escapeHtml, isVoidElement, formatAttributes, minifyHtml } from './core/html-utils.js'; // Component system import { createComponent, withState, withProps, memo, lazy } from './components/component-system.js'; // Development tools import { DevTools } from './dev/dev-tools.js'; /** * Main Coherent Framework Class * Provides a unified interface to all framework capabilities */ class Coherent { constructor(options = {}) { this.options = { enableCache: options.enableCache !== false, enableMonitoring: options.enableMonitoring !== false, enableDevTools: options.enableDevTools !== false, cacheSize: options.cacheSize || 1000, cacheTTL: options.cacheTTL || 300000, // 5 minutes maxDepth: options.maxDepth || 100, minify: options.minify || false, ...options }; // Initialize cache if enabled if (this.options.enableCache) { this.cache = createCacheManager({ maxSize: this.options.cacheSize, ttlMs: this.options.cacheTTL }); // Initialize component cache for advanced caching this.componentCache = createComponentCache({ maxSize: this.options.componentCacheSize || 500, defaultTTL: this.options.componentCacheTTL || 300000 }); } // Initialize reactive state system this.state = createReactiveState({}, { enableHistory: this.options.enableStateHistory !== false, maxHistorySize: this.options.stateHistorySize || 50 }); // Initialize router if routing is enabled if (this.options.enableRouting) { this.router = createRouter(this.options.routerOptions || {}); } // Initialize global error handler this.errorHandler = this.options.errorHandler || globalErrorHandler; // Initialize dev tools in development if (this.options.enableDevTools && typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') { this.devTools = new DevTools(this); } // Bind methods to maintain context this.render = this.render.bind(this); this.renderToString = this.renderToString.bind(this); this.renderBatch = this.renderBatch.bind(this); this.stream = this.stream.bind(this); } /** * Primary render method - converts components to HTML */ render(component, options = {}) { const config = { ...this.options, ...options }; try { const html = renderToString(component, config); // Development debugging if (this.devTools) { this.devTools.logRender(component, html, config); } return html; } catch (error) { if (this.devTools) { this.devTools.logError('render', error, { component, options }); } throw error; } } /** * Alias for render method (for clarity) */ renderToString(component, options = {}) { return this.render(component, options); } /** * Batch rendering for multiple components */ renderBatch(components, options = {}) { const config = { ...this.options, ...options }; try { const results = renderBatch(components, config); if (this.devTools) { this.devTools.logBatchRender(components, results, config); } return results; } catch (error) { if (this.devTools) { this.devTools.logError('renderBatch', error, { components, options }); } throw error; } } /** * Streaming render for large components */ async* stream(component, options = {}) { const config = { ...this.options, ...options, streaming: true }; try { for await (const chunk of renderToStream(component, config)) { if (this.devTools) { this.devTools.logStreamChunk(chunk); } yield chunk; } } catch (error) { if (this.devTools) { this.devTools.logError('stream', error, { component, options }); } throw error; } } /** * Create a streaming renderer instance */ createStreamingRenderer(options = {}) { const config = { ...this.options, ...options }; return createStreamingRenderer(config); } /** * Component creation helpers */ createComponent(definition) { return createComponent(definition); } withState(component, initialState) { return withState(component, initialState); } withProps(component, props) { return withProps(component, props); } memo(component, areEqual) { return memo(component, areEqual); } lazy(componentLoader) { return lazy(componentLoader); } /** * Component caching and memoization */ memoize(component, keyGenerator, options = {}) { return memoize(component, keyGenerator, { ...options, cache: this.componentCache }); } /** * Update DOM with virtual DOM diffing */ updateDOM(element, newComponent, componentId = 'default', options = {}) { const config = { ...this.options, ...options }; return updateDOM(element, newComponent, componentId, config); } /** * Invalidate component cache by dependencies */ invalidateCache(dependencies) { if (this.componentCache) { if (Array.isArray(dependencies)) { return this.componentCache.invalidateMultiple(dependencies); } else { return this.componentCache.invalidate(dependencies); } } return 0; } /** * Reactive state management */ setState(key, value) { return this.state.set(key, value); } getState(key) { return this.state.get(key); } watchState(key, callback, options) { return this.state.watch(key, callback, options); } createComputed(key, getter, options) { return this.state.computed(key, getter, options); } /** * Event system */ emit(eventName, data, target) { return eventSystem.emit(eventName, data, target); } on(eventName, handler, componentId) { return eventSystem.on(eventName, handler, componentId); } off(eventName, handler, componentId) { return eventSystem.off(eventName, handler, componentId); } /** * Router methods */ navigate(location) { if (!this.router) { console.warn('Router not initialized. Enable routing in options.'); return Promise.resolve(false); } return this.router.push(location); } getCurrentRoute() { return this.router ? this.router.getCurrentRoute() : null; } addRoutes(routes) { if (this.router) { this.router.addRoutes(routes); } } /** * Form validation */ createForm(schema, options) { return createForm(schema, options); } /** * Component lifecycle */ createComponentWithLifecycle(component, options) { return componentUtils.createWithLifecycle(component, options); } /** * Error handling */ handleError(error, context) { return this.errorHandler.handle(error, context); } /** * Performance and monitoring */ getPerformanceStats() { return { cache: this.cache ? this.cache.getStats() : null, componentCache: this.componentCache ? this.componentCache.getStats() : null, monitor: performanceMonitor.getStats(), rendering: getRenderingStats() }; } getPerformanceRecommendations() { const stats = this.getPerformanceStats(); const recommendations = []; // Cache recommendations if (stats.cache && stats.cache.hitRate < 50) { recommendations.push({ type: 'cache', priority: 'HIGH', suggestion: 'Consider increasing cache size or enabling component-level caching', impact: 'Major performance improvement for repeated renders', details: `Current hit rate: ${stats.cache.hitRate}%` }); } // Hot path recommendations if (this.cache) { const hotPaths = this.cache.getHotPathElements(10); hotPaths.forEach(({ tagName, count }) => { recommendations.push({ type: 'static-cache', priority: 'MEDIUM', suggestion: `Consider static caching for: ${tagName}`, impact: 'Significant performance improvement for hot paths', details: `Rendered ${count} times, good candidate for static caching` }); }); } // Memory recommendations if (stats.monitor && stats.monitor.avgRenderTime > 10) { recommendations.push({ type: 'optimization', priority: 'MEDIUM', suggestion: 'Consider component memoization or precompilation', impact: 'Reduced render times and better user experience', details: `Average render time: ${stats.monitor.avgRenderTime}ms` }); } return recommendations; } /** * Development and debugging tools */ enableDevMode() { if (!this.devTools) { this.devTools = new DevTools(this); } this.options.enableDevTools = true; return this; } disableDevMode() { this.options.enableDevTools = false; this.devTools = null; return this; } benchmark(component, iterations = 1000) { console.time('Coherent Benchmark'); for (let i = 0; i < iterations; i++) { this.render(component, { enableMonitoring: false }); } console.timeEnd('Coherent Benchmark'); const stats = this.getPerformanceStats(); console.log(`\nš Benchmark Results (${iterations} iterations):`); console.log(`Average render time: ${stats.monitor.avgRenderTime}ms`); console.log(`Cache hit rate: ${stats.cache ? stats.cache.hitRate : 0}%`); return stats; } /** * Cache management */ clearCache() { if (this.cache) { this.cache.clear(); return true; } return false; } getCacheStats() { if (!this.cache) { return { enabled: false, size: 0, hits: 0, misses: 0, hitRate: 0 }; } return this.cache.getStats(); } optimizeCache() { if (this.cache) { this.cache.cleanup(); return true; } return false; } /** * Precompilation for static components */ precompile(component, options = {}) { const config = { ...this.options, ...options }; return precompileComponent(component, config); } /** * Validation helpers */ validate(component) { try { validateComponent(component); return { valid: true }; } catch (error) { return { valid: false, error: error.message }; } } isCoherent(obj) { return isCoherentObject(obj); } /** * Utility methods */ clone(obj) { return deepClone(obj); } merge(obj1, obj2) { return mergeProps(obj1, obj2); } extract(element) { return extractProps(element); } normalize(children) { return normalizeChildren(children); } escape(text) { return escapeHtml(text); } /** * Framework information */ getVersion() { return '1.0.0'; } getInfo() { return { name: 'Coherent.js', version: this.getVersion(), description: 'Object-based rendering framework', features: [ 'Pure object syntax', 'Server-side rendering', 'Performance monitoring', 'Intelligent caching', 'Streaming support', 'Development tools' ], stats: this.getPerformanceStats() }; } } /** * Convenience function to create a Coherent instance */ export function createCoherent(options = {}) { return new Coherent(options); } // NOTE: createDatabaseManager moved to @coherentjs/database package /** * Default instance for quick usage */ export const coherent = new Coherent(); /** * Named exports for specific functionality */ export { // Main class Coherent, // Core rendering renderToString, renderBatch, renderToChunks, renderWithTiming, // Streaming createStreamingRenderer, streamingUtils, // Client-side rendering and hydration renderToDOM, updateDOM, hydrate, makeHydratable, autoHydrate, hydrateAll, hydrateBySelector, enableClientEvents, // Components createComponent, withState, withProps, memo, lazy, // NOTE: Database functionality moved to @coherentjs/database package // Utilities validateComponent, isCoherentObject, extractProps, hasChildren, normalizeChildren, deepClone, escapeHtml, isVoidElement, formatAttributes, minifyHtml, // Configuration utilities createHtmlConfig, createStreamingConfig, createDomConfig, createDevConfig, createProdConfig, validateConfig, mergeConfigs, getConfigPreset, CONFIG_PRESETS, // Performance performanceMonitor, CacheManager, ComponentCache, createComponentCache, memoize, getCache, resetCache, getRenderingStats, // Error Handling ErrorHandler, globalErrorHandler, // Reactive State ReactiveState, createReactiveState, observable, computed, stateUtils, // Component Lifecycle & Events ComponentLifecycle, eventSystem, componentUtils, withLifecycle, // Routing Router, createRouter, routeGuards, routeComponents, // Forms & Validation FormValidator, createForm, validationRules, binding, formComponents, // Development DevTools }; /** * Export specific module bundles for different use cases */ // Server-side rendering bundle export const server = { render: renderToString, renderBatch, chunk: renderToChunks, // chunking pre-rendered HTML stream: createStreamingRenderer, // true progressive streaming CacheManager, performanceMonitor }; // Component development bundle export const components = { createComponent, withState, withProps, memo, lazy, validateComponent, isCoherentObject }; // Utilities bundle - Pure JS object approach export const utils = { // Core utilities extractProps, hasChildren, normalizeChildren, deepClone, escapeHtml, isVoidElement, formatAttributes, minifyHtml, mergeProps, getNestedValue, setNestedValue, // NOTE: Database functionality moved to @coherentjs/database package }; // Performance bundle export const performance = { performanceMonitor, CacheManager, getCache, resetCache, getRenderingStats }; /** * Browser compatibility check */ if (typeof window !== 'undefined') { // Browser environment window.Coherent = Coherent; window.coherent = coherent; // Add browser-specific optimizations if (typeof window !== 'undefined' && window.requestIdleCallback) { // Use idle time for cache optimization const optimizeInIdle = () => { if (coherent.cache) { coherent.cache.optimize('component'); } }; window.requestIdleCallback(optimizeInIdle); } } /** * Node.js environment setup */ if (typeof process !== 'undefined' && process.versions && process.versions.node) { // Node.js environment // Graceful cleanup on exit process.on('exit', () => { if (coherent.cache) { coherent.cache.destroy(); } }); // Performance monitoring in development if (process.env.NODE_ENV === 'development') { coherent.enableDevMode(); } } /** * Version and build information */ export const VERSION = '1.0.0'; export const BUILD_DATE = new Date().toISOString(); export const FEATURES = [ 'object-syntax', 'server-rendering', 'streaming', 'caching', 'monitoring', 'components', 'dev-tools' ].join(','); // Single default export export default Coherent; |