eleva 1.2.7-alpha → 1.2.8-alpha

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/eleva.umd.js CHANGED
@@ -1,3 +1,4 @@
1
+ /*! Eleva v1.2.8-alpha | MIT License | https://elevajs.com */
1
2
  (function (global, factory) {
2
3
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
4
  typeof define === 'function' && define.amd ? define(factory) :
@@ -6,43 +7,58 @@
6
7
 
7
8
  /**
8
9
  * @class 🔒 TemplateEngine
9
- * @classdesc Secure interpolation & dynamic attribute parsing.
10
- * Provides methods to parse template strings by replacing interpolation expressions
11
- * with dynamic data values and to evaluate expressions within a given data context.
10
+ * @classdesc A secure template engine that handles interpolation and dynamic attribute parsing.
11
+ * Provides a safe way to evaluate expressions in templates while preventing XSS attacks.
12
+ * All methods are static and can be called directly on the class.
13
+ *
14
+ * @example
15
+ * const template = "Hello, {{name}}!";
16
+ * const data = { name: "World" };
17
+ * const result = TemplateEngine.parse(template, data); // Returns: "Hello, World!"
12
18
  */
13
19
  class TemplateEngine {
14
20
  /**
15
- * Parses a template string and replaces interpolation expressions with corresponding values.
21
+ * @private {RegExp} Regular expression for matching template expressions in the format {{ expression }}
22
+ */
23
+ static expressionPattern = /\{\{\s*(.*?)\s*\}\}/g;
24
+
25
+ /**
26
+ * Parses a template string, replacing expressions with their evaluated values.
27
+ * Expressions are evaluated in the provided data context.
16
28
  *
17
- * @param {string} template - The template string containing expressions in the format `{{ expression }}`.
18
- * @param {Object<string, any>} data - The data object to use for evaluating expressions.
19
- * @returns {string} The resulting string with evaluated values.
29
+ * @public
30
+ * @static
31
+ * @param {string} template - The template string to parse.
32
+ * @param {Object} data - The data context for evaluating expressions.
33
+ * @returns {string} The parsed template with expressions replaced by their values.
34
+ * @example
35
+ * const result = TemplateEngine.parse("{{user.name}} is {{user.age}} years old", {
36
+ * user: { name: "John", age: 30 }
37
+ * }); // Returns: "John is 30 years old"
20
38
  */
21
39
  static parse(template, data) {
22
- if (!template || typeof template !== "string") return template;
23
- return template.replace(/\{\{\s*(.*?)\s*\}\}/g, (_, expr) => {
24
- return this.evaluate(expr, data);
25
- });
40
+ if (typeof template !== "string") return template;
41
+ return template.replace(this.expressionPattern, (_, expression) => this.evaluate(expression, data));
26
42
  }
27
43
 
28
44
  /**
29
- * Evaluates a JavaScript expression using the provided data context.
45
+ * Evaluates an expression in the context of the provided data object.
46
+ * Note: This does not provide a true sandbox and evaluated expressions may access global scope.
30
47
  *
31
- * @param {string} expr - The JavaScript expression to evaluate.
32
- * @param {Object<string, any>} data - The data context for evaluating the expression.
33
- * @returns {any} The result of the evaluated expression, or an empty string if undefined or on error.
48
+ * @public
49
+ * @static
50
+ * @param {string} expression - The expression to evaluate.
51
+ * @param {Object} data - The data context for evaluation.
52
+ * @returns {*} The result of the evaluation, or an empty string if evaluation fails.
53
+ * @example
54
+ * const result = TemplateEngine.evaluate("user.name", { user: { name: "John" } }); // Returns: "John"
55
+ * const age = TemplateEngine.evaluate("user.age", { user: { age: 30 } }); // Returns: 30
34
56
  */
35
- static evaluate(expr, data) {
36
- if (!expr || typeof expr !== "string") return expr;
57
+ static evaluate(expression, data) {
58
+ if (typeof expression !== "string") return expression;
37
59
  try {
38
- const compiledFn = new Function("data", `with(data) { return ${expr} }`);
39
- return compiledFn(data);
40
- } catch (error) {
41
- console.error(`Template evaluation error:`, {
42
- expression: expr,
43
- data,
44
- error: error.message
45
- });
60
+ return new Function("data", `with(data) { return ${expression}; }`)(data);
61
+ } catch {
46
62
  return "";
47
63
  }
48
64
  }
@@ -50,20 +66,26 @@
50
66
 
51
67
  /**
52
68
  * @class ⚡ Signal
53
- * @classdesc Fine-grained reactivity.
54
- * A reactive data holder that notifies registered watchers when its value changes,
55
- * enabling fine-grained DOM patching rather than full re-renders.
69
+ * @classdesc A reactive data holder that enables fine-grained reactivity in the Eleva framework.
70
+ * Signals notify registered watchers when their value changes, enabling efficient DOM updates
71
+ * through targeted patching rather than full re-renders.
72
+ *
73
+ * @example
74
+ * const count = new Signal(0);
75
+ * count.watch((value) => console.log(`Count changed to: ${value}`));
76
+ * count.value = 1; // Logs: "Count changed to: 1"
56
77
  */
57
78
  class Signal {
58
79
  /**
59
- * Creates a new Signal instance.
80
+ * Creates a new Signal instance with the specified initial value.
60
81
  *
82
+ * @public
61
83
  * @param {*} value - The initial value of the signal.
62
84
  */
63
85
  constructor(value) {
64
- /** @private {*} Internal storage for the signal's current value */
86
+ /** @private {T} Internal storage for the signal's current value, where T is the type of the initial value */
65
87
  this._value = value;
66
- /** @private {Set<function>} Collection of callback functions to be notified when value changes */
88
+ /** @private {Set<function(T): void>} Collection of callback functions to be notified when value changes, where T is the value type */
67
89
  this._watchers = new Set();
68
90
  /** @private {boolean} Flag to prevent multiple synchronous watcher notifications and batch updates into microtasks */
69
91
  this._pending = false;
@@ -72,7 +94,8 @@
72
94
  /**
73
95
  * Gets the current value of the signal.
74
96
  *
75
- * @returns {*} The current value.
97
+ * @public
98
+ * @returns {T} The current value, where T is the type of the initial value.
76
99
  */
77
100
  get value() {
78
101
  return this._value;
@@ -80,8 +103,11 @@
80
103
 
81
104
  /**
82
105
  * Sets a new value for the signal and notifies all registered watchers if the value has changed.
106
+ * The notification is batched using microtasks to prevent multiple synchronous updates.
83
107
  *
84
- * @param {*} newVal - The new value to set.
108
+ * @public
109
+ * @param {T} newVal - The new value to set, where T is the type of the initial value.
110
+ * @returns {void}
85
111
  */
86
112
  set value(newVal) {
87
113
  if (newVal !== this._value) {
@@ -92,9 +118,15 @@
92
118
 
93
119
  /**
94
120
  * Registers a watcher function that will be called whenever the signal's value changes.
121
+ * The watcher will receive the new value as its argument.
95
122
  *
96
- * @param {function(any): void} fn - The callback function to invoke on value change.
123
+ * @public
124
+ * @param {function(T): void} fn - The callback function to invoke on value change, where T is the value type.
97
125
  * @returns {function(): boolean} A function to unsubscribe the watcher.
126
+ * @example
127
+ * const unsubscribe = signal.watch((value) => console.log(value));
128
+ * // Later...
129
+ * unsubscribe(); // Stops watching for changes
98
130
  */
99
131
  watch(fn) {
100
132
  this._watchers.add(fn);
@@ -121,66 +153,105 @@
121
153
  }
122
154
 
123
155
  /**
124
- * @class 🎙️ Emitter
125
- * @classdesc Robust inter-component communication with event bubbling.
126
- * Implements a basic publish-subscribe pattern for event handling, allowing components
127
- * to communicate through custom events.
156
+ * @class 📡 Emitter
157
+ * @classdesc A robust event emitter that enables inter-component communication through a publish-subscribe pattern.
158
+ * Components can emit events and listen for events from other components, facilitating loose coupling
159
+ * and reactive updates across the application.
160
+ *
161
+ * @example
162
+ * const emitter = new Emitter();
163
+ * emitter.on('user:login', (user) => console.log(`User logged in: ${user.name}`));
164
+ * emitter.emit('user:login', { name: 'John' }); // Logs: "User logged in: John"
128
165
  */
129
166
  class Emitter {
130
167
  /**
131
168
  * Creates a new Emitter instance.
169
+ *
170
+ * @public
132
171
  */
133
172
  constructor() {
134
- /** @type {Object.<string, Function[]>} Storage for event handlers mapped by event name */
135
- this.events = {};
173
+ /** @private {Map<string, Set<function(any): void>>} Map of event names to their registered handler functions */
174
+ this._events = new Map();
136
175
  }
137
176
 
138
177
  /**
139
- * Registers an event handler for the specified event.
178
+ * Registers an event handler for the specified event name.
179
+ * The handler will be called with the event data when the event is emitted.
140
180
  *
141
- * @param {string} event - The name of the event.
142
- * @param {function(...any): void} handler - The function to call when the event is emitted.
181
+ * @public
182
+ * @param {string} event - The name of the event to listen for.
183
+ * @param {function(any): void} handler - The callback function to invoke when the event occurs.
184
+ * @returns {function(): boolean} A function to unsubscribe the event handler.
185
+ * @example
186
+ * const unsubscribe = emitter.on('user:login', (user) => console.log(user));
187
+ * // Later...
188
+ * unsubscribe(); // Stops listening for the event
143
189
  */
144
190
  on(event, handler) {
145
- (this.events[event] || (this.events[event] = [])).push(handler);
191
+ if (!this._events.has(event)) this._events.set(event, new Set());
192
+ this._events.get(event).add(handler);
193
+ return () => this.off(event, handler);
146
194
  }
147
195
 
148
196
  /**
149
- * Removes a previously registered event handler.
197
+ * Removes an event handler for the specified event name.
198
+ * If no handler is provided, all handlers for the event are removed.
150
199
  *
200
+ * @public
151
201
  * @param {string} event - The name of the event.
152
- * @param {function(...any): void} handler - The handler function to remove.
202
+ * @param {function(any): void} [handler] - The specific handler function to remove.
203
+ * @returns {void}
153
204
  */
154
205
  off(event, handler) {
155
- if (this.events[event]) {
156
- this.events[event] = this.events[event].filter(h => h !== handler);
206
+ if (!this._events.has(event)) return;
207
+ if (handler) {
208
+ const handlers = this._events.get(event);
209
+ handlers.delete(handler);
210
+ // Remove the event if there are no handlers left
211
+ if (handlers.size === 0) this._events.delete(event);
212
+ } else {
213
+ this._events.delete(event);
157
214
  }
158
215
  }
159
216
 
160
217
  /**
161
- * Emits an event, invoking all handlers registered for that event.
218
+ * Emits an event with the specified data to all registered handlers.
219
+ * Handlers are called synchronously in the order they were registered.
162
220
  *
163
- * @param {string} event - The event name.
164
- * @param {...any} args - Additional arguments to pass to the event handlers.
221
+ * @public
222
+ * @param {string} event - The name of the event to emit.
223
+ * @param {...any} args - Optional arguments to pass to the event handlers.
224
+ * @returns {void}
165
225
  */
166
226
  emit(event, ...args) {
167
- (this.events[event] || []).forEach(handler => handler(...args));
227
+ if (!this._events.has(event)) return;
228
+ this._events.get(event).forEach(handler => handler(...args));
168
229
  }
169
230
  }
170
231
 
171
232
  /**
172
233
  * @class 🎨 Renderer
173
- * @classdesc Handles DOM patching, diffing, and attribute updates.
174
- * Provides methods for efficient DOM updates by diffing the new and old DOM structures
175
- * and applying only the necessary changes.
234
+ * @classdesc A DOM renderer that handles efficient DOM updates through patching and diffing.
235
+ * Provides methods for updating the DOM by comparing new and old structures and applying
236
+ * only the necessary changes, minimizing layout thrashing and improving performance.
237
+ *
238
+ * @example
239
+ * const renderer = new Renderer();
240
+ * const container = document.getElementById("app");
241
+ * const newHtml = "<div>Updated content</div>";
242
+ * renderer.patchDOM(container, newHtml);
176
243
  */
177
244
  class Renderer {
178
245
  /**
179
246
  * Patches the DOM of a container element with new HTML content.
247
+ * This method efficiently updates the DOM by comparing the new content with the existing
248
+ * content and applying only the necessary changes.
180
249
  *
250
+ * @public
181
251
  * @param {HTMLElement} container - The container element to patch.
182
252
  * @param {string} newHtml - The new HTML content to apply.
183
- * @throws {Error} If container is not an HTMLElement or newHtml is not a string
253
+ * @returns {void}
254
+ * @throws {Error} If container is not an HTMLElement or newHtml is not a string.
184
255
  */
185
256
  patchDOM(container, newHtml) {
186
257
  if (!(container instanceof HTMLElement)) {
@@ -197,10 +268,14 @@
197
268
 
198
269
  /**
199
270
  * Diffs two DOM trees (old and new) and applies updates to the old DOM.
271
+ * This method recursively compares nodes and their attributes, applying only
272
+ * the necessary changes to minimize DOM operations.
200
273
  *
274
+ * @private
201
275
  * @param {HTMLElement} oldParent - The original DOM element.
202
276
  * @param {HTMLElement} newParent - The new DOM element.
203
- * @throws {Error} If either parent is not an HTMLElement
277
+ * @returns {void}
278
+ * @throws {Error} If either parent is not an HTMLElement.
204
279
  */
205
280
  diff(oldParent, newParent) {
206
281
  if (!(oldParent instanceof HTMLElement) || !(newParent instanceof HTMLElement)) {
@@ -247,10 +322,13 @@
247
322
 
248
323
  /**
249
324
  * Updates the attributes of an element to match those of a new element.
325
+ * Handles special cases for ARIA attributes, data attributes, and boolean properties.
250
326
  *
327
+ * @private
251
328
  * @param {HTMLElement} oldEl - The element to update.
252
329
  * @param {HTMLElement} newEl - The element providing the updated attributes.
253
- * @throws {Error} If either element is not an HTMLElement
330
+ * @returns {void}
331
+ * @throws {Error} If either element is not an HTMLElement.
254
332
  */
255
333
  updateAttributes(oldEl, newEl) {
256
334
  if (!(oldEl instanceof HTMLElement) || !(newEl instanceof HTMLElement)) {
@@ -305,101 +383,143 @@
305
383
  }
306
384
 
307
385
  /**
308
- * Defines the structure and behavior of a component.
309
386
  * @typedef {Object} ComponentDefinition
310
387
  * @property {function(Object<string, any>): (Object<string, any>|Promise<Object<string, any>>)} [setup]
311
- * Optional setup function that initializes the component's reactive state and lifecycle.
312
- * Receives props and context as an argument and should return an object containing the component's state.
313
- * Can return either a synchronous object or a Promise that resolves to an object for async initialization.
314
- *
388
+ * Optional setup function that initializes the component's state and returns reactive data
315
389
  * @property {function(Object<string, any>): string} template
316
- * Required function that defines the component's HTML structure.
317
- * Receives the merged context (props + setup data) and must return an HTML template string.
318
- * Supports dynamic expressions using {{ }} syntax for reactive data binding.
319
- *
390
+ * Required function that defines the component's HTML structure
320
391
  * @property {function(Object<string, any>): string} [style]
321
- * Optional function that defines component-scoped CSS styles.
322
- * Receives the merged context and returns a CSS string that will be automatically scoped to the component.
323
- * Styles are injected into the component's container and only affect elements within it.
324
- *
392
+ * Optional function that provides component-scoped CSS styles
325
393
  * @property {Object<string, ComponentDefinition>} [children]
326
- * Optional object that defines nested child components.
327
- * Keys are CSS selectors that match elements in the template where child components should be mounted.
328
- * Values are ComponentDefinition objects that define the structure and behavior of each child component.
394
+ * Optional object defining nested child components
395
+ */
396
+
397
+ /**
398
+ * @typedef {Object} ElevaPlugin
399
+ * @property {function(Eleva, Object<string, any>): void} install
400
+ * Function that installs the plugin into the Eleva instance
401
+ * @property {string} name
402
+ * Unique identifier name for the plugin
403
+ */
404
+
405
+ /**
406
+ * @typedef {Object} MountResult
407
+ * @property {HTMLElement} container
408
+ * The DOM element where the component is mounted
409
+ * @property {Object<string, any>} data
410
+ * The component's reactive state and context data
411
+ * @property {function(): void} unmount
412
+ * Function to clean up and unmount the component
329
413
  */
330
414
 
331
415
  /**
332
416
  * @class 🧩 Eleva
333
- * @classdesc Signal-based component runtime framework with lifecycle hooks, scoped styles, and plugin support.
334
- * Manages component registration, plugin integration, event handling, and DOM rendering.
417
+ * @classdesc A modern, signal-based component runtime framework that provides lifecycle hooks,
418
+ * scoped styles, and plugin support. Eleva manages component registration, plugin integration,
419
+ * event handling, and DOM rendering with a focus on performance and developer experience.
420
+ *
421
+ * @example
422
+ * const app = new Eleva("myApp");
423
+ * app.component("myComponent", {
424
+ * template: (ctx) => `<div>Hello ${ctx.props.name}</div>`,
425
+ * setup: (ctx) => ({ count: new Signal(0) })
426
+ * });
427
+ * app.mount(document.getElementById("app"), "myComponent", { name: "World" });
335
428
  */
336
429
  class Eleva {
337
430
  /**
338
- * Creates a new Eleva instance.
431
+ * Creates a new Eleva instance with the specified name and configuration.
339
432
  *
340
- * @param {string} name - The name of the Eleva instance.
341
- * @param {Object<string, any>} [config={}] - Optional configuration for the instance.
433
+ * @public
434
+ * @param {string} name - The unique identifier name for this Eleva instance.
435
+ * @param {Object<string, any>} [config={}] - Optional configuration object for the instance.
436
+ * May include framework-wide settings and default behaviors.
342
437
  */
343
438
  constructor(name, config = {}) {
344
- /** @type {string} The unique identifier name for this Eleva instance */
439
+ /** @public {string} The unique identifier name for this Eleva instance */
345
440
  this.name = name;
346
- /** @type {Object<string, any>} Optional configuration object for the Eleva instance */
441
+ /** @public {Object<string, any>} Optional configuration object for the Eleva instance */
347
442
  this.config = config;
348
- /** @type {Object<string, ComponentDefinition>} Object storing registered component definitions by name */
349
- this._components = {};
350
- /** @private {Array<Object>} Collection of installed plugin instances */
351
- this._plugins = [];
352
- /** @private {string[]} Array of lifecycle hook names supported by the component */
353
- this._lifecycleHooks = ["onBeforeMount", "onMount", "onBeforeUpdate", "onUpdate", "onUnmount"];
354
- /** @private {boolean} Flag indicating if component is currently mounted */
355
- this._isMounted = false;
356
- /** @private {Emitter} Instance of the event emitter for handling component events */
443
+ /** @public {Emitter} Instance of the event emitter for handling component events */
357
444
  this.emitter = new Emitter();
358
- /** @private {Signal} Instance of the signal for handling plugin and component signals */
445
+ /** @public {typeof Signal} Static reference to the Signal class for creating reactive state */
359
446
  this.signal = Signal;
360
- /** @private {Renderer} Instance of the renderer for handling DOM updates and patching */
447
+ /** @public {Renderer} Instance of the renderer for handling DOM updates and patching */
361
448
  this.renderer = new Renderer();
449
+
450
+ /** @private {Map<string, ComponentDefinition>} Registry of all component definitions by name */
451
+ this._components = new Map();
452
+ /** @private {Map<string, ElevaPlugin>} Collection of installed plugin instances by name */
453
+ this._plugins = new Map();
454
+ /** @private {string[]} Array of lifecycle hook names supported by components */
455
+ this._lifecycleHooks = ["onBeforeMount", "onMount", "onBeforeUpdate", "onUpdate", "onUnmount"];
456
+ /** @private {boolean} Flag indicating if the root component is currently mounted */
457
+ this._isMounted = false;
362
458
  }
363
459
 
364
460
  /**
365
461
  * Integrates a plugin with the Eleva framework.
462
+ * The plugin's install function will be called with the Eleva instance and provided options.
463
+ * After installation, the plugin will be available for use by components.
366
464
  *
367
- * @param {Object} plugin - The plugin object which should have an `install` function.
368
- * @param {Object<string, any>} [options={}] - Optional options to pass to the plugin.
369
- * @returns {Eleva} The Eleva instance (for chaining).
465
+ * @public
466
+ * @param {ElevaPlugin} plugin - The plugin object which must have an `install` function.
467
+ * @param {Object<string, any>} [options={}] - Optional configuration options for the plugin.
468
+ * @returns {Eleva} The Eleva instance (for method chaining).
469
+ * @example
470
+ * app.use(myPlugin, { option1: "value1" });
370
471
  */
371
472
  use(plugin, options = {}) {
372
- if (typeof plugin.install === "function") {
373
- plugin.install(this, options);
374
- }
375
- this._plugins.push(plugin);
473
+ plugin.install(this, options);
474
+ this._plugins.set(plugin.name, plugin);
376
475
  return this;
377
476
  }
378
477
 
379
478
  /**
380
- * Registers a component with the Eleva instance.
479
+ * Registers a new component with the Eleva instance.
480
+ * The component will be available for mounting using its registered name.
381
481
  *
382
- * @param {string} name - The name of the component.
482
+ * @public
483
+ * @param {string} name - The unique name of the component to register.
383
484
  * @param {ComponentDefinition} definition - The component definition including setup, template, style, and children.
384
- * @returns {Eleva} The Eleva instance (for chaining).
485
+ * @returns {Eleva} The Eleva instance (for method chaining).
486
+ * @throws {Error} If the component name is already registered.
487
+ * @example
488
+ * app.component("myButton", {
489
+ * template: (ctx) => `<button>${ctx.props.text}</button>`,
490
+ * style: () => "button { color: blue; }"
491
+ * });
385
492
  */
386
493
  component(name, definition) {
387
- this._components[name] = definition;
494
+ /** @type {Map<string, ComponentDefinition>} */
495
+ this._components.set(name, definition);
388
496
  return this;
389
497
  }
390
498
 
391
499
  /**
392
500
  * Mounts a registered component to a DOM element.
501
+ * This will initialize the component, set up its reactive state, and render it to the DOM.
393
502
  *
394
- * @param {HTMLElement} container - A DOM element where the component will be mounted.
395
- * @param {string|ComponentDefinition} compName - The name of the component to mount or a component definition.
503
+ * @public
504
+ * @param {HTMLElement} container - The DOM element where the component will be mounted.
505
+ * @param {string|ComponentDefinition} compName - The name of the registered component or a direct component definition.
396
506
  * @param {Object<string, any>} [props={}] - Optional properties to pass to the component.
397
- * @returns {object|Promise<object>} An object representing the mounted component instance, or a Promise that resolves to it for asynchronous setups.
398
- * @throws {Error} If the container is not found or if the component is not registered.
507
+ * @returns {Promise<MountResult>}
508
+ * A Promise that resolves to an object containing:
509
+ * - container: The mounted component's container element
510
+ * - data: The component's reactive state and context
511
+ * - unmount: Function to clean up and unmount the component
512
+ * @throws {Error} If the container is not found, or component is not registered.
513
+ * @example
514
+ * const instance = await app.mount(document.getElementById("app"), "myComponent", { text: "Click me" });
515
+ * // Later...
516
+ * instance.unmount();
399
517
  */
400
- mount(container, compName, props = {}) {
518
+ async mount(container, compName, props = {}) {
401
519
  if (!container) throw new Error(`Container not found: ${container}`);
402
- const definition = typeof compName === "string" ? this._components[compName] : compName;
520
+
521
+ /** @type {ComponentDefinition} */
522
+ const definition = typeof compName === "string" ? this._components.get(compName) : compName;
403
523
  if (!definition) throw new Error(`Component "${compName}" not registered.`);
404
524
  if (typeof definition.template !== "function") throw new Error("Component template must be a function");
405
525
 
@@ -429,6 +549,7 @@
429
549
  const context = {
430
550
  props,
431
551
  emitter: this.emitter,
552
+ /** @type {(v: any) => Signal} */
432
553
  signal: v => new this.signal(v),
433
554
  ...this._prepareLifecycleHooks()
434
555
  };
@@ -437,15 +558,18 @@
437
558
  * Processes the mounting of the component.
438
559
  *
439
560
  * @param {Object<string, any>} data - Data returned from the component's setup function.
440
- * @returns {object} An object with the container, merged context data, and an unmount function.
561
+ * @returns {MountResult} An object with the container, merged context data, and an unmount function.
441
562
  */
442
563
  const processMount = data => {
443
564
  const mergedContext = {
444
565
  ...context,
445
566
  ...data
446
567
  };
568
+ /** @type {Array<() => void>} */
447
569
  const watcherUnsubscribers = [];
570
+ /** @type {Array<MountResult>} */
448
571
  const childInstances = [];
572
+ /** @type {Array<() => void>} */
449
573
  const cleanupListeners = [];
450
574
 
451
575
  // Execute before hooks
@@ -478,9 +602,9 @@
478
602
  * When a Signal's value changes, the component will re-render to reflect the updates.
479
603
  * Stores unsubscribe functions to clean up watchers when component unmounts.
480
604
  */
481
- Object.values(data).forEach(val => {
605
+ for (const val of Object.values(data)) {
482
606
  if (val instanceof Signal) watcherUnsubscribers.push(val.watch(render));
483
- });
607
+ }
484
608
  render();
485
609
  return {
486
610
  container,
@@ -491,9 +615,9 @@
491
615
  * @returns {void}
492
616
  */
493
617
  unmount: () => {
494
- watcherUnsubscribers.forEach(fn => fn());
495
- cleanupListeners.forEach(fn => fn());
496
- childInstances.forEach(child => child.unmount());
618
+ for (const fn of watcherUnsubscribers) fn();
619
+ for (const fn of cleanupListeners) fn();
620
+ for (const child of childInstances) child.unmount();
497
621
  mergedContext.onUnmount && mergedContext.onUnmount();
498
622
  container.innerHTML = "";
499
623
  }
@@ -505,26 +629,31 @@
505
629
  }
506
630
 
507
631
  /**
508
- * Prepares default no-operation lifecycle hook functions.
632
+ * Prepares default no-operation lifecycle hook functions for a component.
633
+ * These hooks will be called at various stages of the component's lifecycle.
509
634
  *
510
- * @returns {Object<string, function(): void>} An object with keys for lifecycle hooks mapped to empty functions.
511
635
  * @private
636
+ * @returns {Object<string, function(): void>} An object mapping lifecycle hook names to empty functions.
637
+ * The returned object will be merged with the component's context.
512
638
  */
513
639
  _prepareLifecycleHooks() {
514
- return this._lifecycleHooks.reduce((acc, hook) => {
515
- acc[hook] = () => {};
516
- return acc;
517
- }, {});
640
+ /** @type {Object<string, () => void>} */
641
+ const hooks = {};
642
+ for (const hook of this._lifecycleHooks) {
643
+ hooks[hook] = () => {};
644
+ }
645
+ return hooks;
518
646
  }
519
647
 
520
648
  /**
521
649
  * Processes DOM elements for event binding based on attributes starting with "@".
522
- * Tracks listeners for cleanup during unmount.
650
+ * This method handles the event delegation system and ensures proper cleanup of event listeners.
523
651
  *
524
- * @param {HTMLElement} container - The container element in which to search for events.
525
- * @param {Object<string, any>} context - The current context containing event handler definitions.
526
- * @param {Array<Function>} cleanupListeners - Array to collect cleanup functions for each event listener.
527
652
  * @private
653
+ * @param {HTMLElement} container - The container element in which to search for event attributes.
654
+ * @param {Object<string, any>} context - The current component context containing event handler definitions.
655
+ * @param {Array<Function>} cleanupListeners - Array to collect cleanup functions for each event listener.
656
+ * @returns {void}
528
657
  */
529
658
  _processEvents(container, context, cleanupListeners) {
530
659
  const elements = container.querySelectorAll("*");
@@ -547,12 +676,14 @@
547
676
 
548
677
  /**
549
678
  * Injects scoped styles into the component's container.
679
+ * The styles are automatically prefixed to prevent style leakage to other components.
550
680
  *
551
- * @param {HTMLElement} container - The container element.
552
- * @param {string} compName - The component name used to identify the style element.
553
- * @param {function(Object<string, any>): string} [styleFn] - A function that returns CSS styles as a string.
554
- * @param {Object<string, any>} context - The current context for style interpolation.
555
681
  * @private
682
+ * @param {HTMLElement} container - The container element where styles should be injected.
683
+ * @param {string} compName - The component name used to identify the style element.
684
+ * @param {function(Object<string, any>): string} [styleFn] - Optional function that returns CSS styles as a string.
685
+ * @param {Object<string, any>} context - The current component context for style interpolation.
686
+ * @returns {void}
556
687
  */
557
688
  _injectStyles(container, compName, styleFn, context) {
558
689
  if (!styleFn) return;
@@ -567,30 +698,35 @@
567
698
 
568
699
  /**
569
700
  * Mounts child components within the parent component's container.
701
+ * This method handles the recursive mounting of nested components.
570
702
  *
571
- * @param {HTMLElement} container - The parent container element.
572
- * @param {Object<string, ComponentDefinition>} [children] - An object mapping child component selectors to their definitions.
573
- * @param {Array<object>} childInstances - An array to store the mounted child component instances.
574
703
  * @private
704
+ * @param {HTMLElement} container - The parent container element.
705
+ * @param {Object<string, ComponentDefinition>} [children] - Object mapping of child component selectors to their definitions.
706
+ * @param {Array<MountResult>} childInstances - Array to store the mounted child component instances.
707
+ * @returns {void}
575
708
  */
576
- _mountChildren(container, children, childInstances) {
577
- childInstances.forEach(child => child.unmount());
709
+ async _mountChildren(container, children, childInstances) {
710
+ for (const child of childInstances) child.unmount();
578
711
  childInstances.length = 0;
579
- Object.keys(children || {}).forEach(childSelector => {
580
- container.querySelectorAll(childSelector).forEach(childEl => {
712
+ if (!children) return;
713
+ for (const childSelector of Object.keys(children)) {
714
+ if (!childSelector) continue;
715
+ for (const childEl of container.querySelectorAll(childSelector)) {
716
+ if (!(childEl instanceof HTMLElement)) continue;
581
717
  const props = {};
582
- [...childEl.attributes].forEach(({
718
+ for (const {
583
719
  name,
584
720
  value
585
- }) => {
721
+ } of [...childEl.attributes]) {
586
722
  if (name.startsWith("eleva-prop-")) {
587
723
  props[name.replace("eleva-prop-", "")] = value;
588
724
  }
589
- });
590
- const instance = this.mount(childEl, children[childSelector], props);
725
+ }
726
+ const instance = await this.mount(childEl, children[childSelector], props);
591
727
  childInstances.push(instance);
592
- });
593
- });
728
+ }
729
+ }
594
730
  }
595
731
  }
596
732