@coherent.js/nextjs 1.0.0-beta.3 → 1.0.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,3618 +1,10 @@
1
- // ../core/src/performance/monitor.js
2
- function createPerformanceMonitor(options = {}) {
3
- const opts = {
4
- enabled: true,
5
- metrics: {
6
- custom: {}
7
- },
8
- sampling: {
9
- enabled: false,
10
- rate: 1,
11
- strategy: "random"
12
- },
13
- reporting: {
14
- enabled: false,
15
- interval: 6e4,
16
- format: "json",
17
- batch: {
18
- enabled: false,
19
- maxSize: 100,
20
- flushInterval: 5e3
21
- },
22
- onReport: null
23
- },
24
- alerts: {
25
- enabled: true,
26
- rules: []
27
- },
28
- resources: {
29
- enabled: false,
30
- track: ["memory"],
31
- interval: 1e3
32
- },
33
- profiling: {
34
- enabled: false,
35
- mode: "production",
36
- flamegraph: false,
37
- tracing: {
38
- enabled: false,
39
- sampleRate: 0.01
40
- }
41
- },
42
- ...options
43
- };
44
- opts.reporting.batch = {
45
- enabled: false,
46
- maxSize: 100,
47
- flushInterval: 5e3,
48
- ...options.reporting?.batch || {}
49
- };
50
- const metrics = {
51
- builtin: {
52
- renderTime: { type: "histogram", unit: "ms", values: [] },
53
- componentCount: { type: "counter", unit: "renders", value: 0 },
54
- errorCount: { type: "counter", unit: "errors", value: 0 },
55
- memoryUsage: { type: "gauge", unit: "MB", values: [] }
56
- },
57
- custom: {}
58
- };
59
- Object.entries(opts.metrics.custom).forEach(([name, config]) => {
60
- metrics.custom[name] = {
61
- type: config.type || "counter",
62
- unit: config.unit || "",
63
- threshold: config.threshold,
64
- values: config.type === "histogram" ? [] : void 0,
65
- value: config.type === "counter" || config.type === "gauge" ? 0 : void 0
66
- };
67
- });
68
- const samplingState = {
69
- count: 0,
70
- sampled: 0,
71
- adaptiveRate: opts.sampling.rate
72
- };
73
- const reportingState = {
74
- batch: [],
75
- lastReport: Date.now(),
76
- reportTimer: null,
77
- flushTimer: null
78
- };
79
- const alertState = {
80
- triggered: /* @__PURE__ */ new Map(),
81
- history: []
82
- };
83
- const resourceState = {
84
- samples: [],
85
- timer: null
86
- };
87
- const profilingState = {
88
- traces: [],
89
- flamegraphData: []
90
- };
91
- const stats = {
92
- metricsRecorded: 0,
93
- sampleRate: opts.sampling.rate,
94
- reportsGenerated: 0,
95
- alertsTriggered: 0
96
- };
97
- function shouldSample() {
98
- if (!opts.sampling.enabled) return true;
99
- samplingState.count++;
100
- if (opts.sampling.strategy === "random") {
101
- return Math.random() < samplingState.adaptiveRate;
102
- } else if (opts.sampling.strategy === "deterministic") {
103
- return samplingState.count % Math.ceil(1 / samplingState.adaptiveRate) === 0;
104
- } else if (opts.sampling.strategy === "adaptive") {
105
- const recentRenderTimes = metrics.builtin.renderTime.values.slice(-10);
106
- if (recentRenderTimes.length > 0) {
107
- const avgTime = recentRenderTimes.reduce((a, b) => a + b, 0) / recentRenderTimes.length;
108
- samplingState.adaptiveRate = avgTime > 16 ? Math.min(1, opts.sampling.rate * 2) : opts.sampling.rate;
109
- }
110
- return Math.random() < samplingState.adaptiveRate;
111
- }
112
- return true;
113
- }
114
- function recordMetric(name, value, metadata = {}) {
115
- if (!opts.enabled) return;
116
- if (!shouldSample()) return;
117
- stats.metricsRecorded++;
118
- const builtinMetric = metrics.builtin[name];
119
- if (builtinMetric) {
120
- if (builtinMetric.type === "histogram") {
121
- builtinMetric.values.push(value);
122
- if (builtinMetric.values.length > 1e3) {
123
- builtinMetric.values = builtinMetric.values.slice(-1e3);
124
- }
125
- } else if (builtinMetric.type === "counter") {
126
- builtinMetric.value += value;
127
- } else if (builtinMetric.type === "gauge") {
128
- builtinMetric.values.push(value);
129
- if (builtinMetric.values.length > 100) {
130
- builtinMetric.values = builtinMetric.values.slice(-100);
131
- }
132
- }
133
- }
134
- const customMetric = metrics.custom[name];
135
- if (customMetric) {
136
- if (customMetric.type === "histogram") {
137
- customMetric.values = customMetric.values || [];
138
- customMetric.values.push(value);
139
- if (customMetric.values.length > 1e3) {
140
- customMetric.values = customMetric.values.slice(-1e3);
141
- }
142
- } else if (customMetric.type === "counter") {
143
- customMetric.value = (customMetric.value || 0) + value;
144
- } else if (customMetric.type === "gauge") {
145
- customMetric.values = customMetric.values || [];
146
- customMetric.values.push(value);
147
- if (customMetric.values.length > 100) {
148
- customMetric.values = customMetric.values.slice(-100);
149
- }
150
- }
151
- if (customMetric.threshold) {
152
- const currentValue = customMetric.type === "histogram" || customMetric.type === "gauge" ? customMetric.values[customMetric.values.length - 1] : customMetric.value;
153
- if (currentValue > customMetric.threshold) {
154
- checkAlerts(name, currentValue);
155
- }
156
- }
157
- }
158
- if (opts.reporting.enabled && opts.reporting.batch.enabled) {
159
- reportingState.batch.push({
160
- metric: name,
161
- value,
162
- metadata,
163
- timestamp: Date.now()
164
- });
165
- if (reportingState.batch.length >= opts.reporting.batch.maxSize) {
166
- flushBatch();
167
- }
168
- }
169
- checkAlerts(name, value);
170
- }
171
- function checkAlerts(metric, value) {
172
- if (!opts.alerts.enabled) return;
173
- opts.alerts.rules.forEach((rule) => {
174
- if (rule.metric !== metric) return;
175
- let triggered = false;
176
- if (rule.condition === "exceeds" && value > rule.threshold) {
177
- triggered = true;
178
- } else if (rule.condition === "below" && value < rule.threshold) {
179
- triggered = true;
180
- } else if (rule.condition === "equals" && value === rule.threshold) {
181
- triggered = true;
182
- }
183
- if (triggered) {
184
- const alertKey = `${rule.metric}-${rule.condition}-${rule.threshold}`;
185
- const lastTriggered = alertState.triggered.get(alertKey);
186
- const now = Date.now();
187
- if (!lastTriggered || now - lastTriggered > 5e3) {
188
- alertState.triggered.set(alertKey, now);
189
- alertState.history.push({
190
- rule,
191
- value,
192
- timestamp: now
193
- });
194
- stats.alertsTriggered++;
195
- if (rule.action) {
196
- rule.action(value, rule);
197
- }
198
- }
199
- }
200
- });
201
- }
202
- function flushBatch() {
203
- if (reportingState.batch.length === 0) return;
204
- const batch = [...reportingState.batch];
205
- reportingState.batch = [];
206
- if (opts.reporting.onReport) {
207
- opts.reporting.onReport({ type: "batch", data: batch });
208
- }
209
- }
210
- function generateReport() {
211
- const report = {
212
- timestamp: Date.now(),
213
- statistics: { ...stats },
214
- metrics: {}
215
- };
216
- Object.entries(metrics.builtin).forEach(([name, metric]) => {
217
- if (metric.type === "histogram") {
218
- report.metrics[name] = {
219
- type: "histogram",
220
- unit: metric.unit,
221
- count: metric.values.length,
222
- min: metric.values.length > 0 ? Math.min(...metric.values) : 0,
223
- max: metric.values.length > 0 ? Math.max(...metric.values) : 0,
224
- avg: metric.values.length > 0 ? metric.values.reduce((a, b) => a + b, 0) / metric.values.length : 0,
225
- p50: percentile(metric.values, 0.5),
226
- p95: percentile(metric.values, 0.95),
227
- p99: percentile(metric.values, 0.99)
228
- };
229
- } else if (metric.type === "counter") {
230
- report.metrics[name] = {
231
- type: "counter",
232
- unit: metric.unit,
233
- value: metric.value
234
- };
235
- } else if (metric.type === "gauge") {
236
- report.metrics[name] = {
237
- type: "gauge",
238
- unit: metric.unit,
239
- current: metric.values.length > 0 ? metric.values[metric.values.length - 1] : 0,
240
- avg: metric.values.length > 0 ? metric.values.reduce((a, b) => a + b, 0) / metric.values.length : 0
241
- };
242
- }
243
- });
244
- Object.entries(metrics.custom).forEach(([name, metric]) => {
245
- if (metric.type === "histogram") {
246
- report.metrics[name] = {
247
- type: "histogram",
248
- unit: metric.unit,
249
- count: metric.values?.length || 0,
250
- min: metric.values?.length > 0 ? Math.min(...metric.values) : 0,
251
- max: metric.values?.length > 0 ? Math.max(...metric.values) : 0,
252
- avg: metric.values?.length > 0 ? metric.values.reduce((a, b) => a + b, 0) / metric.values.length : 0,
253
- p95: percentile(metric.values || [], 0.95),
254
- p99: percentile(metric.values || [], 0.99)
255
- };
256
- } else if (metric.type === "counter") {
257
- report.metrics[name] = {
258
- type: "counter",
259
- unit: metric.unit,
260
- value: metric.value || 0
261
- };
262
- } else if (metric.type === "gauge") {
263
- report.metrics[name] = {
264
- type: "gauge",
265
- unit: metric.unit,
266
- current: metric.values?.length > 0 ? metric.values[metric.values.length - 1] : 0,
267
- avg: metric.values?.length > 0 ? metric.values.reduce((a, b) => a + b, 0) / metric.values.length : 0
268
- };
269
- }
270
- });
271
- report.alerts = {
272
- total: alertState.history.length,
273
- recent: alertState.history.slice(-10)
274
- };
275
- if (opts.resources.enabled) {
276
- report.resources = {
277
- samples: resourceState.samples.slice(-20)
278
- };
279
- }
280
- stats.reportsGenerated++;
281
- if (opts.reporting.onReport) {
282
- opts.reporting.onReport({ type: "report", data: report });
283
- }
284
- return report;
285
- }
286
- function percentile(values, p) {
287
- if (values.length === 0) return 0;
288
- const sorted = [...values].sort((a, b) => a - b);
289
- const index = Math.ceil(sorted.length * p) - 1;
290
- return sorted[Math.max(0, index)];
291
- }
292
- function startResourceMonitoring() {
293
- if (!opts.resources.enabled) return;
294
- const collectResources = () => {
295
- const sample = {
296
- timestamp: Date.now()
297
- };
298
- if (opts.resources.track.includes("memory")) {
299
- if (typeof process !== "undefined" && process.memoryUsage) {
300
- const mem = process.memoryUsage();
301
- sample.memory = {
302
- heapUsed: mem.heapUsed / 1024 / 1024,
303
- heapTotal: mem.heapTotal / 1024 / 1024,
304
- external: mem.external / 1024 / 1024,
305
- rss: mem.rss / 1024 / 1024
306
- };
307
- } else if (typeof performance !== "undefined" && performance.memory) {
308
- sample.memory = {
309
- heapUsed: performance.memory.usedJSHeapSize / 1024 / 1024,
310
- heapTotal: performance.memory.totalJSHeapSize / 1024 / 1024
311
- };
312
- }
313
- }
314
- resourceState.samples.push(sample);
315
- if (resourceState.samples.length > 100) {
316
- resourceState.samples = resourceState.samples.slice(-100);
317
- }
318
- resourceState.timer = setTimeout(collectResources, opts.resources.interval);
319
- };
320
- collectResources();
321
- }
322
- function stopResourceMonitoring() {
323
- if (resourceState.timer) {
324
- clearTimeout(resourceState.timer);
325
- resourceState.timer = null;
326
- }
327
- }
328
- function startReporting() {
329
- if (!opts.reporting.enabled) return;
330
- reportingState.reportTimer = setInterval(() => {
331
- generateReport();
332
- }, opts.reporting.interval);
333
- if (opts.reporting.batch.enabled) {
334
- reportingState.flushTimer = setInterval(() => {
335
- flushBatch();
336
- }, opts.reporting.batch.flushInterval);
337
- }
338
- }
339
- function stopReporting() {
340
- if (reportingState.reportTimer) {
341
- clearInterval(reportingState.reportTimer);
342
- reportingState.reportTimer = null;
343
- }
344
- if (reportingState.flushTimer) {
345
- clearInterval(reportingState.flushTimer);
346
- reportingState.flushTimer = null;
347
- }
348
- flushBatch();
349
- }
350
- function startProfiling() {
351
- if (!opts.profiling.enabled) return;
352
- }
353
- function recordTrace(name, duration, metadata = {}) {
354
- if (!opts.profiling.enabled || !opts.profiling.tracing.enabled) return;
355
- if (Math.random() < opts.profiling.tracing.sampleRate) {
356
- profilingState.traces.push({
357
- name,
358
- duration,
359
- metadata,
360
- timestamp: Date.now()
361
- });
362
- if (profilingState.traces.length > 1e3) {
363
- profilingState.traces = profilingState.traces.slice(-1e3);
364
- }
365
- }
366
- }
367
- function measure(name, fn, metadata = {}) {
368
- if (!opts.enabled) return fn();
369
- const start = performance.now();
370
- try {
371
- const result = fn();
372
- const duration = performance.now() - start;
373
- recordMetric("renderTime", duration, { name, ...metadata });
374
- recordTrace(name, duration, metadata);
375
- return result;
376
- } catch (error) {
377
- recordMetric("errorCount", 1, { name, error: error.message });
378
- throw error;
379
- }
380
- }
381
- async function measureAsync(name, fn, metadata = {}) {
382
- if (!opts.enabled) return fn();
383
- const start = performance.now();
384
- try {
385
- const result = await fn();
386
- const duration = performance.now() - start;
387
- recordMetric("renderTime", duration, { name, ...metadata });
388
- recordTrace(name, duration, metadata);
389
- return result;
390
- } catch (error) {
391
- recordMetric("errorCount", 1, { name, error: error.message });
392
- throw error;
393
- }
394
- }
395
- function addMetric(name, config) {
396
- metrics.custom[name] = {
397
- type: config.type || "counter",
398
- unit: config.unit || "",
399
- threshold: config.threshold,
400
- values: config.type === "histogram" ? [] : void 0,
401
- value: config.type === "counter" || config.type === "gauge" ? 0 : void 0
402
- };
403
- }
404
- function addAlertRule(rule) {
405
- opts.alerts.rules.push(rule);
406
- }
407
- function getStats() {
408
- return {
409
- ...stats,
410
- sampleRate: samplingState.adaptiveRate,
411
- batchSize: reportingState.batch.length,
412
- resourceSamples: resourceState.samples.length,
413
- traces: profilingState.traces.length,
414
- alerts: {
415
- total: alertState.history.length,
416
- unique: alertState.triggered.size
417
- }
418
- };
419
- }
420
- function reset() {
421
- Object.values(metrics.builtin).forEach((metric) => {
422
- if (metric.type === "histogram" || metric.type === "gauge") {
423
- metric.values = [];
424
- } else if (metric.type === "counter") {
425
- metric.value = 0;
426
- }
427
- });
428
- Object.values(metrics.custom).forEach((metric) => {
429
- if (metric.type === "histogram" || metric.type === "gauge") {
430
- metric.values = [];
431
- } else if (metric.type === "counter") {
432
- metric.value = 0;
433
- }
434
- });
435
- samplingState.count = 0;
436
- samplingState.sampled = 0;
437
- reportingState.batch = [];
438
- alertState.history = [];
439
- alertState.triggered.clear();
440
- resourceState.samples = [];
441
- profilingState.traces = [];
442
- stats.metricsRecorded = 0;
443
- stats.reportsGenerated = 0;
444
- stats.alertsTriggered = 0;
445
- }
446
- if (opts.enabled) {
447
- startResourceMonitoring();
448
- startReporting();
449
- startProfiling();
450
- }
451
- return {
452
- recordMetric,
453
- measure,
454
- measureAsync,
455
- addMetric,
456
- addAlertRule,
457
- generateReport,
458
- getStats,
459
- reset,
460
- start() {
461
- opts.enabled = true;
462
- startResourceMonitoring();
463
- startReporting();
464
- startProfiling();
465
- },
466
- stop() {
467
- opts.enabled = false;
468
- stopResourceMonitoring();
469
- stopReporting();
470
- return generateReport();
471
- }
472
- };
473
- }
474
- var performanceMonitor2 = createPerformanceMonitor();
475
-
476
- // ../core/src/core/object-utils.js
477
- function deepClone(obj, seen = /* @__PURE__ */ new WeakMap()) {
478
- if (obj === null || typeof obj !== "object") {
479
- return obj;
480
- }
481
- if (seen.has(obj)) {
482
- return seen.get(obj);
483
- }
484
- if (obj instanceof Date) {
485
- return new Date(obj.getTime());
486
- }
487
- if (obj instanceof RegExp) {
488
- return new RegExp(obj.source, obj.flags);
489
- }
490
- if (Array.isArray(obj)) {
491
- const clonedArray = [];
492
- seen.set(obj, clonedArray);
493
- for (let i = 0; i < obj.length; i++) {
494
- clonedArray[i] = deepClone(obj[i], seen);
495
- }
496
- return clonedArray;
497
- }
498
- if (typeof obj === "function") {
499
- return obj;
500
- }
501
- if (obj instanceof Map) {
502
- const clonedMap = /* @__PURE__ */ new Map();
503
- seen.set(obj, clonedMap);
504
- for (const [key, value] of obj) {
505
- clonedMap.set(deepClone(key, seen), deepClone(value, seen));
506
- }
507
- return clonedMap;
508
- }
509
- if (obj instanceof Set) {
510
- const clonedSet = /* @__PURE__ */ new Set();
511
- seen.set(obj, clonedSet);
512
- for (const value of obj) {
513
- clonedSet.add(deepClone(value, seen));
514
- }
515
- return clonedSet;
516
- }
517
- if (obj instanceof WeakMap) {
518
- return /* @__PURE__ */ new WeakMap();
519
- }
520
- if (obj instanceof WeakSet) {
521
- return /* @__PURE__ */ new WeakSet();
522
- }
523
- const clonedObj = {};
524
- seen.set(obj, clonedObj);
525
- if (obj.constructor && obj.constructor !== Object) {
526
- try {
527
- clonedObj.__proto__ = obj.__proto__;
528
- } catch {
529
- Object.setPrototypeOf(clonedObj, Object.getPrototypeOf(obj));
530
- }
531
- }
532
- for (const key in obj) {
533
- if (obj.hasOwnProperty(key)) {
534
- clonedObj[key] = deepClone(obj[key], seen);
535
- }
536
- }
537
- const descriptors = Object.getOwnPropertyDescriptors(obj);
538
- for (const key of Object.keys(descriptors)) {
539
- if (!descriptors[key].enumerable && descriptors[key].configurable) {
540
- try {
541
- Object.defineProperty(clonedObj, key, {
542
- ...descriptors[key],
543
- value: deepClone(descriptors[key].value, seen)
544
- });
545
- } catch {
546
- }
547
- }
548
- }
549
- return clonedObj;
550
- }
551
- function validateComponent(component, path2 = "root") {
552
- if (component === null || component === void 0) {
553
- throw new Error(`Invalid component at ${path2}: null or undefined`);
554
- }
555
- if (["string", "number", "boolean"].includes(typeof component)) {
556
- return true;
557
- }
558
- if (typeof component === "function") {
559
- return true;
560
- }
561
- if (Array.isArray(component)) {
562
- component.forEach((child, index) => {
563
- validateComponent(child, `${path2}[${index}]`);
564
- });
565
- return true;
566
- }
567
- if (typeof component === "object") {
568
- const keys = Object.keys(component);
569
- if (keys.length === 0) {
570
- throw new Error(`Empty object at ${path2}`);
571
- }
572
- keys.forEach((key) => {
573
- const value = component[key];
574
- if (!/^[a-zA-Z][a-zA-Z0-9-]*$/.test(key) && key !== "text") {
575
- console.warn(`Potentially invalid tag name at ${path2}: ${key}`);
576
- }
577
- if (value && typeof value === "object" && !Array.isArray(value)) {
578
- if (value.children) {
579
- validateComponent(value.children, `${path2}.${key}.children`);
580
- }
581
- } else if (value && typeof value !== "string" && typeof value !== "number" && typeof value !== "function") {
582
- throw new Error(`Invalid value type at ${path2}.${key}: ${typeof value}`);
583
- }
584
- });
585
- return true;
586
- }
587
- throw new Error(`Invalid component type at ${path2}: ${typeof component}`);
588
- }
589
- function isCoherentObject(obj) {
590
- if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
591
- return false;
592
- }
593
- const keys = Object.keys(obj);
594
- if (keys.length === 0) {
595
- return false;
596
- }
597
- return keys.every((key) => {
598
- if (key === "text") return true;
599
- return /^[a-zA-Z][a-zA-Z0-9-]*$/.test(key);
600
- });
601
- }
602
- function extractProps(coherentObj) {
603
- if (!isCoherentObject(coherentObj)) {
604
- return {};
605
- }
606
- const props = {};
607
- const keys = Object.keys(coherentObj);
608
- keys.forEach((tag) => {
609
- const value = coherentObj[tag];
610
- if (value && typeof value === "object" && !Array.isArray(value)) {
611
- props[tag] = { ...value };
612
- } else {
613
- props[tag] = { text: value };
614
- }
615
- });
616
- return props;
617
- }
618
- function hasChildren(component) {
619
- if (Array.isArray(component)) {
620
- return component.length > 0;
621
- }
622
- if (isCoherentObject(component)) {
623
- if (component.children !== void 0 && component.children !== null) {
624
- return Array.isArray(component.children) ? component.children.length > 0 : true;
625
- }
626
- const keys = Object.keys(component);
627
- return keys.some((key) => {
628
- const value = component[key];
629
- return value && typeof value === "object" && value.children;
630
- });
631
- }
632
- return false;
633
- }
634
- function normalizeChildren(children) {
635
- if (children === null || children === void 0) {
636
- return [];
637
- }
638
- if (Array.isArray(children)) {
639
- return children.flat().filter((child) => child !== null && child !== void 0);
640
- }
641
- return [children];
642
- }
643
-
644
- // ../core/src/components/component-system.js
645
- var COMPONENT_METADATA = /* @__PURE__ */ new WeakMap();
646
- var ComponentState = class {
647
- constructor(initialState = {}) {
648
- this.state = { ...initialState };
649
- this.listeners = /* @__PURE__ */ new Set();
650
- this.isUpdating = false;
651
- }
652
- /**
653
- * Get state value by key or entire state
654
- *
655
- * @param {string} [key] - State key to retrieve
656
- * @returns {*} State value or entire state object
657
- */
658
- get(key) {
659
- return key ? this.state[key] : { ...this.state };
660
- }
661
- /**
662
- * Update state with new values
663
- *
664
- * @param {Object} updates - State updates to apply
665
- * @returns {ComponentState} This instance for chaining
666
- */
667
- set(updates) {
668
- if (this.isUpdating) return this;
669
- const oldState = { ...this.state };
670
- if (typeof updates === "function") {
671
- updates = updates(oldState);
672
- }
673
- this.state = { ...this.state, ...updates };
674
- this.notifyListeners(oldState, this.state);
675
- return this;
676
- }
677
- subscribe(listener) {
678
- this.listeners.add(listener);
679
- return () => this.listeners.delete(listener);
680
- }
681
- notifyListeners(oldState, newState) {
682
- if (this.listeners.size === 0) return;
683
- this.isUpdating = true;
684
- this.listeners.forEach((listener) => {
685
- try {
686
- listener(newState, oldState);
687
- } catch (_error) {
688
- console.error("State listener _error:", _error);
689
- }
690
- });
691
- this.isUpdating = false;
692
- }
693
- };
694
- var Component = class _Component {
695
- constructor(definition = {}) {
696
- this.definition = definition;
697
- this.name = definition.name || "AnonymousComponent";
698
- this.props = {};
699
- this.state = new ComponentState(definition.state || {});
700
- this.children = [];
701
- this.parent = null;
702
- this.rendered = null;
703
- this.isMounted = false;
704
- this.isDestroyed = false;
705
- this.hooks = {
706
- beforeCreate: definition.beforeCreate || (() => {
707
- }),
708
- created: definition.created || (() => {
709
- }),
710
- beforeMount: definition.beforeMount || (() => {
711
- }),
712
- mounted: definition.mounted || (() => {
713
- }),
714
- beforeUpdate: definition.beforeUpdate || (() => {
715
- }),
716
- updated: definition.updated || (() => {
717
- }),
718
- beforeDestroy: definition.beforeDestroy || (() => {
719
- }),
720
- destroyed: definition.destroyed || (() => {
721
- }),
722
- errorCaptured: definition.errorCaptured || (() => {
723
- })
724
- };
725
- this.methods = definition.methods || {};
726
- Object.keys(this.methods).forEach((methodName) => {
727
- if (typeof this.methods[methodName] === "function") {
728
- this[methodName] = this.methods[methodName].bind(this);
729
- }
730
- });
731
- this.computed = definition.computed || {};
732
- this.computedCache = /* @__PURE__ */ new Map();
733
- this.watchers = definition.watch || {};
734
- this.setupWatchers();
735
- COMPONENT_METADATA.set(this, {
736
- createdAt: Date.now(),
737
- updateCount: 0,
738
- renderCount: 0
739
- });
740
- this.callHook("beforeCreate");
741
- this.initialize();
742
- this.callHook("created");
743
- }
744
- /**
745
- * Initialize component
746
- */
747
- initialize() {
748
- this.unsubscribeState = this.state.subscribe((newState, oldState) => {
749
- this.onStateChange(newState, oldState);
750
- });
751
- this.initializeComputed();
752
- }
753
- /**
754
- * Set up watchers for reactive data
755
- */
756
- setupWatchers() {
757
- Object.keys(this.watchers).forEach((key) => {
758
- const handler = this.watchers[key];
759
- this.state.subscribe((newState, oldState) => {
760
- if (newState[key] !== oldState[key]) {
761
- handler.call(this, newState[key], oldState[key]);
762
- }
763
- });
764
- });
765
- }
766
- /**
767
- * Initialize computed properties
768
- */
769
- initializeComputed() {
770
- Object.keys(this.computed).forEach((key) => {
771
- Object.defineProperty(this, key, {
772
- get: () => {
773
- if (!this.computedCache.has(key)) {
774
- const value = this.computed[key].call(this);
775
- this.computedCache.set(key, value);
776
- }
777
- return this.computedCache.get(key);
778
- },
779
- enumerable: true
780
- });
781
- });
782
- }
783
- /**
784
- * Handle state changes
785
- */
786
- onStateChange() {
787
- if (this.isDestroyed) return;
788
- this.computedCache.clear();
789
- if (this.isMounted) {
790
- this.update();
791
- }
792
- }
793
- /**
794
- * Call lifecycle hook
795
- */
796
- callHook(hookName, ...args) {
797
- try {
798
- if (this.hooks[hookName]) {
799
- return this.hooks[hookName].call(this, ...args);
800
- }
801
- } catch (_error) {
802
- this.handleError(_error, `${hookName} hook`);
803
- }
804
- }
805
- /**
806
- * Handle component errors
807
- */
808
- handleError(_error) {
809
- console.error(`Component Error in ${this.name}:`, _error);
810
- this.callHook("errorCaptured", _error);
811
- if (this.parent && this.parent.handleError) {
812
- this.parent.handleError(_error, `${this.name} -> ${context}`);
813
- }
814
- }
815
- /**
816
- * Render the component
817
- */
818
- render(props = {}) {
819
- if (this.isDestroyed) {
820
- console.warn(`Attempting to render destroyed component: ${this.name}`);
821
- return null;
822
- }
823
- try {
824
- const metadata = COMPONENT_METADATA.get(this);
825
- if (metadata) {
826
- metadata.renderCount++;
827
- }
828
- this.props = { ...props };
829
- if (typeof this.definition.render === "function") {
830
- this.rendered = this.definition.render.call(this, this.props, this.state.get());
831
- } else if (typeof this.definition.template !== "undefined") {
832
- this.rendered = this.processTemplate(this.definition.template, this.props, this.state.get());
833
- } else {
834
- throw new Error(`Component ${this.name} must have either render method or template`);
835
- }
836
- if (this.rendered !== null) {
837
- validateComponent(this.rendered, this.name);
838
- }
839
- return this.rendered;
840
- } catch (_error) {
841
- this.handleError(_error);
842
- return { div: { className: "component-_error", text: `Error in ${this.name}` } };
843
- }
844
- }
845
- /**
846
- * Process template with data
847
- */
848
- processTemplate(template, props, state) {
849
- if (typeof template === "function") {
850
- return template.call(this, props, state);
851
- }
852
- if (typeof template === "string") {
853
- return template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
854
- return props[key] || state[key] || "";
855
- });
856
- }
857
- const processed = deepClone(template);
858
- this.interpolateObject(processed, { ...props, ...state });
859
- return processed;
860
- }
861
- /**
862
- * Interpolate object with data
863
- */
864
- interpolateObject(obj, data) {
865
- if (typeof obj === "string") {
866
- return obj.replace(/\{\{(\w+)\}\}/g, (match, key) => data[key] || "");
867
- }
868
- if (Array.isArray(obj)) {
869
- return obj.map((item) => this.interpolateObject(item, data));
870
- }
871
- if (obj && typeof obj === "object") {
872
- Object.keys(obj).forEach((key) => {
873
- obj[key] = this.interpolateObject(obj[key], data);
874
- });
875
- }
876
- return obj;
877
- }
878
- /**
879
- * Mount the component
880
- */
881
- mount() {
882
- if (this.isMounted || this.isDestroyed) return this;
883
- this.callHook("beforeMount");
884
- this.isMounted = true;
885
- this.callHook("mounted");
886
- return this;
887
- }
888
- /**
889
- * Update the component
890
- */
891
- update() {
892
- if (!this.isMounted || this.isDestroyed) return this;
893
- const metadata = COMPONENT_METADATA.get(this);
894
- if (metadata) {
895
- metadata.updateCount++;
896
- }
897
- this.callHook("beforeUpdate");
898
- this.callHook("updated");
899
- return this;
900
- }
901
- /**
902
- * Destroy the component
903
- */
904
- destroy() {
905
- if (this.isDestroyed) return this;
906
- this.callHook("beforeDestroy");
907
- if (this.unsubscribeState) {
908
- this.unsubscribeState();
909
- }
910
- this.children.forEach((child) => {
911
- if (child.destroy) {
912
- child.destroy();
913
- }
914
- });
915
- this.isMounted = false;
916
- this.isDestroyed = true;
917
- this.children = [];
918
- this.parent = null;
919
- this.callHook("destroyed");
920
- return this;
921
- }
922
- /**
923
- * Get component metadata
924
- */
925
- getMetadata() {
926
- return COMPONENT_METADATA.get(this) || {};
927
- }
928
- /**
929
- * Clone component with new props/state
930
- */
931
- clone(overrides = {}) {
932
- const newDefinition = { ...this.definition, ...overrides };
933
- return new _Component(newDefinition);
934
- }
935
- };
936
- if (performanceMonitor2) {
937
- const originalRender = Component.prototype.render;
938
- Component.prototype.render = function(...args) {
939
- const start = performance.now();
940
- const result = originalRender.apply(this, args);
941
- const duration = performance.now() - start;
942
- performanceMonitor2.recordMetric("renderTime", duration, {
943
- type: "component",
944
- name: this.name,
945
- propsSize: JSON.stringify(this.props || {}).length,
946
- hasState: Object.keys(this.state?.get() || {}).length > 0
947
- });
948
- return result;
949
- };
950
- }
951
-
952
- // ../core/src/utils/error-handler.js
953
- var CoherentError = class _CoherentError extends Error {
954
- constructor(message, options = {}) {
955
- super(message);
956
- this.name = "CoherentError";
957
- this.type = options.type || "generic";
958
- this.component = options.component;
959
- this.context = options.context;
960
- this.suggestions = options.suggestions || [];
961
- this.timestamp = Date.now();
962
- if (Error.captureStackTrace) {
963
- Error.captureStackTrace(this, _CoherentError);
964
- }
965
- }
966
- toJSON() {
967
- return {
968
- name: this.name,
969
- message: this.message,
970
- type: this.type,
971
- component: this.component,
972
- context: this.context,
973
- suggestions: this.suggestions,
974
- timestamp: this.timestamp,
975
- stack: this.stack
976
- };
977
- }
978
- };
979
- var ComponentValidationError = class extends CoherentError {
980
- constructor(message, component, suggestions = []) {
981
- super(message, {
982
- type: "validation",
983
- component,
984
- suggestions: [
985
- "Check component structure and syntax",
986
- "Ensure all required properties are present",
987
- "Validate prop types and values",
988
- ...suggestions
989
- ]
990
- });
991
- this.name = "ComponentValidationError";
992
- }
993
- };
994
- var RenderingError = class extends CoherentError {
995
- constructor(message, component, context2, suggestions = []) {
996
- super(message, {
997
- type: "rendering",
998
- component,
999
- context: context2,
1000
- suggestions: [
1001
- "Check for circular references",
1002
- "Validate component depth",
1003
- "Ensure all functions return valid components",
1004
- ...suggestions
1005
- ]
1006
- });
1007
- this.name = "RenderingError";
1008
- }
1009
- };
1010
- var PerformanceError = class extends CoherentError {
1011
- constructor(message, metrics, suggestions = []) {
1012
- super(message, {
1013
- type: "performance",
1014
- context: metrics,
1015
- suggestions: [
1016
- "Consider component memoization",
1017
- "Reduce component complexity",
1018
- "Enable caching",
1019
- ...suggestions
1020
- ]
1021
- });
1022
- this.name = "PerformanceError";
1023
- }
1024
- };
1025
- var StateError = class extends CoherentError {
1026
- constructor(message, state, suggestions = []) {
1027
- super(message, {
1028
- type: "state",
1029
- context: state,
1030
- suggestions: [
1031
- "Check state mutations",
1032
- "Ensure proper state initialization",
1033
- "Validate state transitions",
1034
- ...suggestions
1035
- ]
1036
- });
1037
- this.name = "StateError";
1038
- }
1039
- };
1040
- var ErrorHandler = class {
1041
- constructor(options = {}) {
1042
- this.options = {
1043
- enableStackTrace: options.enableStackTrace !== false,
1044
- enableSuggestions: options.enableSuggestions !== false,
1045
- enableLogging: options.enableLogging !== false,
1046
- logLevel: options.logLevel || "_error",
1047
- maxErrorHistory: options.maxErrorHistory || 100,
1048
- ...options
1049
- };
1050
- this.errorHistory = [];
1051
- this.errorCounts = /* @__PURE__ */ new Map();
1052
- this.suppressedErrors = /* @__PURE__ */ new Set();
1053
- }
1054
- /**
1055
- * Handle and report errors with detailed context
1056
- */
1057
- handle(_error, context2 = {}) {
1058
- const enhancedError = this.enhanceError(_error, context2);
1059
- this.addToHistory(enhancedError);
1060
- if (this.options.enableLogging) {
1061
- this.logError(enhancedError);
1062
- }
1063
- return enhancedError;
1064
- }
1065
- /**
1066
- * Enhance existing errors with more context
1067
- */
1068
- enhanceError(_error, context2 = {}) {
1069
- if (_error instanceof CoherentError) {
1070
- return _error;
1071
- }
1072
- const errorType = this.classifyError(_error, context2);
1073
- switch (errorType) {
1074
- case "validation":
1075
- return new ComponentValidationError(
1076
- _error.message,
1077
- context2.component,
1078
- this.generateSuggestions(_error, context2)
1079
- );
1080
- case "rendering":
1081
- return new RenderingError(
1082
- _error.message,
1083
- context2.component,
1084
- context2.renderContext,
1085
- this.generateSuggestions(_error, context2)
1086
- );
1087
- case "performance":
1088
- return new PerformanceError(
1089
- _error.message,
1090
- context2.metrics,
1091
- this.generateSuggestions(_error, context2)
1092
- );
1093
- case "state":
1094
- return new StateError(
1095
- _error.message,
1096
- context2.state,
1097
- this.generateSuggestions(_error, context2)
1098
- );
1099
- default:
1100
- return new CoherentError(_error.message, {
1101
- type: errorType,
1102
- component: context2.component,
1103
- context: context2.context,
1104
- suggestions: this.generateSuggestions(_error, context2)
1105
- });
1106
- }
1107
- }
1108
- /**
1109
- * Classify _error type based on message and context
1110
- */
1111
- classifyError(_error, context2) {
1112
- const message = _error.message.toLowerCase();
1113
- if (message.includes("invalid") || message.includes("validation") || message.includes("required") || message.includes("type")) {
1114
- return "validation";
1115
- }
1116
- if (message.includes("render") || message.includes("circular") || message.includes("depth") || message.includes("cannot render")) {
1117
- return "rendering";
1118
- }
1119
- if (message.includes("slow") || message.includes("memory") || message.includes("performance") || message.includes("timeout")) {
1120
- return "performance";
1121
- }
1122
- if (message.includes("state") || message.includes("mutation") || message.includes("store") || context2.state) {
1123
- return "state";
1124
- }
1125
- if (context2.component) return "validation";
1126
- if (context2.renderContext) return "rendering";
1127
- if (context2.metrics) return "performance";
1128
- return "generic";
1129
- }
1130
- /**
1131
- * Generate helpful suggestions based on _error
1132
- */
1133
- generateSuggestions(_error, context2 = {}) {
1134
- const suggestions = [];
1135
- const message = _error.message.toLowerCase();
1136
- const patterns = [
1137
- {
1138
- pattern: /cannot render|render.*failed/,
1139
- suggestions: [
1140
- "Check if component returns a valid object structure",
1141
- "Ensure all properties are properly defined",
1142
- "Look for undefined variables or null references"
1143
- ]
1144
- },
1145
- {
1146
- pattern: /circular.*reference/,
1147
- suggestions: [
1148
- "Remove circular references between components",
1149
- "Use lazy loading or memoization to break cycles",
1150
- "Check for self-referencing components"
1151
- ]
1152
- },
1153
- {
1154
- pattern: /maximum.*depth/,
1155
- suggestions: [
1156
- "Reduce component nesting depth",
1157
- "Break complex components into smaller parts",
1158
- "Check for infinite recursion in component functions"
1159
- ]
1160
- },
1161
- {
1162
- pattern: /invalid.*component/,
1163
- suggestions: [
1164
- "Ensure component follows the expected object structure",
1165
- "Check property names and values for typos",
1166
- "Verify component is not null or undefined"
1167
- ]
1168
- },
1169
- {
1170
- pattern: /performance|slow|timeout/,
1171
- suggestions: [
1172
- "Enable component caching",
1173
- "Use memoization for expensive operations",
1174
- "Reduce component complexity",
1175
- "Consider lazy loading for large components"
1176
- ]
1177
- }
1178
- ];
1179
- patterns.forEach(({ pattern, suggestions: patternSuggestions }) => {
1180
- if (pattern.test(message)) {
1181
- suggestions.push(...patternSuggestions);
1182
- }
1183
- });
1184
- if (context2.component) {
1185
- const componentType = typeof context2.component;
1186
- if (componentType === "function") {
1187
- suggestions.push("Check function component return value");
1188
- } else if (componentType === "object" && context2.component === null) {
1189
- suggestions.push("Component is null - ensure proper initialization");
1190
- } else if (Array.isArray(context2.component)) {
1191
- suggestions.push("Arrays should contain valid component objects");
1192
- }
1193
- }
1194
- if (suggestions.length === 0) {
1195
- suggestions.push(
1196
- "Enable development tools for more detailed debugging",
1197
- "Check browser console for additional _error details",
1198
- "Use component validation tools to identify issues"
1199
- );
1200
- }
1201
- return [...new Set(suggestions)];
1202
- }
1203
- /**
1204
- * Add _error to history with deduplication
1205
- */
1206
- addToHistory(_error) {
1207
- const errorKey = `${_error.name}:${_error.message}`;
1208
- this.errorCounts.set(errorKey, (this.errorCounts.get(errorKey) || 0) + 1);
1209
- const historyEntry = {
1210
- ..._error.toJSON(),
1211
- count: this.errorCounts.get(errorKey),
1212
- firstSeen: this.errorHistory.find((e) => e.key === errorKey)?.firstSeen || _error.timestamp,
1213
- key: errorKey
1214
- };
1215
- this.errorHistory = this.errorHistory.filter((e) => e.key !== errorKey);
1216
- this.errorHistory.unshift(historyEntry);
1217
- if (this.errorHistory.length > this.options.maxErrorHistory) {
1218
- this.errorHistory = this.errorHistory.slice(0, this.options.maxErrorHistory);
1219
- }
1220
- }
1221
- /**
1222
- * Log _error with enhanced formatting
1223
- */
1224
- logError(_error) {
1225
- if (this.suppressedErrors.has(`${_error.name}:${_error.message}`)) {
1226
- return;
1227
- }
1228
- const isRepeated = this.errorCounts.get(`${_error.name}:${_error.message}`) > 1;
1229
- const errorGroup = `\u{1F6A8} ${_error.name}${isRepeated ? ` (\xD7${this.errorCounts.get(`${_error.name}:${_error.message}`)})` : ""}`;
1230
- console.group(errorGroup);
1231
- console.error(`\u274C ${_error.message}`);
1232
- if (_error.component) {
1233
- console.log("\u{1F50D} Component:", this.formatComponent(_error.component));
1234
- }
1235
- if (_error.context) {
1236
- console.log("\u{1F4CB} Context:", _error.context);
1237
- }
1238
- if (this.options.enableSuggestions && _error.suggestions.length > 0) {
1239
- console.group("\u{1F4A1} Suggestions:");
1240
- _error.suggestions.forEach((suggestion, index) => {
1241
- console.log(`${index + 1}. ${suggestion}`);
1242
- });
1243
- console.groupEnd();
1244
- }
1245
- if (this.options.enableStackTrace && _error.stack) {
1246
- console.log("\u{1F4DA} Stack trace:", _error.stack);
1247
- }
1248
- console.groupEnd();
1249
- }
1250
- /**
1251
- * Format component for logging
1252
- */
1253
- formatComponent(component, maxDepth = 2, currentDepth = 0) {
1254
- if (currentDepth > maxDepth) {
1255
- return "[...deep]";
1256
- }
1257
- if (typeof component === "function") {
1258
- return `[Function: ${component.name || "anonymous"}]`;
1259
- }
1260
- if (Array.isArray(component)) {
1261
- return component.slice(0, 3).map(
1262
- (item) => this.formatComponent(item, maxDepth, currentDepth + 1)
1263
- );
1264
- }
1265
- if (component && typeof component === "object") {
1266
- const formatted = {};
1267
- const keys = Object.keys(component).slice(0, 5);
1268
- for (const key of keys) {
1269
- if (key === "children" && component[key]) {
1270
- formatted[key] = this.formatComponent(component[key], maxDepth, currentDepth + 1);
1271
- } else {
1272
- formatted[key] = component[key];
1273
- }
1274
- }
1275
- if (Object.keys(component).length > 5) {
1276
- formatted["..."] = `(${Object.keys(component).length - 5} more)`;
1277
- }
1278
- return formatted;
1279
- }
1280
- return component;
1281
- }
1282
- /**
1283
- * Suppress specific _error types
1284
- */
1285
- suppress(errorPattern) {
1286
- this.suppressedErrors.add(errorPattern);
1287
- }
1288
- /**
1289
- * Clear _error history
1290
- */
1291
- clearHistory() {
1292
- this.errorHistory = [];
1293
- this.errorCounts.clear();
1294
- }
1295
- /**
1296
- * Get _error statistics
1297
- */
1298
- getStats() {
1299
- const errorsByType = {};
1300
- const errorsByTime = {};
1301
- this.errorHistory.forEach((_error) => {
1302
- errorsByType[_error.type] = (errorsByType[_error.type] || 0) + _error.count;
1303
- const hour = new Date(_error.timestamp).toISOString().slice(0, 13);
1304
- errorsByTime[hour] = (errorsByTime[hour] || 0) + _error.count;
1305
- });
1306
- return {
1307
- totalErrors: this.errorHistory.reduce((sum, e) => sum + e.count, 0),
1308
- uniqueErrors: this.errorHistory.length,
1309
- errorsByType,
1310
- errorsByTime,
1311
- mostCommonErrors: this.getMostCommonErrors(5),
1312
- recentErrors: this.errorHistory.slice(0, 10)
1313
- };
1314
- }
1315
- /**
1316
- * Get most common errors
1317
- */
1318
- getMostCommonErrors(limit = 10) {
1319
- return this.errorHistory.sort((a, b) => b.count - a.count).slice(0, limit).map(({ name, message, count, type }) => ({
1320
- name,
1321
- message,
1322
- count,
1323
- type
1324
- }));
1325
- }
1326
- };
1327
- var globalErrorHandler = new ErrorHandler();
1328
-
1329
- // ../core/src/components/lifecycle.js
1330
- var LIFECYCLE_PHASES = {
1331
- BEFORE_CREATE: "beforeCreate",
1332
- CREATED: "created",
1333
- BEFORE_MOUNT: "beforeMount",
1334
- MOUNTED: "mounted",
1335
- BEFORE_UPDATE: "beforeUpdate",
1336
- UPDATED: "updated",
1337
- BEFORE_UNMOUNT: "beforeUnmount",
1338
- UNMOUNTED: "unmounted",
1339
- ERROR: "_error"
1340
- };
1341
- var componentInstances = /* @__PURE__ */ new WeakMap();
1342
- var ComponentEventSystem = class {
1343
- constructor() {
1344
- this.events = /* @__PURE__ */ new Map();
1345
- this.globalHandlers = /* @__PURE__ */ new Map();
1346
- }
1347
- /**
1348
- * Emit event to component or globally
1349
- */
1350
- emit(eventName, data = {}, target = null) {
1351
- const event = {
1352
- name: eventName,
1353
- data,
1354
- target,
1355
- timestamp: Date.now(),
1356
- stopped: false,
1357
- preventDefault: false
1358
- };
1359
- if (target) {
1360
- const instance = componentInstances.get(target);
1361
- if (instance) {
1362
- this._notifyHandlers(instance.id, event);
1363
- }
1364
- } else {
1365
- this._notifyGlobalHandlers(event);
1366
- }
1367
- return event;
1368
- }
1369
- /**
1370
- * Listen for events on component or globally
1371
- */
1372
- on(eventName, handler, componentId = null) {
1373
- if (componentId) {
1374
- if (!this.events.has(componentId)) {
1375
- this.events.set(componentId, /* @__PURE__ */ new Map());
1376
- }
1377
- const componentEvents = this.events.get(componentId);
1378
- if (!componentEvents.has(eventName)) {
1379
- componentEvents.set(eventName, /* @__PURE__ */ new Set());
1380
- }
1381
- componentEvents.get(eventName).add(handler);
1382
- } else {
1383
- if (!this.globalHandlers.has(eventName)) {
1384
- this.globalHandlers.set(eventName, /* @__PURE__ */ new Set());
1385
- }
1386
- this.globalHandlers.get(eventName).add(handler);
1387
- }
1388
- return () => this.off(eventName, handler, componentId);
1389
- }
1390
- /**
1391
- * Remove event handler
1392
- */
1393
- off(eventName, handler, componentId = null) {
1394
- if (componentId) {
1395
- const componentEvents = this.events.get(componentId);
1396
- if (componentEvents && componentEvents.has(eventName)) {
1397
- componentEvents.get(eventName).delete(handler);
1398
- if (componentEvents.get(eventName).size === 0) {
1399
- componentEvents.delete(eventName);
1400
- if (componentEvents.size === 0) {
1401
- this.events.delete(componentId);
1402
- }
1403
- }
1404
- }
1405
- } else {
1406
- const handlers = this.globalHandlers.get(eventName);
1407
- if (handlers) {
1408
- handlers.delete(handler);
1409
- if (handlers.size === 0) {
1410
- this.globalHandlers.delete(eventName);
1411
- }
1412
- }
1413
- }
1414
- }
1415
- /**
1416
- * Listen once
1417
- */
1418
- once(eventName, handler, componentId = null) {
1419
- const onceHandler = (event) => {
1420
- handler(event);
1421
- this.off(eventName, onceHandler, componentId);
1422
- };
1423
- return this.on(eventName, onceHandler, componentId);
1424
- }
1425
- /**
1426
- * Notify component handlers
1427
- */
1428
- _notifyHandlers(componentId, event) {
1429
- const componentEvents = this.events.get(componentId);
1430
- if (componentEvents && componentEvents.has(event.name)) {
1431
- const handlers = componentEvents.get(event.name);
1432
- for (const handler of handlers) {
1433
- if (event.stopped) break;
1434
- try {
1435
- handler(event);
1436
- } catch (_error) {
1437
- globalErrorHandler.handle(_error, {
1438
- type: "event-handler-_error",
1439
- context: { event, handler: handler.toString() }
1440
- });
1441
- }
1442
- }
1443
- }
1444
- }
1445
- /**
1446
- * Notify global handlers
1447
- */
1448
- _notifyGlobalHandlers(event) {
1449
- const handlers = this.globalHandlers.get(event.name);
1450
- if (handlers) {
1451
- for (const handler of handlers) {
1452
- if (event.stopped) break;
1453
- try {
1454
- handler(event);
1455
- } catch (_error) {
1456
- globalErrorHandler.handle(_error, {
1457
- type: "global-event-handler-_error",
1458
- context: { event, handler: handler.toString() }
1459
- });
1460
- }
1461
- }
1462
- }
1463
- }
1464
- /**
1465
- * Clean up events for a component
1466
- */
1467
- cleanup(componentId) {
1468
- this.events.delete(componentId);
1469
- }
1470
- /**
1471
- * Get event statistics
1472
- */
1473
- getStats() {
1474
- return {
1475
- componentEvents: this.events.size,
1476
- globalEvents: this.globalHandlers.size,
1477
- totalHandlers: Array.from(this.events.values()).reduce((sum, events) => {
1478
- return sum + Array.from(events.values()).reduce((eventSum, handlers) => {
1479
- return eventSum + handlers.size;
1480
- }, 0);
1481
- }, 0) + Array.from(this.globalHandlers.values()).reduce((sum, handlers) => {
1482
- return sum + handlers.size;
1483
- }, 0)
1484
- };
1485
- }
1486
- };
1487
- var eventSystem = new ComponentEventSystem();
1488
- function createLifecycleHooks() {
1489
- const hooks = {};
1490
- Object.values(LIFECYCLE_PHASES).forEach((phase) => {
1491
- hooks[phase] = (callback) => {
1492
- const instance = getCurrentInstance();
1493
- if (instance) {
1494
- instance.hook(phase, callback);
1495
- }
1496
- };
1497
- });
1498
- return hooks;
1499
- }
1500
- var currentInstance = null;
1501
- function getCurrentInstance() {
1502
- return currentInstance;
1503
- }
1504
- var useHooks = createLifecycleHooks();
1505
-
1506
- // ../core/src/events/event-bus.js
1507
- function throttle(func, delay) {
1508
- let lastCall = 0;
1509
- let timeoutId = null;
1510
- return function throttled(...args) {
1511
- const now = Date.now();
1512
- const timeSinceLastCall = now - lastCall;
1513
- if (timeSinceLastCall >= delay) {
1514
- lastCall = now;
1515
- return func.apply(this, args);
1516
- } else {
1517
- if (timeoutId) clearTimeout(timeoutId);
1518
- timeoutId = setTimeout(() => {
1519
- lastCall = Date.now();
1520
- func.apply(this, args);
1521
- }, delay - timeSinceLastCall);
1522
- }
1523
- };
1524
- }
1525
- var EventBus = class {
1526
- constructor(options = {}) {
1527
- this.listeners = /* @__PURE__ */ new Map();
1528
- this.handlers = /* @__PURE__ */ new Map();
1529
- this.actionHandlers = /* @__PURE__ */ new Map();
1530
- this.middleware = [];
1531
- this.throttledEmitters = /* @__PURE__ */ new Map();
1532
- this.debouncedEmitters = /* @__PURE__ */ new Map();
1533
- this.options = {
1534
- debug: false,
1535
- performance: true,
1536
- maxListeners: 100,
1537
- enableWildcards: true,
1538
- enableAsync: true,
1539
- wildcardSeparator: ":",
1540
- enablePriority: true,
1541
- defaultPriority: 0,
1542
- errorHandler: null,
1543
- filters: {
1544
- allowList: null,
1545
- // null means allow all
1546
- blockList: []
1547
- },
1548
- throttle: {
1549
- enabled: false,
1550
- defaultDelay: 100,
1551
- events: {}
1552
- },
1553
- batching: {
1554
- enabled: false,
1555
- maxBatchSize: 10,
1556
- flushInterval: 16
1557
- },
1558
- ...options
1559
- };
1560
- this.stats = {
1561
- eventsEmitted: 0,
1562
- listenersExecuted: 0,
1563
- errorsOccurred: 0,
1564
- averageEmitTime: 0,
1565
- throttledEvents: 0,
1566
- filteredEvents: 0
1567
- };
1568
- if (this.options.batching.enabled) {
1569
- this.batchQueue = [];
1570
- this.batchTimer = null;
1571
- }
1572
- if (this.options.debug) {
1573
- this.use((event, data, next) => {
1574
- console.log(`[EventBus] ${event}:`, data);
1575
- next();
1576
- });
1577
- }
1578
- }
1579
- /**
1580
- * Add middleware
1581
- */
1582
- use(middleware) {
1583
- if (typeof middleware !== "function") {
1584
- throw new Error("Middleware must be a function");
1585
- }
1586
- this.middleware.push(middleware);
1587
- return this;
1588
- }
1589
- /**
1590
- * Check if event passes filters
1591
- */
1592
- passesFilters(event) {
1593
- const { allowList, blockList } = this.options.filters;
1594
- if (blockList && blockList.length > 0) {
1595
- for (const pattern of blockList) {
1596
- if (this.matchPattern(pattern, event)) {
1597
- this.stats.filteredEvents++;
1598
- return false;
1599
- }
1600
- }
1601
- }
1602
- if (allowList && allowList.length > 0) {
1603
- for (const pattern of allowList) {
1604
- if (this.matchPattern(pattern, event)) {
1605
- return true;
1606
- }
1607
- }
1608
- this.stats.filteredEvents++;
1609
- return false;
1610
- }
1611
- return true;
1612
- }
1613
- /**
1614
- * Match event against pattern
1615
- */
1616
- matchPattern(pattern, event) {
1617
- const sep = this.options.wildcardSeparator;
1618
- const patternParts = pattern.split(sep);
1619
- const eventParts = event.split(sep);
1620
- if (pattern.includes("*")) {
1621
- if (patternParts.length !== eventParts.length) {
1622
- return false;
1623
- }
1624
- return patternParts.every((part, i) => part === "*" || part === eventParts[i]);
1625
- }
1626
- return pattern === event;
1627
- }
1628
- /**
1629
- * Emit an event
1630
- */
1631
- async emit(event, data = null) {
1632
- if (!this.passesFilters(event)) {
1633
- if (this.options.debug) {
1634
- console.warn(`[EventBus] Event filtered: ${event}`);
1635
- }
1636
- return;
1637
- }
1638
- if (this.options.batching.enabled) {
1639
- return this.addToBatch(event, data);
1640
- }
1641
- if (this.options.throttle.enabled) {
1642
- const throttleDelay = this.options.throttle.events && this.options.throttle.events[event] || this.options.throttle.defaultDelay;
1643
- if (throttleDelay > 0) {
1644
- return this.emitThrottled(event, data, throttleDelay);
1645
- }
1646
- }
1647
- return this.emitImmediate(event, data);
1648
- }
1649
- /**
1650
- * Emit immediately without throttling
1651
- */
1652
- async emitImmediate(event, data = null) {
1653
- const startTime = this.options.performance ? performance.now() : 0;
1654
- try {
1655
- await this.runMiddleware(event, data);
1656
- const listeners = this.getEventListeners(event);
1657
- if (listeners.length === 0) {
1658
- if (this.options.debug) {
1659
- console.warn(`[EventBus] No listeners for event: ${event}`);
1660
- }
1661
- return;
1662
- }
1663
- const promises = listeners.map(
1664
- (listenerObj) => this.executeListener(listenerObj.listener, event, data, listenerObj.options)
1665
- );
1666
- if (this.options.enableAsync) {
1667
- await Promise.allSettled(promises);
1668
- } else {
1669
- for (const promise of promises) {
1670
- await promise;
1671
- }
1672
- }
1673
- this.stats.eventsEmitted++;
1674
- this.stats.listenersExecuted += listeners.length;
1675
- } catch (error) {
1676
- this.stats.errorsOccurred++;
1677
- this.handleError(error, event, data);
1678
- } finally {
1679
- if (this.options.performance) {
1680
- const duration = performance.now() - startTime;
1681
- this.updatePerformanceStats(duration);
1682
- }
1683
- }
1684
- }
1685
- /**
1686
- * Emit with throttling
1687
- */
1688
- emitThrottled(event, data, delay) {
1689
- if (!this.throttledEmitters.has(event)) {
1690
- const throttled = throttle((evt, d) => this.emitImmediate(evt, d), delay);
1691
- this.throttledEmitters.set(event, throttled);
1692
- }
1693
- this.stats.throttledEvents++;
1694
- return this.throttledEmitters.get(event)(event, data);
1695
- }
1696
- /**
1697
- * Add event to batch queue
1698
- */
1699
- addToBatch(event, data) {
1700
- this.batchQueue.push({ event, data, timestamp: Date.now() });
1701
- if (this.batchQueue.length >= this.options.batching.maxBatchSize) {
1702
- this.flushBatch();
1703
- } else if (!this.batchTimer) {
1704
- this.batchTimer = setTimeout(() => {
1705
- this.flushBatch();
1706
- }, this.options.batching.flushInterval);
1707
- }
1708
- }
1709
- /**
1710
- * Flush batch queue
1711
- */
1712
- async flushBatch() {
1713
- if (this.batchTimer) {
1714
- clearTimeout(this.batchTimer);
1715
- this.batchTimer = null;
1716
- }
1717
- const batch = this.batchQueue.splice(0);
1718
- for (const { event, data } of batch) {
1719
- await this.emitImmediate(event, data);
1720
- }
1721
- }
1722
- /**
1723
- * Register event listener with options
1724
- */
1725
- on(event, listener, options = {}) {
1726
- if (typeof listener !== "function") {
1727
- throw new Error("Listener must be a function");
1728
- }
1729
- if (!this.listeners.has(event)) {
1730
- this.listeners.set(event, []);
1731
- }
1732
- const listeners = this.listeners.get(event);
1733
- if (listeners.length >= this.options.maxListeners) {
1734
- console.warn(`[EventBus] Max listeners (${this.options.maxListeners}) reached for event: ${event}`);
1735
- }
1736
- const listenerId = this.generateListenerId(event);
1737
- const listenerObj = {
1738
- listener,
1739
- listenerId,
1740
- priority: options.priority !== void 0 ? options.priority : this.options.defaultPriority,
1741
- condition: options.condition || null,
1742
- timeout: options.timeout || null,
1743
- options
1744
- };
1745
- listener.__listenerId = listenerId;
1746
- listener.__event = event;
1747
- if (this.options.enablePriority) {
1748
- const insertIndex = listeners.findIndex((l) => l.priority < listenerObj.priority);
1749
- if (insertIndex === -1) {
1750
- listeners.push(listenerObj);
1751
- } else {
1752
- listeners.splice(insertIndex, 0, listenerObj);
1753
- }
1754
- } else {
1755
- listeners.push(listenerObj);
1756
- }
1757
- return listenerId;
1758
- }
1759
- /**
1760
- * Register one-time listener
1761
- */
1762
- once(event, listener, options = {}) {
1763
- const onceListener = (...args) => {
1764
- this.off(event, onceListener.__listenerId);
1765
- return listener.call(this, ...args);
1766
- };
1767
- if (options.timeout) {
1768
- const timeoutId = setTimeout(() => {
1769
- this.off(event, onceListener.__listenerId);
1770
- if (this.options.debug) {
1771
- console.warn(`[EventBus] Listener timeout for event: ${event}`);
1772
- }
1773
- }, options.timeout);
1774
- onceListener.__cleanup = () => clearTimeout(timeoutId);
1775
- }
1776
- return this.on(event, onceListener, options);
1777
- }
1778
- /**
1779
- * Remove listener
1780
- */
1781
- off(event, listenerId) {
1782
- if (!this.listeners.has(event)) {
1783
- return false;
1784
- }
1785
- const listeners = this.listeners.get(event);
1786
- const index = listeners.findIndex((l) => l.listenerId === listenerId);
1787
- if (index !== -1) {
1788
- const listenerObj = listeners[index];
1789
- if (listenerObj.listener.__cleanup) {
1790
- listenerObj.listener.__cleanup();
1791
- }
1792
- listeners.splice(index, 1);
1793
- if (listeners.length === 0) {
1794
- this.listeners.delete(event);
1795
- }
1796
- return true;
1797
- }
1798
- return false;
1799
- }
1800
- /**
1801
- * Remove all listeners for an event
1802
- */
1803
- removeAllListeners(event) {
1804
- if (event) {
1805
- this.listeners.delete(event);
1806
- } else {
1807
- this.listeners.clear();
1808
- }
1809
- }
1810
- /**
1811
- * Get event listeners with wildcard support
1812
- */
1813
- getEventListeners(event) {
1814
- const listeners = [];
1815
- if (this.listeners.has(event)) {
1816
- listeners.push(...this.listeners.get(event));
1817
- }
1818
- if (this.options.enableWildcards) {
1819
- for (const [pattern, patternListeners] of this.listeners) {
1820
- if (pattern.includes("*") && this.matchPattern(pattern, event)) {
1821
- listeners.push(...patternListeners);
1822
- }
1823
- }
1824
- }
1825
- if (this.options.enablePriority) {
1826
- listeners.sort((a, b) => b.priority - a.priority);
1827
- }
1828
- return listeners;
1829
- }
1830
- /**
1831
- * Execute listener with options
1832
- */
1833
- async executeListener(listener, event, data, options = {}) {
1834
- try {
1835
- if (options.condition && !options.condition(data)) {
1836
- return;
1837
- }
1838
- const result = listener.call(this, data, event);
1839
- if (result && typeof result.then === "function") {
1840
- await result;
1841
- }
1842
- return result;
1843
- } catch (error) {
1844
- this.handleError(error, event, data);
1845
- }
1846
- }
1847
- /**
1848
- * Run middleware chain
1849
- */
1850
- async runMiddleware(event, data) {
1851
- if (this.middleware.length === 0) return;
1852
- let index = 0;
1853
- const next = async () => {
1854
- if (index < this.middleware.length) {
1855
- const middleware = this.middleware[index++];
1856
- await middleware(event, data, next);
1857
- }
1858
- };
1859
- await next();
1860
- }
1861
- /**
1862
- * Handle errors
1863
- */
1864
- handleError(error, event, data) {
1865
- if (this.options.errorHandler) {
1866
- this.options.errorHandler(error, event, data);
1867
- } else if (this.options.debug) {
1868
- console.error(`[EventBus] Error in event ${event}:`, error, data);
1869
- }
1870
- this.emitSync("eventbus:error", { error, event, data });
1871
- }
1872
- /**
1873
- * Synchronous emit
1874
- */
1875
- emitSync(event, data = null) {
1876
- try {
1877
- const listeners = this.getEventListeners(event);
1878
- listeners.forEach((listenerObj) => {
1879
- try {
1880
- if (!listenerObj.options.condition || listenerObj.options.condition(data)) {
1881
- listenerObj.listener.call(this, data, event);
1882
- }
1883
- } catch (error) {
1884
- this.handleError(error, event, data);
1885
- }
1886
- });
1887
- this.stats.eventsEmitted++;
1888
- this.stats.listenersExecuted += listeners.length;
1889
- } catch (error) {
1890
- this.stats.errorsOccurred++;
1891
- this.handleError(error, event, data);
1892
- }
1893
- }
1894
- /**
1895
- * Register action handler
1896
- */
1897
- registerAction(action, handler) {
1898
- if (typeof handler !== "function") {
1899
- throw new Error("Action handler must be a function");
1900
- }
1901
- this.actionHandlers.set(action, handler);
1902
- if (this.options.debug) {
1903
- console.log(`[EventBus] Registered action: ${action}`);
1904
- }
1905
- }
1906
- /**
1907
- * Register multiple actions
1908
- */
1909
- registerActions(actions) {
1910
- Object.entries(actions).forEach(([action, handler]) => {
1911
- this.registerAction(action, handler);
1912
- });
1913
- }
1914
- /**
1915
- * Get registered actions
1916
- */
1917
- getRegisteredActions() {
1918
- return Array.from(this.actionHandlers.keys());
1919
- }
1920
- /**
1921
- * Handle action event (called by DOM integration)
1922
- */
1923
- handleAction(action, element, event, data) {
1924
- const handler = this.actionHandlers.get(action);
1925
- if (!handler) {
1926
- if (this.options.debug) {
1927
- console.warn(`[EventBus] No handler registered for action: ${action}`);
1928
- }
1929
- return;
1930
- }
1931
- try {
1932
- handler.call(element, {
1933
- element,
1934
- event,
1935
- data,
1936
- emit: this.emit.bind(this),
1937
- emitSync: this.emitSync.bind(this)
1938
- });
1939
- } catch (error) {
1940
- this.handleError(error, `action:${action}`, { element, event, data });
1941
- }
1942
- }
1943
- /**
1944
- * Generate unique listener ID
1945
- */
1946
- generateListenerId(event) {
1947
- return `${event}_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
1948
- }
1949
- /**
1950
- * Update performance stats
1951
- */
1952
- updatePerformanceStats(duration) {
1953
- const count = this.stats.eventsEmitted;
1954
- this.stats.averageEmitTime = (this.stats.averageEmitTime * (count - 1) + duration) / count;
1955
- }
1956
- /**
1957
- * Get statistics
1958
- */
1959
- getStats() {
1960
- return { ...this.stats };
1961
- }
1962
- /**
1963
- * Reset statistics
1964
- */
1965
- resetStats() {
1966
- this.stats = {
1967
- eventsEmitted: 0,
1968
- listenersExecuted: 0,
1969
- errorsOccurred: 0,
1970
- averageEmitTime: 0,
1971
- throttledEvents: 0,
1972
- filteredEvents: 0
1973
- };
1974
- }
1975
- /**
1976
- * Destroy event bus
1977
- */
1978
- destroy() {
1979
- this.removeAllListeners();
1980
- this.actionHandlers.clear();
1981
- this.handlers.clear();
1982
- this.middleware = [];
1983
- this.throttledEmitters.clear();
1984
- this.debouncedEmitters.clear();
1985
- if (this.batchTimer) {
1986
- clearTimeout(this.batchTimer);
1987
- }
1988
- }
1989
- };
1990
- function createEventBus(options = {}) {
1991
- return new EventBus(options);
1992
- }
1993
- var globalEventBus = createEventBus();
1994
- var emit = globalEventBus.emit.bind(globalEventBus);
1995
- var emitSync = globalEventBus.emitSync.bind(globalEventBus);
1996
- var on = globalEventBus.on.bind(globalEventBus);
1997
- var once = globalEventBus.once.bind(globalEventBus);
1998
- var off = globalEventBus.off.bind(globalEventBus);
1999
- var registerAction = globalEventBus.registerAction.bind(globalEventBus);
2000
- var handleAction = globalEventBus.handleAction.bind(globalEventBus);
2001
-
2002
- // ../core/src/events/dom-integration.js
2003
- var DOMEventIntegration = class {
2004
- constructor(eventBus = globalEventBus, options = {}) {
2005
- this.eventBus = eventBus;
2006
- this.options = {
2007
- debug: false,
2008
- debounceDelay: 150,
2009
- throttleDelay: 100,
2010
- enableDelegation: true,
2011
- enableDebounce: true,
2012
- enableThrottle: false,
2013
- ...options
2014
- };
2015
- this.boundHandlers = /* @__PURE__ */ new Map();
2016
- this.activeElement = null;
2017
- this.isInitialized = false;
2018
- this.handleClick = this.handleClick.bind(this);
2019
- this.handleChange = this.handleChange.bind(this);
2020
- this.handleInput = this.handleInput.bind(this);
2021
- this.handleSubmit = this.handleSubmit.bind(this);
2022
- this.handleKeydown = this.handleKeydown.bind(this);
2023
- this.handleFocus = this.handleFocus.bind(this);
2024
- this.handleBlur = this.handleBlur.bind(this);
2025
- }
2026
- /**
2027
- * Initialize DOM event listeners
2028
- * @param {HTMLElement} rootElement - Root element to attach listeners to (default: document)
2029
- */
2030
- initialize(rootElement = document) {
2031
- if (this.isInitialized) {
2032
- console.warn("[DOMEventIntegration] Already initialized");
2033
- return;
2034
- }
2035
- if (typeof window === "undefined" || !rootElement) {
2036
- console.warn("[DOMEventIntegration] Cannot initialize: no DOM environment");
2037
- return;
2038
- }
2039
- this.rootElement = rootElement;
2040
- this.setupDOMEventListeners();
2041
- this.isInitialized = true;
2042
- if (this.options.debug) {
2043
- console.log("[DOMEventIntegration] Initialized with options:", this.options);
2044
- }
2045
- }
2046
- /**
2047
- * Set up delegated DOM event listeners
2048
- * @private
2049
- */
2050
- setupDOMEventListeners() {
2051
- const clickHandler = this.createDelegatedHandler("click", this.handleClick);
2052
- this.rootElement.addEventListener("click", clickHandler, { passive: false });
2053
- this.boundHandlers.set("click", clickHandler);
2054
- const changeHandler = this.options.enableDebounce ? this.debounce(this.createDelegatedHandler("change", this.handleChange), this.options.debounceDelay) : this.createDelegatedHandler("change", this.handleChange);
2055
- this.rootElement.addEventListener("change", changeHandler, { passive: true });
2056
- this.boundHandlers.set("change", changeHandler);
2057
- const inputHandler = this.options.enableDebounce ? this.debounce(this.createDelegatedHandler("input", this.handleInput), this.options.debounceDelay) : this.createDelegatedHandler("input", this.handleInput);
2058
- this.rootElement.addEventListener("input", inputHandler, { passive: true });
2059
- this.boundHandlers.set("input", inputHandler);
2060
- const submitHandler = this.createDelegatedHandler("submit", this.handleSubmit);
2061
- this.rootElement.addEventListener("submit", submitHandler, { passive: false });
2062
- this.boundHandlers.set("submit", submitHandler);
2063
- const keydownHandler = this.createDelegatedHandler("keydown", this.handleKeydown);
2064
- this.rootElement.addEventListener("keydown", keydownHandler, { passive: false });
2065
- this.boundHandlers.set("keydown", keydownHandler);
2066
- const focusHandler = this.createDelegatedHandler("focus", this.handleFocus);
2067
- this.rootElement.addEventListener("focus", focusHandler, { passive: true, capture: true });
2068
- this.boundHandlers.set("focus", focusHandler);
2069
- const blurHandler = this.createDelegatedHandler("blur", this.handleBlur);
2070
- this.rootElement.addEventListener("blur", blurHandler, { passive: true, capture: true });
2071
- this.boundHandlers.set("blur", blurHandler);
2072
- }
2073
- /**
2074
- * Create a delegated event handler
2075
- * @private
2076
- */
2077
- createDelegatedHandler(eventType, handler) {
2078
- return (event) => {
2079
- const target = event.target;
2080
- if (!target) return;
2081
- const actionElement = this.options.enableDelegation ? target.closest("[data-action]") : target.hasAttribute?.("data-action") ? target : null;
2082
- if (actionElement) {
2083
- handler(actionElement, event);
2084
- } else {
2085
- handler(target, event);
2086
- }
2087
- };
2088
- }
2089
- /**
2090
- * Handle click events
2091
- * @private
2092
- */
2093
- handleClick(element, event) {
2094
- const action = element.getAttribute?.("data-action");
2095
- if (action) {
2096
- this.handleDataAction(element, event, action);
2097
- }
2098
- this.eventBus.emitSync("dom:click", {
2099
- element,
2100
- event,
2101
- action,
2102
- data: this.parseDataAttributes(element)
2103
- });
2104
- }
2105
- /**
2106
- * Handle change events
2107
- * @private
2108
- */
2109
- handleChange(element, event) {
2110
- const action = element.getAttribute?.("data-action");
2111
- if (action) {
2112
- this.handleDataAction(element, event, action);
2113
- }
2114
- this.eventBus.emitSync("dom:change", {
2115
- element,
2116
- event,
2117
- value: element.value,
2118
- action,
2119
- data: this.parseDataAttributes(element)
2120
- });
2121
- }
2122
- /**
2123
- * Handle input events
2124
- * @private
2125
- */
2126
- handleInput(element, event) {
2127
- const action = element.getAttribute?.("data-action");
2128
- if (action) {
2129
- this.handleDataAction(element, event, action);
2130
- }
2131
- this.eventBus.emitSync("dom:input", {
2132
- element,
2133
- event,
2134
- value: element.value,
2135
- action,
2136
- data: this.parseDataAttributes(element)
2137
- });
2138
- }
2139
- /**
2140
- * Handle submit events
2141
- * @private
2142
- */
2143
- handleSubmit(element, event) {
2144
- const action = element.getAttribute?.("data-action");
2145
- if (action) {
2146
- event.preventDefault();
2147
- this.handleDataAction(element, event, action);
2148
- }
2149
- this.eventBus.emitSync("dom:submit", {
2150
- element,
2151
- event,
2152
- action,
2153
- formData: this.extractFormData(element),
2154
- data: this.parseDataAttributes(element)
2155
- });
2156
- }
2157
- /**
2158
- * Handle keydown events
2159
- * @private
2160
- */
2161
- handleKeydown(element, event) {
2162
- const action = element.getAttribute?.("data-action");
2163
- const keyAction = element.getAttribute?.(`data-key-${event.key.toLowerCase()}`);
2164
- if (action && this.shouldTriggerKeyAction(event)) {
2165
- this.handleDataAction(element, event, action);
2166
- }
2167
- if (keyAction) {
2168
- this.handleDataAction(element, event, keyAction);
2169
- }
2170
- this.eventBus.emitSync("dom:keydown", {
2171
- element,
2172
- event,
2173
- key: event.key,
2174
- code: event.code,
2175
- action,
2176
- keyAction,
2177
- data: this.parseDataAttributes(element)
2178
- });
2179
- }
2180
- /**
2181
- * Handle focus events
2182
- * @private
2183
- */
2184
- handleFocus(element, event) {
2185
- this.activeElement = element;
2186
- this.eventBus.emitSync("dom:focus", {
2187
- element,
2188
- event,
2189
- data: this.parseDataAttributes(element)
2190
- });
2191
- }
2192
- /**
2193
- * Handle blur events
2194
- * @private
2195
- */
2196
- handleBlur(element, event) {
2197
- if (this.activeElement === element) {
2198
- this.activeElement = null;
2199
- }
2200
- this.eventBus.emitSync("dom:blur", {
2201
- element,
2202
- event,
2203
- data: this.parseDataAttributes(element)
2204
- });
2205
- }
2206
- /**
2207
- * Handle data-action attributes
2208
- * @private
2209
- */
2210
- handleDataAction(element, event, action) {
2211
- if (!action) return;
2212
- const data = this.parseDataAttributes(element);
2213
- this.eventBus.emitSync("dom:action", {
2214
- action,
2215
- element,
2216
- event,
2217
- data
2218
- });
2219
- this.eventBus.handleAction(action, element, event, data);
2220
- if (this.options.debug) {
2221
- console.log(`[DOMEventIntegration] Action triggered: ${action}`, {
2222
- element,
2223
- event: event.type,
2224
- data
2225
- });
2226
- }
2227
- }
2228
- /**
2229
- * Parse data attributes from an element
2230
- * @private
2231
- */
2232
- parseDataAttributes(element) {
2233
- if (!element?.attributes) return {};
2234
- const data = {};
2235
- Array.from(element.attributes).forEach((attr) => {
2236
- if (attr.name.startsWith("data-") && attr.name !== "data-action") {
2237
- const key = attr.name.slice(5).replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
2238
- let value = attr.value;
2239
- try {
2240
- if (value === "true") value = true;
2241
- else if (value === "false") value = false;
2242
- else if (value === "null") value = null;
2243
- else if (value === "undefined") value = void 0;
2244
- else if (/^\d+$/.test(value)) value = parseInt(value, 10);
2245
- else if (/^\d*\.\d+$/.test(value)) value = parseFloat(value);
2246
- else if (value.startsWith("{") && value.endsWith("}") || value.startsWith("[") && value.endsWith("]")) {
2247
- value = JSON.parse(value);
2248
- }
2249
- } catch {
2250
- }
2251
- data[key] = value;
2252
- }
2253
- });
2254
- return data;
2255
- }
2256
- /**
2257
- * Extract form data from a form element
2258
- * @private
2259
- */
2260
- extractFormData(formElement) {
2261
- if (!formElement || formElement.tagName !== "FORM") {
2262
- return {};
2263
- }
2264
- const formData = new FormData(formElement);
2265
- const data = {};
2266
- for (const [key, value] of formData.entries()) {
2267
- if (data[key]) {
2268
- if (Array.isArray(data[key])) {
2269
- data[key].push(value);
2270
- } else {
2271
- data[key] = [data[key], value];
2272
- }
2273
- } else {
2274
- data[key] = value;
2275
- }
2276
- }
2277
- return data;
2278
- }
2279
- /**
2280
- * Check if a key event should trigger an action
2281
- * @private
2282
- */
2283
- shouldTriggerKeyAction(event) {
2284
- const triggerKeys = ["Enter", "Space", "Escape"];
2285
- return triggerKeys.includes(event.key);
2286
- }
2287
- /**
2288
- * Debounce utility function
2289
- * @private
2290
- */
2291
- debounce(func, wait) {
2292
- let timeout;
2293
- return function executedFunction(...args) {
2294
- const later = () => {
2295
- clearTimeout(timeout);
2296
- func.apply(this, args);
2297
- };
2298
- clearTimeout(timeout);
2299
- timeout = setTimeout(later, wait);
2300
- };
2301
- }
2302
- /**
2303
- * Throttle utility function
2304
- * @private
2305
- */
2306
- throttle(func, limit) {
2307
- let inThrottle;
2308
- return function executedFunction(...args) {
2309
- if (!inThrottle) {
2310
- func.apply(this, args);
2311
- inThrottle = true;
2312
- setTimeout(() => inThrottle = false, limit);
2313
- }
2314
- };
2315
- }
2316
- /**
2317
- * Add custom event listener
2318
- * @param {string} eventType - Event type
2319
- * @param {Function} handler - Event handler
2320
- * @param {Object} options - Event listener options
2321
- */
2322
- addCustomListener(eventType, handler, options = {}) {
2323
- const wrappedHandler = this.createDelegatedHandler(eventType, handler);
2324
- this.rootElement.addEventListener(eventType, wrappedHandler, options);
2325
- this.boundHandlers.set(`custom:${eventType}`, wrappedHandler);
2326
- }
2327
- /**
2328
- * Remove custom event listener
2329
- * @param {string} eventType - Event type
2330
- */
2331
- removeCustomListener(eventType) {
2332
- const handler = this.boundHandlers.get(`custom:${eventType}`);
2333
- if (handler) {
2334
- this.rootElement.removeEventListener(eventType, handler);
2335
- this.boundHandlers.delete(`custom:${eventType}`);
2336
- }
2337
- }
2338
- /**
2339
- * Register action handlers in bulk
2340
- * @param {Object} actions - Object mapping action names to handlers
2341
- */
2342
- registerActions(actions) {
2343
- this.eventBus.registerActions(actions);
2344
- }
2345
- /**
2346
- * Get the currently active (focused) element
2347
- * @returns {HTMLElement|null}
2348
- */
2349
- getActiveElement() {
2350
- return this.activeElement;
2351
- }
2352
- /**
2353
- * Trigger an action programmatically
2354
- * @param {string} action - Action name
2355
- * @param {HTMLElement} element - Target element
2356
- * @param {Object} data - Additional data
2357
- */
2358
- triggerAction(action, element, data = {}) {
2359
- const syntheticEvent = new CustomEvent("synthetic", {
2360
- bubbles: true,
2361
- cancelable: true,
2362
- detail: data
2363
- });
2364
- this.eventBus.handleAction(action, element, syntheticEvent, data);
2365
- }
2366
- /**
2367
- * Clean up event listeners
2368
- */
2369
- destroy() {
2370
- if (!this.isInitialized) return;
2371
- this.boundHandlers.forEach((handler, eventType) => {
2372
- this.rootElement.removeEventListener(
2373
- eventType.replace("custom:", ""),
2374
- handler
2375
- );
2376
- });
2377
- this.boundHandlers.clear();
2378
- this.activeElement = null;
2379
- this.isInitialized = false;
2380
- if (this.options.debug) {
2381
- console.log("[DOMEventIntegration] Destroyed");
2382
- }
2383
- }
2384
- };
2385
- var globalDOMIntegration = new DOMEventIntegration(globalEventBus, {
2386
- debug: typeof process !== "undefined" && true
2387
- });
2388
- if (typeof window !== "undefined") {
2389
- if (document.readyState === "loading") {
2390
- document.addEventListener("DOMContentLoaded", () => {
2391
- globalDOMIntegration.initialize();
2392
- });
2393
- } else {
2394
- globalDOMIntegration.initialize();
2395
- }
2396
- }
2397
-
2398
- // ../core/src/events/index.js
2399
- var eventSystem2 = {
2400
- // Core bus
2401
- bus: globalEventBus,
2402
- dom: globalDOMIntegration,
2403
- // Quick access methods
2404
- emit: globalEventBus.emit.bind(globalEventBus),
2405
- emitSync: globalEventBus.emitSync.bind(globalEventBus),
2406
- on: globalEventBus.on.bind(globalEventBus),
2407
- once: globalEventBus.once.bind(globalEventBus),
2408
- off: globalEventBus.off.bind(globalEventBus),
2409
- // Action methods
2410
- registerAction: globalEventBus.registerAction.bind(globalEventBus),
2411
- registerActions: globalEventBus.registerActions.bind(globalEventBus),
2412
- handleAction: globalEventBus.handleAction.bind(globalEventBus),
2413
- // Statistics and debugging
2414
- getStats: globalEventBus.getStats.bind(globalEventBus),
2415
- resetStats: globalEventBus.resetStats.bind(globalEventBus),
2416
- // Lifecycle
2417
- destroy() {
2418
- globalEventBus.destroy();
2419
- globalDOMIntegration.destroy();
2420
- }
2421
- };
2422
-
2423
- // ../core/src/index.js
2424
- var scopeCounter = { value: 0 };
2425
- function generateScopeId() {
2426
- return `coh-${scopeCounter.value++}`;
2427
- }
2428
- function scopeCSS(css, scopeId) {
2429
- if (!css || typeof css !== "string") return css;
2430
- return css.replace(/([^{}]*)\s*{/g, (match, selector) => {
2431
- const selectors = selector.split(",").map((s) => {
2432
- const trimmed = s.trim();
2433
- if (!trimmed) return s;
2434
- if (trimmed.includes(":")) {
2435
- return trimmed.replace(/([^:]+)(:.*)?/, `$1[${scopeId}]$2`);
2436
- }
2437
- return `${trimmed}[${scopeId}]`;
2438
- });
2439
- return `${selectors.join(", ")} {`;
2440
- });
2441
- }
2442
- function applyScopeToElement(element, scopeId) {
2443
- if (typeof element === "string" || typeof element === "number" || !element) {
2444
- return element;
2445
- }
2446
- if (Array.isArray(element)) {
2447
- return element.map((item) => applyScopeToElement(item, scopeId));
2448
- }
2449
- if (typeof element === "object") {
2450
- const scoped = {};
2451
- for (const [tagName, props] of Object.entries(element)) {
2452
- if (typeof props === "object" && props !== null) {
2453
- const scopedProps = { ...props };
2454
- scopedProps[scopeId] = "";
2455
- if (scopedProps.children) {
2456
- scopedProps.children = applyScopeToElement(scopedProps.children, scopeId);
2457
- }
2458
- scoped[tagName] = scopedProps;
2459
- } else {
2460
- scoped[tagName] = props;
2461
- }
2462
- }
2463
- return scoped;
2464
- }
2465
- return element;
2466
- }
2467
- function escapeHtml(text) {
2468
- if (typeof text !== "string") return text;
2469
- return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#x27;");
2470
- }
2471
- function isTrustedContent(value) {
2472
- return value && typeof value === "object" && value.__trusted === true && typeof value.__html === "string";
2473
- }
2474
- function isVoidElement(tagName) {
2475
- const voidElements = /* @__PURE__ */ new Set([
2476
- "area",
2477
- "base",
2478
- "br",
2479
- "col",
2480
- "embed",
2481
- "hr",
2482
- "img",
2483
- "input",
2484
- "link",
2485
- "meta",
2486
- "param",
2487
- "source",
2488
- "track",
2489
- "wbr"
2490
- ]);
2491
- return voidElements.has(tagName.toLowerCase());
2492
- }
2493
- function formatAttributes(attrs) {
2494
- if (!attrs || typeof attrs !== "object") return "";
2495
- return Object.entries(attrs).filter(([, value]) => value !== null && value !== void 0 && value !== false).map(([key, value]) => {
2496
- if (typeof value === "function") {
2497
- value = value();
2498
- }
2499
- const attrName = key === "className" ? "class" : key;
2500
- if (value === true) return attrName;
2501
- return `${attrName}="${escapeHtml(String(value))}"`;
2502
- }).join(" ");
2503
- }
2504
- function renderRaw(obj) {
2505
- if (obj === null || obj === void 0) return "";
2506
- if (typeof obj === "string" || typeof obj === "number") return escapeHtml(String(obj));
2507
- if (Array.isArray(obj)) return obj.map(renderRaw).join("");
2508
- if (typeof obj === "function") {
2509
- const result = obj(renderRaw);
2510
- return renderRaw(result);
2511
- }
2512
- if (typeof obj !== "object") return escapeHtml(String(obj));
2513
- if (obj.text !== void 0) {
2514
- return escapeHtml(String(obj.text));
2515
- }
2516
- for (const [tagName, props] of Object.entries(obj)) {
2517
- if (typeof props === "object" && props !== null) {
2518
- const { children, text, ...attributes } = props;
2519
- const attrsStr = formatAttributes(attributes);
2520
- const openTag = attrsStr ? `<${tagName} ${attrsStr}>` : `<${tagName}>`;
2521
- if (isVoidElement(tagName)) {
2522
- return openTag.replace(">", " />");
2523
- }
2524
- let content = "";
2525
- if (text !== void 0) {
2526
- if (isTrustedContent(text)) {
2527
- content = text.__html;
2528
- } else {
2529
- content = escapeHtml(String(text));
2530
- }
2531
- } else if (children) {
2532
- content = renderRaw(children);
2533
- }
2534
- return `${openTag}${content}</${tagName}>`;
2535
- } else if (typeof props === "string") {
2536
- const content = isTrustedContent(props) ? props.__html : escapeHtml(props);
2537
- return isVoidElement(tagName) ? `<${tagName} />` : `<${tagName}>${content}</${tagName}>`;
2538
- }
2539
- }
2540
- return "";
2541
- }
2542
- function render(obj, options = {}) {
2543
- const { scoped = false } = options;
2544
- if (scoped) {
2545
- return renderScopedComponent(obj);
2546
- }
2547
- return renderRaw(obj);
2548
- }
2549
- function renderScopedComponent(component) {
2550
- const scopeId = generateScopeId();
2551
- function processScopedElement(element) {
2552
- if (!element || typeof element !== "object") {
2553
- return element;
2554
- }
2555
- if (Array.isArray(element)) {
2556
- return element.map(processScopedElement);
2557
- }
2558
- const result = {};
2559
- for (const [tagName, props] of Object.entries(element)) {
2560
- if (tagName === "style" && typeof props === "object" && props.text) {
2561
- result[tagName] = {
2562
- ...props,
2563
- text: scopeCSS(props.text, scopeId)
2564
- };
2565
- } else if (typeof props === "object" && props !== null) {
2566
- const scopedProps = { ...props };
2567
- if (scopedProps.children) {
2568
- scopedProps.children = processScopedElement(scopedProps.children);
2569
- }
2570
- result[tagName] = scopedProps;
2571
- } else {
2572
- result[tagName] = props;
2573
- }
2574
- }
2575
- return result;
2576
- }
2577
- const processedComponent = processScopedElement(component);
2578
- const scopedComponent = applyScopeToElement(processedComponent, scopeId);
2579
- return renderRaw(scopedComponent);
2580
- }
2581
-
2582
- // ../core/src/utils/dependency-utils.js
2583
- async function importPeerDependency(packageName, integrationName) {
2584
- try {
2585
- return await import(packageName);
2586
- } catch {
2587
- throw new Error(
2588
- `${integrationName} integration requires the '${packageName}' package to be installed.
2589
- Please install it with: npm install ${packageName}
2590
- Or with pnpm: pnpm add ${packageName}
2591
- Or with yarn: yarn add ${packageName}`
2592
- );
2593
- }
2594
- }
2595
-
2596
- // ../core/src/rendering/base-renderer.js
2597
- var DEFAULT_RENDERER_CONFIG = {
2598
- // Core rendering options
2599
- maxDepth: 100,
2600
- enableValidation: true,
2601
- enableMonitoring: false,
2602
- validateInput: true,
2603
- // HTML Renderer specific options
2604
- enableCache: true,
2605
- minify: false,
2606
- cacheSize: 1e3,
2607
- cacheTTL: 3e5,
2608
- // 5 minutes
2609
- // Streaming Renderer specific options
2610
- chunkSize: 1024,
2611
- // Size of each chunk in bytes
2612
- bufferSize: 4096,
2613
- // Internal buffer size
2614
- enableMetrics: false,
2615
- // Track streaming metrics
2616
- yieldThreshold: 100,
2617
- // Yield control after N elements
2618
- encoding: "utf8",
2619
- // Output encoding
2620
- // DOM Renderer specific options
2621
- enableHydration: true,
2622
- // Enable hydration support
2623
- namespace: null,
2624
- // SVG namespace support
2625
- // Performance options
2626
- enablePerformanceTracking: false,
2627
- performanceThreshold: 10,
2628
- // ms threshold for slow renders
2629
- // Development options
2630
- enableDevWarnings: typeof process !== "undefined" && process.env && true,
2631
- enableDebugLogging: false,
2632
- // Error handling options
2633
- errorFallback: "",
2634
- // Fallback content on errors
2635
- throwOnError: true
2636
- // Whether to throw or return fallback
2637
- };
2638
- var BaseRenderer = class {
2639
- constructor(options = {}) {
2640
- this.config = this.validateAndMergeConfig(options);
2641
- this.metrics = {
2642
- startTime: null,
2643
- endTime: null,
2644
- elementsProcessed: 0
2645
- };
2646
- }
2647
- /**
2648
- * Validate and merge configuration options
2649
- */
2650
- validateAndMergeConfig(options) {
2651
- const config = { ...DEFAULT_RENDERER_CONFIG, ...options };
2652
- if (typeof config.maxDepth !== "number") {
2653
- throw new Error("maxDepth must be a number");
2654
- }
2655
- if (config.maxDepth <= 0) {
2656
- throw new Error("maxDepth must be a positive number");
2657
- }
2658
- if (typeof config.chunkSize !== "number") {
2659
- throw new Error("chunkSize must be a number");
2660
- }
2661
- if (config.chunkSize <= 0) {
2662
- throw new Error("chunkSize must be a positive number");
2663
- }
2664
- if (typeof config.yieldThreshold !== "number") {
2665
- throw new Error("yieldThreshold must be a number");
2666
- }
2667
- if (config.yieldThreshold <= 0) {
2668
- throw new Error("yieldThreshold must be a positive number");
2669
- }
2670
- if (config.enableDevWarnings) {
2671
- if (config.maxDepth > 1e3) {
2672
- console.warn("Coherent.js: maxDepth > 1000 may cause performance issues");
2673
- }
2674
- if (config.chunkSize > 16384) {
2675
- console.warn("Coherent.js: Large chunkSize may increase memory usage");
2676
- }
2677
- }
2678
- return config;
2679
- }
2680
- /**
2681
- * Get configuration for specific renderer type
2682
- */
2683
- getRendererConfig(rendererType) {
2684
- const baseConfig = { ...this.config };
2685
- switch (rendererType) {
2686
- case "html":
2687
- return {
2688
- ...baseConfig,
2689
- // HTML-specific defaults
2690
- enableCache: baseConfig.enableCache !== false,
2691
- enableMonitoring: baseConfig.enableMonitoring !== false
2692
- };
2693
- case "streaming":
2694
- return {
2695
- ...baseConfig,
2696
- // Streaming-specific defaults
2697
- enableMetrics: baseConfig.enableMetrics ?? false,
2698
- maxDepth: baseConfig.maxDepth ?? 1e3
2699
- // Higher default for streaming
2700
- };
2701
- case "dom":
2702
- return {
2703
- ...baseConfig,
2704
- // DOM-specific defaults
2705
- enableHydration: baseConfig.enableHydration !== false
2706
- };
2707
- default:
2708
- return baseConfig;
2709
- }
2710
- }
2711
- /**
2712
- * Validate component structure
2713
- */
2714
- validateComponent(component) {
2715
- if (this.config.validateInput !== false) {
2716
- return validateComponent(component);
2717
- }
2718
- return true;
2719
- }
2720
- /**
2721
- * Check if component is valid for rendering
2722
- */
2723
- isValidComponent(component) {
2724
- if (component === null || component === void 0) return true;
2725
- if (typeof component === "string" || typeof component === "number") return true;
2726
- if (typeof component === "function") return true;
2727
- if (Array.isArray(component)) return component.every((child) => this.isValidComponent(child));
2728
- if (isCoherentObject(component)) return true;
2729
- return false;
2730
- }
2731
- /**
2732
- * Validate rendering depth to prevent stack overflow
2733
- */
2734
- validateDepth(depth) {
2735
- if (depth > this.config.maxDepth) {
2736
- throw new Error(`Maximum render depth (${this.config.maxDepth}) exceeded`);
2737
- }
2738
- }
2739
- /**
2740
- * Handle different component types with consistent logic
2741
- */
2742
- processComponentType(component) {
2743
- if (component === null || component === void 0) {
2744
- return { type: "empty", value: "" };
2745
- }
2746
- if (typeof component === "string") {
2747
- return { type: "text", value: component };
2748
- }
2749
- if (typeof component === "number" || typeof component === "boolean") {
2750
- return { type: "text", value: String(component) };
2751
- }
2752
- if (typeof component === "function") {
2753
- return { type: "function", value: component };
2754
- }
2755
- if (Array.isArray(component)) {
2756
- return { type: "array", value: component };
2757
- }
2758
- if (isCoherentObject(component)) {
2759
- return { type: "element", value: component };
2760
- }
2761
- return { type: "unknown", value: component };
2762
- }
2763
- /**
2764
- * Execute function components with _error handling
2765
- */
2766
- executeFunctionComponent(func, depth = 0) {
2767
- try {
2768
- const isContextProvider = func.length > 0 || func.isContextProvider;
2769
- let result;
2770
- if (isContextProvider) {
2771
- result = func((children) => {
2772
- return this.renderComponent(children, this.config, depth + 1);
2773
- });
2774
- } else {
2775
- result = func();
2776
- }
2777
- if (typeof result === "function") {
2778
- return this.executeFunctionComponent(result, depth);
2779
- }
2780
- return result;
2781
- } catch (_error) {
2782
- if (this.config.enableMonitoring) {
2783
- performanceMonitor2.recordError("functionComponent", _error);
2784
- }
2785
- if (typeof process !== "undefined" && process.env && true) {
2786
- console.warn("Coherent.js Function Component Error:", _error.message);
2787
- }
2788
- return null;
2789
- }
2790
- }
2791
- /**
2792
- * Process element children consistently
2793
- */
2794
- processChildren(children, options, depth) {
2795
- if (!hasChildren({ children })) {
2796
- return [];
2797
- }
2798
- const normalizedChildren = normalizeChildren(children);
2799
- return normalizedChildren.map(
2800
- (child) => this.renderComponent(child, options, depth + 1)
2801
- );
2802
- }
2803
- /**
2804
- * Extract and process element attributes
2805
- */
2806
- extractElementAttributes(props) {
2807
- if (!props || typeof props !== "object") return {};
2808
- const attributes = { ...props };
2809
- delete attributes.children;
2810
- delete attributes.text;
2811
- return attributes;
2812
- }
2813
- /**
2814
- * Record performance metrics
2815
- */
2816
- recordPerformance(operation, startTime, fromCache = false, metadata = {}) {
2817
- if (this.config.enableMonitoring) {
2818
- performanceMonitor2.recordRender(
2819
- operation,
2820
- this.getCurrentTime() - startTime,
2821
- fromCache,
2822
- metadata
2823
- );
2824
- }
2825
- }
2826
- /**
2827
- * Record _error for monitoring
2828
- */
2829
- recordError(operation, _error, metadata = {}) {
2830
- if (this.config.enableMonitoring) {
2831
- performanceMonitor2.recordError(operation, _error, metadata);
2832
- }
2833
- }
2834
- /**
2835
- * Get current timestamp with fallback
2836
- */
2837
- getCurrentTime() {
2838
- if (typeof performance !== "undefined" && performance.now) {
2839
- return performance.now();
2840
- }
2841
- return Date.now();
2842
- }
2843
- /**
2844
- * Start performance timing
2845
- */
2846
- startTiming() {
2847
- this.metrics.startTime = this.getCurrentTime();
2848
- }
2849
- /**
2850
- * End performance timing
2851
- */
2852
- endTiming() {
2853
- this.metrics.endTime = this.getCurrentTime();
2854
- }
2855
- /**
2856
- * Get performance metrics
2857
- */
2858
- getMetrics() {
2859
- const duration = this.metrics.endTime ? this.metrics.endTime - this.metrics.startTime : this.getCurrentTime() - this.metrics.startTime;
2860
- return {
2861
- ...this.metrics,
2862
- duration,
2863
- elementsPerSecond: this.metrics.elementsProcessed / (duration / 1e3)
2864
- };
2865
- }
2866
- /**
2867
- * Reset metrics for new render
2868
- */
2869
- resetMetrics() {
2870
- this.metrics = {
2871
- startTime: null,
2872
- endTime: null,
2873
- elementsProcessed: 0
2874
- };
2875
- }
2876
- /**
2877
- * Abstract method - must be implemented by subclasses
2878
- */
2879
- renderComponent() {
2880
- throw new Error("renderComponent must be implemented by subclass");
2881
- }
2882
- /**
2883
- * Abstract method - must be implemented by subclasses
2884
- */
2885
- render() {
2886
- throw new Error("render must be implemented by subclass");
2887
- }
2888
- };
2889
- var RendererUtils = {
2890
- /**
2891
- * Check if element is static (no functions)
2892
- */
2893
- isStaticElement(element) {
2894
- if (!element || typeof element !== "object") {
2895
- return typeof element === "string" || typeof element === "number";
2896
- }
2897
- for (const [key, value] of Object.entries(element)) {
2898
- if (typeof value === "function") return false;
2899
- if (key === "children" && Array.isArray(value)) {
2900
- return value.every((child) => RendererUtils.isStaticElement(child));
2901
- }
2902
- if (key === "children" && typeof value === "object" && value !== null) {
2903
- return RendererUtils.isStaticElement(value);
2904
- }
2905
- }
2906
- return true;
2907
- },
2908
- /**
2909
- * Check if object has functions (for caching decisions)
2910
- */
2911
- hasFunctions(obj, visited = /* @__PURE__ */ new WeakSet()) {
2912
- if (visited.has(obj)) return false;
2913
- visited.add(obj);
2914
- for (const value of Object.values(obj)) {
2915
- if (typeof value === "function") return true;
2916
- if (typeof value === "object" && value !== null && RendererUtils.hasFunctions(value, visited)) {
2917
- return true;
2918
- }
2919
- }
2920
- return false;
2921
- },
2922
- /**
2923
- * Get element complexity score
2924
- */
2925
- getElementComplexity(element) {
2926
- if (!element || typeof element !== "object") return 1;
2927
- let complexity = Object.keys(element).length;
2928
- if (element.children && Array.isArray(element.children)) {
2929
- complexity += element.children.reduce(
2930
- (sum, child) => sum + RendererUtils.getElementComplexity(child),
2931
- 0
2932
- );
2933
- }
2934
- return complexity;
2935
- },
2936
- /**
2937
- * Generate cache key for element
2938
- */
2939
- generateCacheKey(tagName, element) {
2940
- try {
2941
- const keyData = {
2942
- tag: tagName,
2943
- props: extractProps(element),
2944
- hasChildren: hasChildren(element),
2945
- childrenType: Array.isArray(element.children) ? "array" : typeof element.children
2946
- };
2947
- return `element:${JSON.stringify(keyData)}`;
2948
- } catch (_error) {
2949
- if (typeof process !== "undefined" && process.env && true) {
2950
- console.warn("Failed to generate cache key:", _error);
2951
- }
2952
- return null;
2953
- }
2954
- },
2955
- /**
2956
- * Check if element is cacheable
2957
- */
2958
- isCacheable(element, options) {
2959
- if (!options.enableCache) return false;
2960
- if (RendererUtils.hasFunctions(element)) return false;
2961
- if (RendererUtils.getElementComplexity(element) > 1e3) return false;
2962
- const cacheKey = RendererUtils.generateCacheKey(element.tagName || "unknown", element);
2963
- if (!cacheKey) return false;
2964
- return true;
2965
- }
2966
- };
2967
-
2968
- // ../core/src/core/html-utils.js
2969
- function escapeHtml2(text) {
2970
- if (typeof text !== "string") return text;
2971
- return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
2972
- }
2973
- function isVoidElement2(tagName) {
2974
- if (typeof tagName !== "string") {
2975
- return false;
2976
- }
2977
- const voidElements = /* @__PURE__ */ new Set([
2978
- "area",
2979
- "base",
2980
- "br",
2981
- "col",
2982
- "embed",
2983
- "hr",
2984
- "img",
2985
- "input",
2986
- "link",
2987
- "meta",
2988
- "param",
2989
- "source",
2990
- "track",
2991
- "wbr"
2992
- ]);
2993
- return voidElements.has(tagName.toLowerCase());
2994
- }
2995
- function formatAttributes2(props) {
2996
- let formatted = "";
2997
- for (const key in props) {
2998
- if (props.hasOwnProperty(key)) {
2999
- let value = props[key];
3000
- const attributeName = key === "className" ? "class" : key;
3001
- if (typeof value === "function") {
3002
- if (attributeName.startsWith("on")) {
3003
- const actionId = `__coherent_action_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
3004
- const DEBUG = typeof process !== "undefined" && process && process.env && (process.env.COHERENT_DEBUG === "1" || true) || typeof window !== "undefined" && window && window.COHERENT_DEBUG === true;
3005
- if (typeof global !== "undefined") {
3006
- if (!global.__coherentActionRegistry) {
3007
- global.__coherentActionRegistry = {};
3008
- if (DEBUG) console.log("Initialized global action registry");
3009
- }
3010
- global.__coherentActionRegistry[actionId] = value;
3011
- if (DEBUG) console.log(`Added action ${actionId} to global registry, total: ${Object.keys(global.__coherentActionRegistry).length}`);
3012
- if (DEBUG) console.log(`Global registry keys: ${Object.keys(global.__coherentActionRegistry).join(", ")}`);
3013
- if (DEBUG) {
3014
- if (typeof global.__coherentActionRegistryLog === "undefined") {
3015
- global.__coherentActionRegistryLog = [];
3016
- }
3017
- global.__coherentActionRegistryLog.push({
3018
- action: "add",
3019
- actionId,
3020
- timestamp: Date.now(),
3021
- registrySize: Object.keys(global.__coherentActionRegistry).length
3022
- });
3023
- }
3024
- } else if (typeof window !== "undefined") {
3025
- if (!window.__coherentActionRegistry) {
3026
- window.__coherentActionRegistry = {};
3027
- if (DEBUG) console.log("Initialized window action registry");
3028
- }
3029
- window.__coherentActionRegistry[actionId] = value;
3030
- if (DEBUG) console.log(`Added action ${actionId} to window registry, total: ${Object.keys(window.__coherentActionRegistry).length}`);
3031
- if (DEBUG) console.log(`Window registry keys: ${Object.keys(window.__coherentActionRegistry).join(", ")}`);
3032
- }
3033
- const eventType = attributeName.substring(2);
3034
- formatted += ` data-action="${actionId}" data-event="${eventType}"`;
3035
- continue;
3036
- } else {
3037
- try {
3038
- value = value();
3039
- } catch (_error) {
3040
- console.warn(`Error executing function for attribute '${key}':`, {
3041
- _error: _error.message,
3042
- stack: _error.stack,
3043
- attributeKey: key
3044
- });
3045
- value = "";
3046
- }
3047
- }
3048
- }
3049
- if (value === true) {
3050
- formatted += ` ${attributeName}`;
3051
- } else if (value !== false && value !== null && value !== void 0) {
3052
- formatted += ` ${attributeName}="${escapeHtml2(String(value))}"`;
3053
- }
3054
- }
3055
- }
3056
- return formatted.trim();
3057
- }
3058
- function minifyHtml(html, options = {}) {
3059
- if (!options.minify) return html;
3060
- return html.replace(/<!--[\s\S]*?-->/g, "").replace(/\s+/g, " ").replace(/>\s+</g, "><").trim();
3061
- }
3062
-
3063
- // ../core/src/performance/cache-manager.js
3064
- function createCacheManager(options = {}) {
3065
- const {
3066
- maxCacheSize = 1e3,
3067
- maxMemoryMB = 100,
3068
- ttlMs = 1e3 * 60 * 5,
3069
- // 5 minutes
3070
- enableStatistics = true
3071
- } = options;
3072
- const caches = {
3073
- static: /* @__PURE__ */ new Map(),
3074
- // Never-changing components
3075
- component: /* @__PURE__ */ new Map(),
3076
- // Component results with deps
3077
- template: /* @__PURE__ */ new Map(),
3078
- // Template strings
3079
- data: /* @__PURE__ */ new Map()
3080
- // General purpose data
3081
- };
3082
- let memoryUsage = 0;
3083
- const stats = {
3084
- hits: 0,
3085
- misses: 0,
3086
- hitRate: {
3087
- static: 0,
3088
- component: 0,
3089
- template: 0,
3090
- data: 0
3091
- },
3092
- accessCount: {
3093
- static: 0,
3094
- component: 0,
3095
- template: 0,
3096
- data: 0
3097
- }
3098
- };
3099
- let cleanupInterval;
3100
- if (typeof setInterval === "function") {
3101
- cleanupInterval = setInterval(() => cleanup(), 3e4);
3102
- if (cleanupInterval.unref) {
3103
- cleanupInterval.unref();
3104
- }
3105
- }
3106
- function generateCacheKey(component, props = {}, context2 = {}) {
3107
- const componentStr = typeof component === "function" ? component.name || component.toString() : JSON.stringify(component);
3108
- const propsStr = JSON.stringify(props, Object.keys(props).sort());
3109
- const contextStr = JSON.stringify(context2);
3110
- const hash = simpleHash(componentStr + propsStr + contextStr);
3111
- return `${extractComponentName(component)}_${hash}`;
3112
- }
3113
- function get(key, type = "component") {
3114
- const cache = caches[type] || caches.component;
3115
- const entry = cache.get(key);
3116
- if (!entry) {
3117
- stats.misses++;
3118
- if (enableStatistics) stats.accessCount[type]++;
3119
- return null;
3120
- }
3121
- if (Date.now() - entry.timestamp > ttlMs) {
3122
- cache.delete(key);
3123
- updateMemoryUsage(-entry.size);
3124
- stats.misses++;
3125
- if (enableStatistics) stats.accessCount[type]++;
3126
- return null;
3127
- }
3128
- entry.lastAccess = Date.now();
3129
- entry.accessCount++;
3130
- stats.hits++;
3131
- if (enableStatistics) {
3132
- stats.accessCount[type]++;
3133
- stats.hitRate[type] = stats.hits / (stats.hits + stats.misses) * 100;
3134
- }
3135
- return entry.value;
3136
- }
3137
- function set(key, value, type = "component", metadata = {}) {
3138
- const cache = caches[type] || caches.component;
3139
- const size = calculateSize(value);
3140
- if (memoryUsage + size > maxMemoryMB * 1024 * 1024) {
3141
- optimize(type, size);
3142
- }
3143
- const entry = {
3144
- value,
3145
- timestamp: Date.now(),
3146
- lastAccess: Date.now(),
3147
- size,
3148
- metadata,
3149
- accessCount: 0
3150
- };
3151
- const existing = cache.get(key);
3152
- if (existing) {
3153
- updateMemoryUsage(-existing.size);
3154
- }
3155
- cache.set(key, entry);
3156
- updateMemoryUsage(size);
3157
- if (cache.size > maxCacheSize) {
3158
- optimize(type);
3159
- }
3160
- }
3161
- function remove(key, type) {
3162
- if (type) {
3163
- const cache = caches[type];
3164
- if (!cache) return false;
3165
- const entry = cache.get(key);
3166
- if (entry) {
3167
- updateMemoryUsage(-entry.size);
3168
- return cache.delete(key);
3169
- }
3170
- return false;
3171
- }
3172
- for (const [, cache] of Object.entries(caches)) {
3173
- const entry = cache.get(key);
3174
- if (entry) {
3175
- updateMemoryUsage(-entry.size);
3176
- return cache.delete(key);
3177
- }
3178
- }
3179
- return false;
3180
- }
3181
- function clear(type) {
3182
- if (type) {
3183
- const cache = caches[type];
3184
- if (cache) {
3185
- cache.clear();
3186
- }
3187
- } else {
3188
- Object.values(caches).forEach((cache) => cache.clear());
3189
- }
3190
- memoryUsage = 0;
3191
- }
3192
- function getStats() {
3193
- const entries = Object.values(caches).reduce((sum, cache) => sum + cache.size, 0);
3194
- return {
3195
- hits: stats.hits,
3196
- misses: stats.misses,
3197
- size: memoryUsage,
3198
- entries,
3199
- hitRate: stats.hitRate,
3200
- accessCount: stats.accessCount
3201
- };
3202
- }
3203
- function cleanup() {
3204
- const now = Date.now();
3205
- let freed = 0;
3206
- for (const [, cache] of Object.entries(caches)) {
3207
- for (const [key, entry] of cache.entries()) {
3208
- if (now - entry.timestamp > ttlMs) {
3209
- cache.delete(key);
3210
- updateMemoryUsage(-entry.size);
3211
- freed++;
3212
- }
3213
- }
3214
- }
3215
- return { freed };
3216
- }
3217
- function calculateSize(value) {
3218
- if (value === null || value === void 0) return 0;
3219
- if (typeof value === "string") return value.length * 2;
3220
- if (typeof value === "number") return 8;
3221
- if (typeof value === "boolean") return 4;
3222
- if (Array.isArray(value)) {
3223
- return value.reduce((sum, item) => sum + calculateSize(item), 0);
3224
- }
3225
- if (typeof value === "object") {
3226
- return Object.values(value).reduce((sum, val) => sum + calculateSize(val), 0);
3227
- }
3228
- return 0;
3229
- }
3230
- function updateMemoryUsage(delta) {
3231
- memoryUsage = Math.max(0, memoryUsage + delta);
3232
- }
3233
- function optimize(type, requiredSpace = 0) {
3234
- const cache = caches[type] || caches.component;
3235
- const entries = Array.from(cache.entries()).sort(([, a], [, b]) => a.lastAccess - b.lastAccess);
3236
- let freed = 0;
3237
- for (const [key, entry] of entries) {
3238
- if (freed >= requiredSpace) break;
3239
- cache.delete(key);
3240
- updateMemoryUsage(-entry.size);
3241
- freed += entry.size;
3242
- }
3243
- return { freed };
3244
- }
3245
- function simpleHash(str) {
3246
- let hash = 0;
3247
- for (let i = 0; i < str.length; i++) {
3248
- const char = str.charCodeAt(i);
3249
- hash = (hash << 5) - hash + char;
3250
- hash = hash & hash;
3251
- }
3252
- return Math.abs(hash).toString(36);
3253
- }
3254
- function extractComponentName(component) {
3255
- if (typeof component === "function") {
3256
- return component.name || "AnonymousComponent";
3257
- }
3258
- if (component && typeof component === "object") {
3259
- const keys = Object.keys(component);
3260
- return keys.length > 0 ? keys[0] : "ObjectComponent";
3261
- }
3262
- return "UnknownComponent";
3263
- }
3264
- function destroy() {
3265
- if (cleanupInterval) {
3266
- clearInterval(cleanupInterval);
3267
- }
3268
- clear();
3269
- }
3270
- return {
3271
- get,
3272
- set,
3273
- remove,
3274
- clear,
3275
- getStats,
3276
- cleanup,
3277
- destroy,
3278
- generateCacheKey,
3279
- get memoryUsage() {
3280
- return memoryUsage;
3281
- },
3282
- get maxMemory() {
3283
- return maxMemoryMB * 1024 * 1024;
3284
- }
3285
- };
3286
- }
3287
- var cacheManager = createCacheManager();
3288
-
3289
- // ../core/src/rendering/css-manager.js
3290
- import fs from "node:fs/promises";
3291
- import path from "node:path";
3292
- var CSSManager = class {
3293
- constructor(options = {}) {
3294
- this.options = {
3295
- basePath: process.cwd(),
3296
- minify: false,
3297
- cache: true,
3298
- autoprefixer: false,
3299
- ...options
3300
- };
3301
- this.cache = /* @__PURE__ */ new Map();
3302
- this.loadedFiles = /* @__PURE__ */ new Set();
3303
- }
3304
- /**
3305
- * Load CSS file content
3306
- */
3307
- async loadCSSFile(filePath) {
3308
- const fullPath = path.resolve(this.options.basePath, filePath);
3309
- const cacheKey = fullPath;
3310
- if (this.options.cache && this.cache.has(cacheKey)) {
3311
- return this.cache.get(cacheKey);
3312
- }
3313
- try {
3314
- let content = await fs.readFile(fullPath, "utf8");
3315
- if (this.options.minify) {
3316
- content = this.minifyCSS(content);
3317
- }
3318
- if (this.options.cache) {
3319
- this.cache.set(cacheKey, content);
3320
- }
3321
- this.loadedFiles.add(filePath);
3322
- return content;
3323
- } catch (_error) {
3324
- console.warn(`Failed to load CSS file: ${filePath}`, _error.message);
3325
- return "";
3326
- }
3327
- }
3328
- /**
3329
- * Load multiple CSS files
3330
- */
3331
- async loadCSSFiles(filePaths) {
3332
- if (!Array.isArray(filePaths)) {
3333
- filePaths = [filePaths];
3334
- }
3335
- const cssContents = await Promise.all(
3336
- filePaths.map((filePath) => this.loadCSSFile(filePath))
3337
- );
3338
- return cssContents.join("\n");
3339
- }
3340
- /**
3341
- * Generate CSS link tags for external files
3342
- */
3343
- generateCSSLinks(filePaths, baseUrl = "/") {
3344
- if (!Array.isArray(filePaths)) {
3345
- filePaths = [filePaths];
3346
- }
3347
- return filePaths.map((filePath) => {
3348
- const href = filePath.startsWith("http") ? filePath : `${baseUrl}${filePath}`.replace(/\/+/g, "/");
3349
- return `<link rel="stylesheet" href="${this.escapeHtml(href)}" />`;
3350
- }).join("\n");
3351
- }
3352
- /**
3353
- * Generate inline style tag with CSS content
3354
- */
3355
- generateInlineStyles(cssContent) {
3356
- if (!cssContent) return "";
3357
- return `<style type="text/css">
3358
- ${cssContent}
3359
- </style>`;
3360
- }
3361
- /**
3362
- * Basic CSS minification
3363
- */
3364
- minifyCSS(css) {
3365
- return css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\s+/g, " ").replace(/;\s*}/g, "}").replace(/{\s+/g, "{").replace(/;\s+/g, ";").trim();
3366
- }
3367
- /**
3368
- * Escape HTML entities
3369
- */
3370
- escapeHtml(text) {
3371
- const div = { textContent: text };
3372
- return div.innerHTML || text;
3373
- }
3374
- /**
3375
- * Clear cache
3376
- */
3377
- clearCache() {
3378
- this.cache.clear();
3379
- this.loadedFiles.clear();
3380
- }
3381
- /**
3382
- * Get loaded file list
3383
- */
3384
- getLoadedFiles() {
3385
- return Array.from(this.loadedFiles);
3386
- }
3387
- };
3388
- var defaultCSSManager = new CSSManager();
3389
-
3390
- // ../core/src/rendering/html-renderer.js
3391
- var rendererCache = createCacheManager({
3392
- maxSize: 1e3,
3393
- ttlMs: 3e5
3394
- // 5 minutes
3395
- });
3396
- var HTMLRenderer = class extends BaseRenderer {
3397
- constructor(options = {}) {
3398
- super({
3399
- enableCache: options.enableCache !== false,
3400
- enableMonitoring: options.enableMonitoring !== false,
3401
- minify: options.minify || false,
3402
- streaming: options.streaming || false,
3403
- maxDepth: options.maxDepth || 100,
3404
- ...options
3405
- });
3406
- if (this.config.enableCache && !this.cache) {
3407
- this.cache = rendererCache;
3408
- }
3409
- }
3410
- /**
3411
- * Main render method - converts components to HTML string
3412
- *
3413
- * @param {Object|Array|string|Function} component - Component to render
3414
- * @param {Object} [options={}] - Rendering options
3415
- * @param {Object} [options.context] - Rendering context
3416
- * @param {boolean} [options.enableCache] - Override cache setting
3417
- * @param {number} [options.depth=0] - Current rendering depth
3418
- * @returns {string} Rendered HTML string
3419
- *
3420
- * @example
3421
- * const html = renderer.render({
3422
- * div: {
3423
- * className: 'container',
3424
- * children: [
3425
- * { h1: { text: 'Title' } },
3426
- * { p: { text: 'Content' } }
3427
- * ]
3428
- * }
3429
- * });
3430
- */
3431
- render(component, options = {}) {
3432
- const config = { ...this.config, ...options };
3433
- this.startTiming();
3434
- try {
3435
- if (config.validateInput && !this.isValidComponent(component)) {
3436
- throw new Error("Invalid component structure");
3437
- }
3438
- const html = this.renderComponent(component, config, 0);
3439
- const finalHtml = config.minify ? minifyHtml(html, config) : html;
3440
- this.endTiming();
3441
- this.recordPerformance("render", this.metrics.startTime, false, {
3442
- cacheEnabled: config.enableCache
3443
- });
3444
- return finalHtml;
3445
- } catch (_error) {
3446
- this.recordError("render", _error);
3447
- throw _error;
3448
- }
3449
- }
3450
- /**
3451
- * Render a single component with full optimization pipeline
3452
- */
3453
- renderComponent(component, options, depth = 0) {
3454
- this.validateDepth(depth);
3455
- const { type, value } = this.processComponentType(component);
3456
- switch (type) {
3457
- case "empty":
3458
- return "";
3459
- case "text":
3460
- return escapeHtml2(value);
3461
- case "function":
3462
- const result = this.executeFunctionComponent(value, depth);
3463
- return this.renderComponent(result, options, depth + 1);
3464
- case "array":
3465
- return value.map((child) => this.renderComponent(child, options, depth + 1)).join("");
3466
- case "element":
3467
- const tagName = Object.keys(value)[0];
3468
- const elementContent = value[tagName];
3469
- return this.renderElement(tagName, elementContent, options, depth);
3470
- default:
3471
- this.recordError("renderComponent", new Error(`Unknown component type: ${type}`));
3472
- return "";
3473
- }
3474
- }
3475
- /**
3476
- * Render an HTML element with advanced caching and optimization
3477
- */
3478
- renderElement(tagName, element, options, depth = 0) {
3479
- const startTime = performance.now();
3480
- if (options.enableMonitoring && this.cache) {
3481
- }
3482
- if (options.enableCache && this.cache && RendererUtils.isStaticElement(element)) {
3483
- const cacheKey = `static:${tagName}:${JSON.stringify(element)}`;
3484
- const cached = this.cache.get("static", cacheKey);
3485
- if (cached) {
3486
- this.recordPerformance(tagName, startTime, true);
3487
- return cached.value;
3488
- }
3489
- }
3490
- if (typeof element === "string" || typeof element === "number" || typeof element === "boolean") {
3491
- const html2 = isVoidElement2(tagName) ? `<${tagName}>` : `<${tagName}>${escapeHtml2(String(element))}</${tagName}>`;
3492
- this.cacheIfStatic(tagName, element, html2, options);
3493
- this.recordPerformance(tagName, startTime, false);
3494
- return html2;
3495
- }
3496
- if (typeof element === "function") {
3497
- const result = this.executeFunctionComponent(element, depth);
3498
- return this.renderElement(tagName, result, options, depth);
3499
- }
3500
- if (element && typeof element === "object") {
3501
- return this.renderObjectElement(tagName, element, options, depth);
3502
- }
3503
- if (element === null || element === void 0) {
3504
- const html2 = isVoidElement2(tagName) ? `<${tagName}>` : `<${tagName}></${tagName}>`;
3505
- this.recordPerformance(tagName, startTime, false);
3506
- return html2;
3507
- }
3508
- const html = `<${tagName}>${escapeHtml2(String(element))}</${tagName}>`;
3509
- this.recordPerformance(tagName, startTime, false);
3510
- return html;
3511
- }
3512
- /**
3513
- * Cache element if it's static
3514
- */
3515
- cacheIfStatic(tagName, element, html) {
3516
- if (this.config.enableCache && this.cache && RendererUtils.isStaticElement(element)) {
3517
- const cacheKey = `static:${tagName}:${JSON.stringify(element)}`;
3518
- this.cache.set("static", cacheKey, html, {
3519
- ttlMs: this.config.cacheTTL || 5 * 60 * 1e3,
3520
- // 5 minutes default
3521
- size: html.length
3522
- // Approximate size
3523
- });
3524
- }
3525
- }
3526
- /**
3527
- * Render complex object elements with attributes and children
3528
- */
3529
- renderObjectElement(tagName, element, options, depth = 0) {
3530
- const startTime = performance.now();
3531
- if (options.enableCache && this.cache) {
3532
- const cacheKey = RendererUtils.generateCacheKey(tagName, element);
3533
- if (cacheKey) {
3534
- const cached = this.cache.get(cacheKey);
3535
- if (cached) {
3536
- this.recordPerformance(tagName, startTime, true);
3537
- return cached;
3538
- }
3539
- }
3540
- }
3541
- const { children, text, ...attributes } = element || {};
3542
- const attributeString = formatAttributes2(attributes);
3543
- const openingTag = attributeString ? `<${tagName} ${attributeString}>` : `<${tagName}>`;
3544
- let textContent = "";
3545
- if (text !== void 0) {
3546
- const isScript = tagName === "script";
3547
- const isStyle = tagName === "style";
3548
- const isRawTag = isScript || isStyle;
3549
- const raw = typeof text === "function" ? String(text()) : String(text);
3550
- if (isRawTag) {
3551
- const safe = raw.replace(/<\/(script)/gi, "<\\/$1").replace(/<\/(style)/gi, "<\\/$1").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
3552
- textContent = safe;
3553
- } else {
3554
- textContent = escapeHtml2(raw);
3555
- }
3556
- }
3557
- let childrenHtml = "";
3558
- if (hasChildren(element)) {
3559
- const normalizedChildren = normalizeChildren(children);
3560
- childrenHtml = normalizedChildren.map((child) => this.renderComponent(child, options, depth + 1)).join("");
3561
- }
3562
- const html = `${openingTag}${textContent}${childrenHtml}</${tagName}>`;
3563
- if (options.enableCache && this.cache && RendererUtils.isCacheable(element, options)) {
3564
- const cacheKey = RendererUtils.generateCacheKey(tagName, element);
3565
- if (cacheKey) {
3566
- this.cache.set(cacheKey, html);
3567
- }
3568
- }
3569
- this.recordPerformance(tagName, startTime, false);
3570
- return html;
3571
- }
3572
- };
3573
- function render2(component, options = {}) {
3574
- const mergedOptions = {
3575
- enableCache: true,
3576
- enableMonitoring: false,
3577
- ...options
3578
- };
3579
- const renderer = new HTMLRenderer(mergedOptions);
3580
- return renderer.render(component, mergedOptions);
3581
- }
3582
-
3583
- // ../core/src/utils/render-utils.js
3584
- function renderWithMonitoring(component, options = {}) {
3585
- const {
3586
- enablePerformanceMonitoring = false
3587
- } = options;
3588
- let html;
3589
- if (enablePerformanceMonitoring) {
3590
- const renderId = performanceMonitor2.startRender();
3591
- html = render2(component);
3592
- performanceMonitor2.endRender(renderId);
3593
- } else {
3594
- html = render2(component);
3595
- }
3596
- return html;
3597
- }
3598
- function renderWithTemplate(component, options = {}) {
3599
- const {
3600
- template = "<!DOCTYPE html>\n{{content}}"
3601
- } = options;
3602
- const html = renderWithMonitoring(component, options);
3603
- return template.replace("{{content}}", html);
3604
- }
3605
- async function renderComponentFactory(componentFactory, factoryArgs, options = {}) {
3606
- const component = await Promise.resolve(
3607
- componentFactory(...factoryArgs)
3608
- );
3609
- if (!component) {
3610
- throw new Error("Component factory returned null/undefined");
3611
- }
3612
- return renderWithTemplate(component, options);
3613
- }
3614
-
3615
1
  // src/coherent-nextjs.js
2
+ import {
3
+ render,
4
+ performanceMonitor,
5
+ importPeerDependency,
6
+ renderComponentFactory
7
+ } from "@coherent.js/core";
3616
8
  function createCoherentNextHandler(componentFactory, options = {}) {
3617
9
  return async (req, res) => {
3618
10
  try {
@@ -3764,18 +156,4 @@ export {
3764
156
  createNextIntegration,
3765
157
  coherent_nextjs_default as default
3766
158
  };
3767
- /**
3768
- * Coherent.js - Object-Based Rendering Framework
3769
- * A pure JavaScript framework for server-side rendering using natural object syntax
3770
- *
3771
- * @version 2.0.0
3772
- * @author Coherent Framework Team
3773
- * @license MIT
3774
- */
3775
- /**
3776
- * Advanced caching system with memory management and smart invalidation for Coherent.js
3777
- *
3778
- * @module @coherent/performance/cache-manager
3779
- * @license MIT
3780
- */
3781
159
  //# sourceMappingURL=index.js.map