@zenithbuild/runtime 0.2.1 → 0.5.0-beta.2.3

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/events.js ADDED
@@ -0,0 +1,59 @@
1
+ // ---------------------------------------------------------------------------
2
+ // events.js — Zenith Runtime V0
3
+ // ---------------------------------------------------------------------------
4
+ // Event binding system.
5
+ //
6
+ // For: <button data-zx-on-click="1">
7
+ //
8
+ // Algorithm:
9
+ // 1. Extract event name from attribute suffix ("click")
10
+ // 2. Resolve expression at index → must be a function
11
+ // 3. addEventListener directly
12
+ //
13
+ // No synthetic events.
14
+ // No delegation.
15
+ // No wrapping.
16
+ // ---------------------------------------------------------------------------
17
+
18
+ import { _registerListener } from './cleanup.js';
19
+ import { rethrowZenithRuntimeError, throwZenithRuntimeError } from './diagnostics.js';
20
+
21
+ /**
22
+ * Bind an event listener to a DOM element.
23
+ *
24
+ * @param {Element} element - The DOM element
25
+ * @param {string} eventName - The event name (e.g. "click")
26
+ * @param {() => any} exprFn - Pre-bound expression function that must resolve to a handler
27
+ */
28
+ export function bindEvent(element, eventName, exprFn) {
29
+ const resolved = exprFn();
30
+
31
+ if (typeof resolved !== 'function') {
32
+ throwZenithRuntimeError({
33
+ phase: 'bind',
34
+ code: 'BINDING_APPLY_FAILED',
35
+ message: `Event binding did not resolve to a function for "${eventName}"`,
36
+ marker: { type: `data-zx-on-${eventName}`, id: '<unknown>' },
37
+ path: `event:${eventName}`,
38
+ hint: 'Bind events to function references.'
39
+ });
40
+ }
41
+
42
+ const wrapped = function zenithBoundEvent(event) {
43
+ try {
44
+ return resolved.call(this, event);
45
+ } catch (error) {
46
+ rethrowZenithRuntimeError(error, {
47
+ phase: 'event',
48
+ code: 'EVENT_HANDLER_FAILED',
49
+ message: `Event handler failed for "${eventName}"`,
50
+ marker: { type: `data-zx-on-${eventName}`, id: '<unknown>' },
51
+ path: `event:${eventName}:${resolved.name || '<anonymous>'}`,
52
+ hint: 'Inspect handler logic and referenced state.'
53
+ });
54
+ }
55
+ };
56
+
57
+ element.addEventListener(eventName, wrapped);
58
+ _registerListener(element, eventName, wrapped);
59
+ }