@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/src/index.ts DELETED
@@ -1,488 +0,0 @@
1
- // Zenith Runtime
2
- // Ported from hydration_runtime.js
3
-
4
- // Exported Types
5
- export type Signal<T> = ((v?: T) => T) & { _isSignal: true };
6
- export type Memo<T> = Signal<T>;
7
- export type Ref<T> = { current: T };
8
- export type EffectFn = () => (void | (() => void));
9
- export type DisposeFn = () => void;
10
- export type Subscriber = { run: () => void; dependencies: Set<Set<any>>; isRunning: boolean };
11
- export type TrackingContext = { subscriber: Subscriber };
12
-
13
- export type MountCallback = () => void;
14
- export type UnmountCallback = () => void;
15
-
16
- // Global types for internal use
17
- declare global {
18
- interface Window {
19
- __ZENITH_RUNTIME__: any;
20
- __ZENITH_STATE__: any;
21
- __ZENITH_SCOPES__: any;
22
- __ZENITH_EXPRESSIONS__: any;
23
- __zenith: any;
24
- zenSignal: any;
25
- zenState: any;
26
- zenEffect: any;
27
- zenMemo: any;
28
- zenBatch: any;
29
- zenUntrack: any;
30
- zenRef: any;
31
- zenOnMount: any;
32
- zenOnUnmount: any;
33
- zenithHydrate: any;
34
- zenithNotify: any;
35
- zenithSubscribe: any;
36
- zenOrder: any;
37
- zenFixSVGNamespace: any;
38
- }
39
- }
40
-
41
- // Internal reactivity state
42
- let cE: any = null;
43
- let cS: any[] = [];
44
- let bD = 0;
45
- let isFlushing = false;
46
- let flushScheduled = false;
47
- const pE = new Set<any>();
48
-
49
- // Lifecycle State
50
- let isMounted = false;
51
- const unmountCallbacks = new Set<() => void>();
52
-
53
- // Public Reactivity Utilities
54
- export const trackDependency = (s: Set<any>) => { if (cE) { s.add(cE); cE.dependencies.add(s); } };
55
- export const notifySubscribers = (s: Set<any> | undefined) => {
56
- if (!s) return;
57
- const es = Array.from(s);
58
- for (const e of es) {
59
- if (e.isRunning) continue;
60
- if (bD > 0 || isFlushing) pE.add(e);
61
- else e.run();
62
- }
63
- };
64
- export const getCurrentContext = () => cE;
65
- export const pushContext = (e: any) => { cS.push(cE); cE = e; };
66
- export const popContext = () => { cE = cS.pop(); };
67
- export const cleanupContext = (e: any) => { for (const d of e.dependencies) d.delete(e); e.dependencies.clear(); };
68
- export const startBatch = () => { bD++; };
69
- export const endBatch = () => { bD--; if (bD === 0) flushEffects(); };
70
- export const isBatching = () => bD > 0;
71
- export const runUntracked = <T>(fn: () => T): T => {
72
- pushContext(null);
73
- try { return fn(); } finally { popContext(); }
74
- };
75
-
76
- // Public Lifecycle Utilities
77
- export const triggerMount = () => { isMounted = true; };
78
- export const triggerUnmount = () => {
79
- isMounted = false;
80
- executeUnmountCallbacks();
81
- };
82
- export const executeUnmountCallbacks = () => {
83
- for (const cb of unmountCallbacks) {
84
- try { cb(); } catch (e) { console.error('Error in unmount callback:', e); }
85
- }
86
- unmountCallbacks.clear();
87
- };
88
- export const getIsMounted = () => isMounted;
89
- export const getUnmountCallbackCount = () => unmountCallbacks.size;
90
- export const resetMountState = () => { isMounted = false; };
91
- export const resetUnmountState = () => { unmountCallbacks.clear(); };
92
-
93
- if (typeof window !== 'undefined') {
94
- window.__ZENITH_EXPRESSIONS__ = new Map();
95
- window.__ZENITH_SCOPES__ = {};
96
- }
97
-
98
- // Phase A3: Post-Mount Execution Hook
99
- const mountedScopes = new Set<string>();
100
-
101
- export function mountComponent(scopeId: string) {
102
- if (mountedScopes.has(scopeId)) return;
103
- mountedScopes.add(scopeId);
104
-
105
- const scope = window.__ZENITH_SCOPES__[scopeId];
106
- if (!scope) return;
107
-
108
- if (typeof scope.__run === 'function') {
109
- scope.__run();
110
- }
111
- }
112
-
113
- // Internal shorthand helpers (matching original compiled code requirements if any)
114
- const pC = pushContext;
115
- const oC = popContext;
116
- const tD = trackDependency;
117
- const nS = notifySubscribers;
118
- const cEf = cleanupContext;
119
-
120
- export function zenRoute() {
121
- if (typeof window === 'undefined') return { path: '/', slugs: [] };
122
- const path = window.location.pathname;
123
- return {
124
- path: path,
125
- slugs: path.split('/').filter(Boolean)
126
- };
127
- }
128
-
129
- function scheduleFlush() {
130
- if (flushScheduled) return;
131
- flushScheduled = true;
132
- queueMicrotask(() => {
133
- flushScheduled = false;
134
- flushEffects();
135
- });
136
- }
137
-
138
- function flushEffects() {
139
- if (isFlushing || bD > 0) return;
140
- isFlushing = true;
141
- try {
142
- while (pE.size > 0) {
143
- const efs = Array.from(pE);
144
- pE.clear();
145
- for (const e of efs) {
146
- if (!e.isRunning) e.run();
147
- }
148
- }
149
- } finally {
150
- isFlushing = false;
151
- }
152
- }
153
-
154
- export const signal = function (v: any) {
155
- const s = new Set<any>();
156
- function sig(nV?: any) {
157
- if (arguments.length === 0) { tD(s); return v; }
158
- if (nV !== v) { v = nV; nS(s); scheduleFlush(); }
159
- return v;
160
- }
161
- sig._isSignal = true; sig.toString = () => String(v); sig.valueOf = () => v;
162
- return sig;
163
- };
164
-
165
- export const state = function (o: any) {
166
- const subs = new Map<string, Set<any>>();
167
- function gS(p: string) { if (!subs.has(p)) subs.set(p, new Set()); return subs.get(p); }
168
- function notify(p: string) { nS(gS(p)); scheduleFlush(); }
169
- function subscribe(p: string, ef: any) { gS(p)!.add(ef); ef.dependencies.add(gS(p)); }
170
- function cP(obj: any, pPath = ''): any {
171
- if (obj === null || typeof obj !== 'object' || obj._isSignal) return obj;
172
- return new Proxy(obj, {
173
- get(t, p) {
174
- if (p === Symbol.for('zenith_notify')) return notify;
175
- if (p === Symbol.for('zenith_subscribe')) return subscribe;
176
- if (typeof p === 'symbol') return t[p];
177
- const path = pPath ? `${pPath}.${String(p)}` : String(p);
178
- tD(gS(path)!);
179
- const v = t[p];
180
- if (v !== null && typeof v === 'object' && !v._isSignal) return cP(v, path);
181
- return v;
182
- },
183
- set(t, p, nV) {
184
- if (typeof p === 'symbol') { t[p] = nV; return true; }
185
- const path = pPath ? `${pPath}.${String(p)}` : String(p);
186
- const oV = t[p];
187
- if (oV && typeof oV === 'function' && oV._isSignal) oV(nV);
188
- else if (oV !== nV) {
189
- t[p] = nV;
190
- nS(gS(path));
191
- const pts = path.split('.');
192
- for (let i = pts.length - 1; i >= 0; i--) {
193
- const pp = pts.slice(0, i).join('.');
194
- if (pp) nS(gS(pp));
195
- }
196
- scheduleFlush();
197
- }
198
- return true;
199
- }
200
- });
201
- }
202
- return cP(o);
203
- };
204
-
205
- export const effect = function (fn: () => any, opts: any = {}) {
206
- let cl: any, tm: any;
207
- const ef: any = {
208
- dependencies: new Set(),
209
- isRunning: false,
210
- run: () => {
211
- if (ef.isRunning) return;
212
- const schedule = opts.scheduler || ((f: any) => f());
213
- if (opts.debounce) {
214
- if (tm) clearTimeout(tm);
215
- tm = setTimeout(() => schedule(ex), opts.debounce);
216
- } else schedule(ex);
217
- }
218
- };
219
- function ex() {
220
- if (ef.isRunning) return;
221
- ef.isRunning = true;
222
- cEf(ef);
223
- pC(ef);
224
- try { if (cl) cl(); cl = fn(); }
225
- finally {
226
- oC();
227
- ef.isRunning = false;
228
- }
229
- }
230
- if (!opts.defer) ex();
231
- return () => { cEf(ef); if (cl) cl(); };
232
- };
233
-
234
- export const memo = function (fn: () => any) {
235
- const sig = signal(undefined);
236
- effect(() => sig(fn()));
237
- const m = () => sig(); m._isSignal = true; return m;
238
- };
239
-
240
- export const batch = function (fn: () => void) {
241
- startBatch();
242
- try { fn(); } finally { endBatch(); }
243
- };
244
-
245
- export const untrack = function (fn: () => any) {
246
- return runUntracked(fn);
247
- };
248
-
249
- export const ref = (i: any) => ({ current: i || null });
250
-
251
- export const onMount = (cb: () => void) => {
252
- if (typeof window !== 'undefined' && window.__zenith && window.__zenith.activeInstance) {
253
- window.__zenith.activeInstance.mountHooks.push(cb);
254
- } else {
255
- // Fallback for non-component context or SSR
256
- if (isMounted) cb();
257
- }
258
- };
259
- export const onUnmount = (cb: () => void) => {
260
- unmountCallbacks.add(cb);
261
- };
262
-
263
- // DOM Helper (hC)
264
- function hC(parent: Node, child: any) {
265
- if (child == null || child === false) return;
266
-
267
- let fn = child;
268
- let id: string | null = null;
269
- if (typeof child === 'object' && child.fn) {
270
- fn = child.fn;
271
- id = child.id;
272
- }
273
-
274
- const isTitle = parent && (parent as any).tagName && (parent as any).tagName.toLowerCase() === 'title';
275
-
276
- if (typeof fn === 'function') {
277
- if (isTitle) {
278
- const val = fn();
279
- if (val != null && val !== false) {
280
- const text = String(val);
281
- parent.appendChild(document.createTextNode(text));
282
- document.title = text;
283
- }
284
- effect(() => {
285
- const newVal = fn();
286
- if (newVal != null && newVal !== false) {
287
- const newText = String(newVal);
288
- if (document.title !== newText) {
289
- parent.textContent = newText;
290
- document.title = newText;
291
- }
292
- }
293
- }, { id: id ? `title-${id}` : 'title-sync' });
294
- } else {
295
- const ph = document.createComment('expr' + (id ? ':' + id : ''));
296
- parent.appendChild(ph);
297
- let curNodes: Node[] = [];
298
- effect(() => {
299
- const r = fn();
300
- curNodes.forEach(n => { if (n.parentNode) n.parentNode.removeChild(n); });
301
- curNodes = [];
302
- if (r == null || r === false) return;
303
- const items = Array.isArray(r) ? r.flat(Infinity) : [r];
304
- items.forEach(item => {
305
- if (item == null || item === false) return;
306
- const node = item instanceof Node ? item : document.createTextNode(String(item));
307
- if (ph.parentNode) {
308
- ph.parentNode.insertBefore(node, ph);
309
- curNodes.push(node);
310
- }
311
- });
312
- }, { id });
313
- }
314
- } else if (Array.isArray(child)) {
315
- child.flat(Infinity).forEach(c => hC(parent, c));
316
- } else {
317
- parent.appendChild(child instanceof Node ? child : document.createTextNode(String(child)));
318
- }
319
- }
320
-
321
- // Global Hydration
322
- export function hydrate(state: any, container: Element | Document = document, locals: any = {}) {
323
- const ir = (window as any).canonicalIR; if (!ir) return;
324
- window.__ZENITH_STATE__ = state;
325
- const rootScope = { state, props: {}, locals: locals };
326
- const nodes = ir(rootScope);
327
-
328
- function findTag(items: any, tag: string): any {
329
- const list = Array.isArray(items) ? items : [items];
330
- for (const item of list) {
331
- if (item instanceof Element && item.tagName.toLowerCase() === tag) return item;
332
- if (item instanceof DocumentFragment) {
333
- const found = findTag(Array.from(item.childNodes), tag);
334
- if (found) return found;
335
- }
336
- }
337
- return null;
338
- }
339
-
340
- const headNode = findTag(nodes, 'head');
341
- const bodyNode = findTag(nodes, 'body');
342
-
343
- if (headNode) {
344
- const headMount = document.head;
345
- const newTitle = headNode.querySelector('title');
346
- if (newTitle) {
347
- let oldTitle = headMount.querySelector('title');
348
- if (!oldTitle) {
349
- oldTitle = document.createElement('title');
350
- headMount.appendChild(oldTitle);
351
- }
352
- const titleContent = newTitle.childNodes.length > 0
353
- ? Array.from(newTitle.childNodes).map((n: any) => n.textContent).join('')
354
- : '';
355
- oldTitle.textContent = titleContent;
356
- document.title = titleContent;
357
- effect(() => {
358
- const text = oldTitle!.textContent?.trim();
359
- if (text && document.title !== text) {
360
- document.title = text;
361
- }
362
- }, { id: 'title-sync' });
363
- }
364
- headNode.querySelectorAll('meta').forEach((newMeta: Element) => {
365
- const name = newMeta.getAttribute('name');
366
- if (name) {
367
- const oldMeta = headMount.querySelector(`meta[name="${name}"]`);
368
- if (oldMeta) oldMeta.setAttribute('content', newMeta.getAttribute('content')!);
369
- else headMount.appendChild(newMeta.cloneNode(true));
370
- }
371
- });
372
- headNode.childNodes.forEach((n: Node) => {
373
- if ((n as Element).tagName === 'TITLE' || (n as Element).tagName === 'META') return;
374
- headMount.appendChild(n.cloneNode(true));
375
- });
376
- }
377
-
378
- const bodyMount = container === document ? document.body : (container as Element);
379
- if (bodyNode) {
380
- bodyMount.innerHTML = '';
381
- Array.from(bodyNode.childNodes).forEach(n => hC(bodyMount, n));
382
- } else {
383
- bodyMount.innerHTML = '';
384
- const items = Array.isArray(nodes) ? nodes : [nodes];
385
- items.forEach(n => hC(bodyMount, n));
386
- }
387
-
388
- for (const scopeId in window.__ZENITH_SCOPES__) {
389
- mountComponent(scopeId);
390
- }
391
-
392
- queueMicrotask(() => {
393
- flushEffects();
394
- const titleEl = document.querySelector('title');
395
- if (titleEl && titleEl.textContent) {
396
- const text = titleEl.textContent.trim();
397
- if (text && document.title !== text) {
398
- document.title = text;
399
- }
400
- }
401
- });
402
- }
403
-
404
- // Ordered Effects
405
- export function order(fn: () => void) {
406
- if (typeof fn === 'function') fn();
407
- }
408
-
409
- // Hyperscript
410
- let currentNamespace: string | null = null;
411
- export function h(tag: string, props: any, children: any) {
412
- const SVG_NS = 'http://www.w3.org/2000/svg';
413
- const SVG_TAGS = new Set(['svg', 'path', 'circle', 'ellipse', 'line', 'polygon', 'polyline', 'rect', 'g', 'defs', 'clipPath', 'mask', 'use', 'symbol', 'text', 'tspan', 'textPath', 'image', 'foreignObject', 'switch', 'desc', 'title', 'metadata', 'linearGradient', 'radialGradient', 'stop', 'pattern', 'filter', 'feBlend', 'feColorMatrix', 'feComponentTransfer', 'feComposite', 'feConvolveMatrix', 'feDiffuseLighting', 'feDisplacementMap', 'feFlood', 'feGaussianBlur', 'feImage', 'feMerge', 'feMergeNode', 'feMorphology', 'feOffset', 'feSpecularLighting', 'feTile', 'feTurbulence', 'animate', 'animateMotion', 'animateTransform', 'set', 'marker']);
414
-
415
- const isSvgTag = SVG_TAGS.has(tag) || SVG_TAGS.has(tag.toLowerCase());
416
- const useSvgNamespace = isSvgTag || currentNamespace === SVG_NS;
417
-
418
- const el = useSvgNamespace ? document.createElementNS(SVG_NS, tag) : document.createElement(tag);
419
-
420
- const previousNamespace = currentNamespace;
421
- if (tag === 'svg' || tag === 'SVG') {
422
- currentNamespace = SVG_NS;
423
- }
424
-
425
- if (props) {
426
- const setClass = (element: Element, value: any) => {
427
- if (useSvgNamespace && 'className' in element && typeof (element as any).className === 'object') {
428
- (element as any).className.baseVal = String(value || '');
429
- } else {
430
- (element as any).className = String(value || '');
431
- }
432
- };
433
-
434
- for (const [k, v] of Object.entries(props)) {
435
- if (k === 'ref') {
436
- if (v && typeof v === 'object' && 'current' in v) (v as any).current = el;
437
- else if (typeof v === 'string') {
438
- const s = window.__ZENITH_STATE__;
439
- if (s && s[v] && typeof s[v] === 'object' && 'current' in s[v]) s[v].current = el;
440
- }
441
- } else if (k.startsWith('on')) {
442
- let fn = v;
443
- if (v && typeof v === 'object' && (v as any).fn) fn = (v as any).fn;
444
- if (typeof fn === 'function') {
445
- el.addEventListener(k.slice(2).toLowerCase(), (e) => {
446
- const h = (fn as Function).call(el, e, el);
447
- if (typeof h === 'function') h.call(el, e, el);
448
- });
449
- }
450
- } else {
451
- let fn = v;
452
- let id = null;
453
- if (typeof v === 'object' && (v as any).fn) {
454
- fn = (v as any).fn;
455
- id = (v as any).id;
456
- }
457
- if (typeof fn === 'function') {
458
- effect(() => {
459
- const val = (fn as Function)();
460
- if (k === 'class' || k === 'className') setClass(el, val);
461
- else if (val == null || val === false) el.removeAttribute(k);
462
- else if (el.setAttribute) el.setAttribute(k, String(val));
463
- }, { id });
464
- } else {
465
- if (k === 'class' || k === 'className') setClass(el, v);
466
- else if (el.setAttribute) el.setAttribute(k, String(v));
467
- }
468
- }
469
- }
470
- }
471
- if (children) {
472
- const items = Array.isArray(children) ? children : [children];
473
- items.forEach(c => hC(el, c));
474
- }
475
-
476
- currentNamespace = previousNamespace;
477
- return el;
478
- }
479
-
480
- export function fragment(c: any) {
481
- const f = document.createDocumentFragment();
482
- const items = Array.isArray(c) ? c : [c];
483
- items.forEach(i => hC(f, i));
484
- return f;
485
- }
486
-
487
- export * from './dom-hydration.js';
488
- export * from './thin-runtime.js';
@@ -1,159 +0,0 @@
1
- /**
2
- * Thin Runtime
3
- *
4
- * Phase 8/9/10: Declarative runtime for DOM updates and event binding
5
- *
6
- * This runtime is purely declarative - it:
7
- * - Updates DOM nodes by ID
8
- * - Binds event handlers
9
- * - Reacts to state changes
10
- * - Does NOT parse templates or expressions
11
- * - Does NOT use eval, new Function, or with(window)
12
- */
13
-
14
- /**
15
- * Generate thin declarative runtime code
16
- *
17
- * This runtime is minimal and safe - it only:
18
- * 1. Updates DOM nodes using pre-compiled expression functions
19
- * 2. Binds event handlers by ID
20
- * 3. Provides reactive state updates
21
- *
22
- * All expressions are pre-compiled at build time.
23
- */
24
- export function generateThinRuntime(): string {
25
- return `
26
- // Zenith Thin Runtime (Phase 8/9/10)
27
- // Purely declarative - no template parsing, no eval, no with(window)
28
-
29
- (function() {
30
- 'use strict';
31
-
32
- /**
33
- * Update a single DOM node with expression result
34
- * Node is identified by data-zen-text or data-zen-attr-* attribute
35
- */
36
- function updateNode(node, expressionId, state, loaderData, props, stores) {
37
- const expression = window.__ZENITH_EXPRESSIONS__.get(expressionId);
38
- if (!expression) {
39
- console.warn('[Zenith] Expression not found:', expressionId);
40
- return;
41
- }
42
-
43
- try {
44
- const result = expression(state, loaderData, props, stores);
45
-
46
- // Update node based on attribute type
47
- if (node.hasAttribute('data-zen-text')) {
48
- // Text node update
49
- if (result === null || result === undefined || result === false) {
50
- node.textContent = '';
51
- } else {
52
- node.textContent = String(result);
53
- }
54
- } else {
55
- // Attribute update - determine attribute name from data-zen-attr-*
56
- const attrMatch = Array.from(node.attributes)
57
- .find(attr => attr.name.startsWith('data-zen-attr-'));
58
-
59
- if (attrMatch) {
60
- const attrName = attrMatch.name.replace('data-zen-attr-', '');
61
-
62
- if (attrName === 'class' || attrName === 'className') {
63
- node.className = String(result ?? '');
64
- } else if (attrName === 'style') {
65
- if (typeof result === 'string') {
66
- node.setAttribute('style', result);
67
- }
68
- } else if (attrName === 'disabled' || attrName === 'checked') {
69
- if (result) {
70
- node.setAttribute(attrName, '');
71
- } else {
72
- node.removeAttribute(attrName);
73
- }
74
- } else {
75
- if (result != null && result !== false) {
76
- node.setAttribute(attrName, String(result));
77
- } else {
78
- node.removeAttribute(attrName);
79
- }
80
- }
81
- }
82
- }
83
- } catch (error) {
84
- console.error('[Zenith] Error updating node:', expressionId, error);
85
- }
86
- }
87
-
88
- /**
89
- * Update all hydrated nodes
90
- * Called when state changes
91
- */
92
- function updateAll(state, loaderData, props, stores) {
93
- // Find all nodes with hydration markers
94
- const textNodes = document.querySelectorAll('[data-zen-text]');
95
- const attrNodes = document.querySelectorAll('[data-zen-attr-class], [data-zen-attr-style], [data-zen-attr-src], [data-zen-attr-href]');
96
-
97
- textNodes.forEach(node => {
98
- const expressionId = node.getAttribute('data-zen-text');
99
- if (expressionId) {
100
- updateNode(node, expressionId, state, loaderData, props, stores);
101
- }
102
- });
103
-
104
- attrNodes.forEach(node => {
105
- const attrMatch = Array.from(node.attributes)
106
- .find(attr => attr.name.startsWith('data-zen-attr-'));
107
- if (attrMatch) {
108
- const expressionId = attrMatch.value;
109
- if (expressionId) {
110
- updateNode(node, expressionId, state, loaderData, props, stores);
111
- }
112
- }
113
- });
114
- }
115
-
116
- /**
117
- * Bind event handlers
118
- * Handlers are pre-compiled and registered on window
119
- */
120
- function bindEvents(container) {
121
- container = container || document;
122
-
123
- const eventTypes = ['click', 'change', 'input', 'submit', 'focus', 'blur', 'keyup', 'keydown', 'mouseenter'];
124
-
125
- eventTypes.forEach(eventType => {
126
- const elements = container.querySelectorAll('[data-zen-' + eventType + ']');
127
- elements.forEach(element => {
128
- const handlerName = element.getAttribute('data-zen-' + eventType);
129
- if (!handlerName) return;
130
-
131
- // Remove existing handler
132
- const handlerKey = '__zen_' + eventType + '_handler';
133
- const existingHandler = element[handlerKey];
134
- if (existingHandler) {
135
- element.removeEventListener(eventType, existingHandler);
136
- }
137
-
138
- // Bind new handler (pre-compiled, registered on window)
139
- const handler = function(event) {
140
- const handlerFunc = window[handlerName];
141
- if (typeof handlerFunc === 'function') {
142
- handlerFunc(event, element);
143
- }
144
- };
145
-
146
- element[handlerKey] = handler;
147
- element.addEventListener(eventType, handler);
148
- });
149
- });
150
- }
151
-
152
- // Export to window
153
- if (typeof window !== 'undefined') {
154
- window.__zenith_updateAll = updateAll;
155
- window.__zenith_bindEvents = bindEvents;
156
- }
157
- })();
158
- `
159
- }