UNPKG

jelenjs

Version:

Core runtime library for JelenJS - an experimental UI framework with fine-grained reactivity

3 lines (2 loc) 13.5 kB
const n="__JELENJS_REACTIVE_STATE__",t="undefined"!=typeof globalThis?(globalThis[n]||(globalThis[n]={currentEffect:null,currentContext:null,microtaskPending:!1,globalEffectId:0}),globalThis[n]):"undefined"!=typeof window?(window[n]||(window[n]={currentEffect:null,currentContext:null,microtaskPending:!1,globalEffectId:0}),window[n]):{currentEffect:null,currentContext:null,microtaskPending:!1,globalEffectId:0};let e=t.currentEffect,o=t.currentContext,r=t.microtaskPending;t.globalEffectId;let c=0;const u=Object.is;function f(n){return t.currentEffect,t.currentEffect=n,e=n,t.currentEffect}function i(n){return t.microtaskPending,t.microtaskPending=n,r=n,t.microtaskPending}const l=new Set;function a(n){2&n.state||(n.state|=2,l.add(n),n.id,l.size)}function s(){r?l.size:(i(!0),l.size,queueMicrotask(p))}function p(){if(i(!1),0===l.size)return;t.globalEffectId++,c=t.globalEffectId,l.size;const n=Array.from(l);l.clear();for(const t of n)t.state&=-3,!t.disposed&&1&t.state?(t.state&=-2,t.id,t()):t.disposed?t.id:1&t.state||t.id;l.size>0&&(l.size,s())}const d="__JELENJS_REACTIVE_STATE__",y="__JELENJS_SIGNAL_REGISTRY__",g="undefined"!=typeof globalThis?(globalThis[y]||(globalThis[y]=new Set),globalThis[y]):"undefined"!=typeof window?(window[y]||(window[y]=new Set),window[y]):new Set,b=Symbol("signalGetter");function m(n){return"function"==typeof n&&(!0===n[b]||g.has(n))}function w(n,t=u){Math.random().toString(36).substr(2,5);const o={value:n,subscribers:new Set,observers:null,observerSlots:null};function r(){const n="undefined"!=typeof globalThis&&globalThis[d]?globalThis[d].currentEffect:"undefined"!=typeof window&&window[d]?window[d].currentEffect:e;return null!==n&&(n.id,o.subscribers.add(n),n.sources.length,n.sources.push(o),n.sourceSlots.push(o.subscribers.size-1),n.id),o.value}return r[b]=!0,g.add(r),r[b],g.size,[r,function(n){if("function"==typeof n&&(n=n(o.value)),!t(o.value,n)){if(o.value,o.value=n,0===o.subscribers.size)return o.value;o.subscribers.size;const t=Array.from(o.subscribers);for(const n of t)n&&!n.disposed&&(n.state|=1,a(n),n.id);s()}return o.value}]}function $(n){const r=()=>{if(r.disposed)return void r.id;if(r.id,r.cleanupFns&&r.cleanupFns.length>0){r.id,r.cleanupFns.length;for(let n=0;n<r.cleanupFns.length;n++)r.cleanupFns[n]();r.cleanupFns=[]}if(r.sources&&r.sources.length>0){r.id,r.sources.length;for(let n=0;n<r.sources.length;n++)r.sources[n].subscribers.delete(r);r.sources.length=0,r.sourceSlots.length=0}if(r.running)return void r.id;r.running=!0;const t=e;f(r),r.id;try{const t=n();"function"==typeof t&&(r.id,r.cleanupFns.push(t))}catch(n){}finally{f(t),r.running=!1,r.id}};return r.id=(t.globalEffectId++,t.globalEffectId,t.globalEffectId,t.globalEffectId),r.disposed=!1,r.running=!1,r.state=0,r.cleanupFns=[],r.sources=[],r.sourceSlots=[],r.updatedAt=null,r.parentContext=o,r.id,o&&o.effects&&(o.effects.add(r),r.id),r.dispose=()=>{if(r.disposed)r.id;else{if(r.id,r.disposed=!0,r.cleanupFns.length>0){r.id,r.cleanupFns.length;for(let n=0;n<r.cleanupFns.length;n++)r.cleanupFns[n]();r.cleanupFns=[]}if(r.sources&&r.sources.length>0){r.id,r.sources.length;for(let n=0;n<r.sources.length;n++)r.sources[n].subscribers.delete(r);r.sources.length=0,r.sourceSlots.length=0}r.parentContext&&r.parentContext.effects&&(r.parentContext.effects.delete(r),r.id)}},r.id,r(),r.dispose}function E(n){const[t,e]=w(void 0);return $((()=>{e(n())})),t}let h=0;function _(n){h++;try{return n()}finally{h--}}var S=Object.freeze({__proto__:null,CLEAN:0,PENDING:2,STALE:1,batch:_,createEffect:$,createMemo:E,createSignal:w,get currentEffect(){return e},flushPendingEffects:p,isBatching:function(){return h>0},scheduleFlush:s,setCurrentEffect:f});function T(n){return!(null==n||(!Array.isArray(n)||2!==n.length||"function"!=typeof n[0]||"function"!=typeof n[1])&&!m(n))}function x(n,t=!1){let e;if("function"==typeof n)try{e=n()}catch(t){e=n}else if(Array.isArray(n)&&"function"==typeof n[0])try{e=n[0]()}catch(t){e=n}else e=n;return t&&"boolean"==typeof e?e?"active":"":e}var A=Object.freeze({__proto__:null,DEBUG:!1,generateId:function(n=""){return n+Math.random().toString(36).substr(2,9)},getSignalValue:x,isSignal:T,useSignal:function(n){if("function"==typeof n)try{return n()}catch(t){return n}else if(Array.isArray(n)&&"function"==typeof n[0])try{return n[0]()}catch(t){return n}return n}});const C=new Map;function v(n,t,...e){const o=document.createElement(n);if(t)for(const[n,e]of Object.entries(t))if("style"===n&&"object"==typeof e)Object.assign(o.style,e);else if(n.startsWith("on")&&"function"==typeof e){const t=n.slice(2).toLowerCase();o.addEventListener(t,e)}else if(T(e)){const t=x(e);N(o,n,t),C.has(o)||C.set(o,new Map),C.get(o).set(n,e),$((()=>{const t=x(e);N(o,n,t)}))}else"class"===n||"className"===n?o.className=String(e):n.startsWith("data-")?o.setAttribute(n,String(e)):o[n]=e;for(const n of e)k(o,n);return o}function k(n,t){if(null!=t)if(Array.isArray(t))for(const e of t)k(n,e);else if(t instanceof Node)n.appendChild(t);else{if(null!=(e=t)&&"string"!=typeof e&&"number"!=typeof e&&"boolean"!=typeof e&&(T(e)||"function"==typeof e&&(m(e),1))){const e=document.createElement("span");return e.style.display="contents",n.appendChild(e),void $((()=>{let n;if(n="function"==typeof t?t():Array.isArray(t)&&"function"==typeof t[0]?t[0]():T(t)?x(t):t,e.innerHTML="",n instanceof Node)e.appendChild(n);else if(n instanceof Element)e.appendChild(n);else if(Array.isArray(n))for(const t of n)k(e,t);else!1===n||null==n||null!=n&&(e.textContent=String(n))}))}var e;n.appendChild(document.createTextNode(String(t)))}}function N(n,t,e){"class"===t||"className"===t?n.className=String(e):"style"===t&&"object"==typeof e?Object.assign(n.style,e):"disabled"===t||"checked"===t||"required"===t?e?n.setAttribute(t,""):n.removeAttribute(t):t.startsWith("data-")?n.setAttribute(t,String(e)):(n[t]=e,"object"!=typeof e&&void 0!==e&&n.setAttribute(t,String(e)))}function O(n,t){const e=n();return t.appendChild(e),e}function M(n){return document.querySelector(n)}var j=Object.freeze({__proto__:null,createApp:function(n,t="#app"){const e=M(t);e&&O(n,e)},h:v,mount:O,query:M,queryAll:function(n){return Array.from(document.querySelectorAll(n))}}),J=Object.freeze({__proto__:null});const F="__JELENJS_COMPONENT_STATE__",I="undefined"!=typeof globalThis?(globalThis[F]||(globalThis[F]={currentComponent:null,mountCallbacks:new WeakMap,unmountCallbacks:new WeakMap}),globalThis[F]):"undefined"!=typeof window?(window[F]||(window[F]={currentComponent:null,mountCallbacks:new WeakMap,unmountCallbacks:new WeakMap}),window[F]):{currentComponent:null,mountCallbacks:new WeakMap,unmountCallbacks:new WeakMap};function L(n){I.currentComponent=n}function R(n){"function"==typeof n&&I.currentComponent&&(I.unmountCallbacks.has(I.currentComponent)||I.unmountCallbacks.set(I.currentComponent,[]),I.unmountCallbacks.get(I.currentComponent).push(n))}function q(n){const t=I.mountCallbacks.get(n);t&&(t.forEach((n=>{try{n()}catch(n){}})),I.mountCallbacks.delete(n))}var V=Object.freeze({__proto__:null,getCurrentComponent:function(){return I.currentComponent},getCurrentElement:function(){return null},handleUnmount:function(n){const t=I.unmountCallbacks.get(n);t&&(t.forEach((n=>{try{n()}catch(n){}})),I.unmountCallbacks.delete(n))},markAsMounted:q,onMount:function(n){"function"==typeof n&&(I.currentComponent?(I.mountCallbacks.has(I.currentComponent)||I.mountCallbacks.set(I.currentComponent,[]),I.mountCallbacks.get(I.currentComponent).push(n)):setTimeout(n,0))},onUnmount:R,onUpdate:function(n){"function"==typeof n&&$((()=>{n()}))},setCurrentComponent:L,setCurrentElement:function(n){},useEffect:function(n,t){"function"==typeof n&&$((()=>{const t=n();"function"==typeof t&&R(t)}))}});function P(n){if("function"==typeof n)try{return n()}catch(t){return n}return T(n)?x(n):n}function U(n){return/\?\s*.*\s*:/.test(n)||/&&/.test(n)||/\|\|/.test(n)}function W(n){if("function"==typeof n)return n;if("string"==typeof n&&U(n))try{return new Function("count",`return ${n}`)}catch(t){return()=>n}return()=>n}function D(n){return null==n?[]:Array.isArray(n)?n.flatMap(D):"object"==typeof n&&null!==n&&("operator"in n&&"&&"===n.operator||"test"in n&&"consequent"in n&&"alternate"in n)?[()=>"operator"in n&&"&&"===n.operator?P(n.left)?n.right:null:"test"in n?P(n.test)?n.consequent:n.alternate:null]:"string"==typeof n&&U(n)?[W(n)]:("boolean"==typeof n||"function"==typeof n||T(n),[n])}function G(n,t={}){const{children:e,...o}=t;if("function"==typeof n){const t=n;L(t);try{const r=n({...o,children:e});return setTimeout((()=>{q(t)}),0),r}finally{L(null)}}return v(n,o,...void 0===e?[]:D(e))}const z=G,B=n=>{if(!n.children)return document.createDocumentFragment();const t=document.createDocumentFragment(),e=D(n.children);for(const n of e)if(n instanceof Node||n instanceof Element)t.appendChild(n);else if("function"==typeof n){const e=document.createElement("span");e.style.display="contents",t.appendChild(e),$((()=>{try{const t=n();e.innerHTML="",t instanceof Node?e.appendChild(t):null!=t&&(e.textContent=String(t))}catch(n){}}))}else null!=n&&t.appendChild(document.createTextNode(String(n)));return t};function H(n,t){const e=document.createElement("pre");e.style.cssText="\n background: #f0f0f0;\n border: 1px solid #ccc;\n padding: 10px;\n margin: 5px 0;\n border-radius: 4px;\n font-family: monospace;\n font-size: 12px;\n white-space: pre-wrap;\n overflow-x: auto;\n ";const o=()=>{let o=t?`${t}:\n`:"JSX Debug:\n";try{if("function"==typeof n){o+="Type: Function\n",o+=`Function: ${n.toString().substring(0,200)}${n.toString().length>200?"...":""}\n`;try{const t=n();o+=`Result: ${JSON.stringify(t,null,2)}\n`,o+=`Result Type: ${typeof t}\n`,t instanceof Element&&(o+=`Element Tag: ${t.tagName}\n`,o+=`Element HTML: ${t.outerHTML.substring(0,200)}${t.outerHTML.length>200?"...":""}\n`)}catch(n){o+=`Function Call Error: ${n.message}\n`}}else T(n)?(o+="Type: Signal\n",o+=`Signal Value: ${JSON.stringify(x(n),null,2)}\n`):n instanceof Element?(o+="Type: Element\n",o+=`Tag: ${n.tagName}\n`,o+=`HTML: ${n.outerHTML.substring(0,300)}${n.outerHTML.length>300?"...":""}\n`):(o+=`Type: ${typeof n}\n`,o+=`Value: ${JSON.stringify(n,null,2)}\n`)}catch(n){o+=`Debug Error: ${n.message}\n`}e.textContent=o};return o(),("function"==typeof n||T(n))&&$((()=>{o()})),e}function X(n,t){const e=document.createElement("div");e.style.cssText="\n background: #fff3cd;\n border: 1px solid #ffeaa7;\n padding: 10px;\n margin: 5px 0;\n border-radius: 4px;\n ";const o=document.createElement("h4");o.textContent=t||"JSX Transform Result",o.style.margin="0 0 10px 0",e.appendChild(o);const r=document.createElement("pre");r.style.cssText="\n background: #f8f9fa;\n padding: 8px;\n border-radius: 3px;\n font-family: monospace;\n font-size: 11px;\n overflow-x: auto;\n margin: 0;\n ";const c=(n,t=0)=>{const e=" ".repeat(t);if(null==n)return`${e}${n}\n`;if("string"==typeof n||"number"==typeof n||"boolean"==typeof n)return`${e}"${n}" (${typeof n})\n`;if("function"==typeof n){const t=n.toString(),o=t.substring(0,100).replace(/\n/g," ");return`${e}Function: ${o}${t.length>100?"...":""}\n`}if(Array.isArray(n)){let o=`${e}Array[${n.length}]:\n`;return n.forEach(((n,r)=>{o+=`${e} [${r}]: `,o+=c(n,t+2).trim()+"\n"})),o}if(n instanceof Element){let o=`${e}Element: <${n.tagName.toLowerCase()}`;if(n.attributes.length>0){o+=" ";for(let t=0;t<n.attributes.length;t++){const e=n.attributes[t];o+=`${e.name}="${e.value}" `}}if(o+=">\n",n.children.length>0){o+=`${e} children[${n.children.length}]:\n`;for(let e=0;e<n.children.length;e++)o+=c(n.children[e],t+2)}return o}if("object"==typeof n){let o=`${e}Object:\n`;for(const[r,u]of Object.entries(n))o+=`${e} ${r}: `,o+=c(u,t+2).trim()+"\n";return o}return`${e}${typeof n}: ${String(n)}\n`};return r.textContent=c(n),e.appendChild(r),e}function Y(n,t){const e=document.createElement("div");e.style.cssText="\n background: #e8f5e8;\n border: 1px solid #4caf50;\n padding: 10px;\n margin: 5px 0;\n border-radius: 4px;\n ";const o=document.createElement("h4");o.textContent=t||"Reactive Debug",o.style.margin="0 0 10px 0",e.appendChild(o);const r=document.createElement("div");r.style.fontFamily="monospace",r.style.fontSize="12px";const c=()=>{let t="";if(T(n))t+="Signal detected\n",t+=`Current value: ${JSON.stringify(x(n))}\n`,t+=`Value type: ${typeof x(n)}\n`;else if("function"==typeof n){t+="Function detected\n";try{const e=n();t+=`Function result: ${JSON.stringify(e)}\n`,t+=`Result type: ${typeof e}\n`}catch(n){t+=`Function error: ${n.message}\n`}}else t+="Static value\n",t+=`Value: ${JSON.stringify(n)}\n`,t+=`Type: ${typeof n}\n`;r.textContent=t};return c(),("function"==typeof n||T(n))&&$((()=>{c()})),e.appendChild(r),e}const K=w,Q=$,Z=E,{createApp:nn,mount:tn,h:en,query:on,queryAll:rn}=j,{onMount:cn,onUnmount:un,onUpdate:fn,useEffect:ln}=V,an="0.1.6";export{B as Fragment,an as VERSION,_ as batch,V as component,Z as computed,nn as createApp,$ as createEffect,E as createMemo,w as createSignal,H as debugJSX,Y as debugReactive,j as dom,Q as effect,en as h,G as jsx,z as jsxs,tn as mount,cn as onMount,un as onUnmount,fn as onUpdate,on as query,rn as queryAll,S as reactive,J as runtime,X as showJSXTransform,K as signal,ln as useEffect,A as utils}; //# sourceMappingURL=index.min.js.map