UNPKG

jelenjs

Version:

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

3 lines (2 loc) 13.6 kB
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).JelenJS={})}(this,(function(n){"use strict";const t="__JELENJS_REACTIVE_STATE__",e="undefined"!=typeof globalThis?(globalThis[t]||(globalThis[t]={currentEffect:null,currentContext:null,microtaskPending:!1,globalEffectId:0}),globalThis[t]):"undefined"!=typeof window?(window[t]||(window[t]={currentEffect:null,currentContext:null,microtaskPending:!1,globalEffectId:0}),window[t]):{currentEffect:null,currentContext:null,microtaskPending:!1,globalEffectId:0};let o=e.currentEffect,r=e.currentContext,u=e.microtaskPending;e.globalEffectId;let c=0;const f=Object.is;function i(n){return e.currentEffect,e.currentEffect=n,o=n,e.currentEffect}function l(n){return e.microtaskPending,e.microtaskPending=n,u=n,e.microtaskPending}const a=new Set;function s(n){2&n.state||(n.state|=2,a.add(n),n.id,a.size)}function p(){u?a.size:(l(!0),a.size,queueMicrotask(d))}function d(){if(l(!1),0===a.size)return;e.globalEffectId++,c=e.globalEffectId,a.size;const n=Array.from(a);a.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;a.size>0&&(a.size,p())}const y="__JELENJS_REACTIVE_STATE__",b="__JELENJS_SIGNAL_REGISTRY__",g="undefined"!=typeof globalThis?(globalThis[b]||(globalThis[b]=new Set),globalThis[b]):"undefined"!=typeof window?(window[b]||(window[b]=new Set),window[b]):new Set,m=Symbol("signalGetter");function h(n){return"function"==typeof n&&(!0===n[m]||g.has(n))}function w(n,t=f){Math.random().toString(36).substr(2,5);const e={value:n,subscribers:new Set,observers:null,observerSlots:null};function r(){const n="undefined"!=typeof globalThis&&globalThis[y]?globalThis[y].currentEffect:"undefined"!=typeof window&&window[y]?window[y].currentEffect:o;return null!==n&&(n.id,e.subscribers.add(n),n.sources.length,n.sources.push(e),n.sourceSlots.push(e.subscribers.size-1),n.id),e.value}return r[m]=!0,g.add(r),r[m],g.size,[r,function(n){if("function"==typeof n&&(n=n(e.value)),!t(e.value,n)){if(e.value,e.value=n,0===e.subscribers.size)return e.value;e.subscribers.size;const t=Array.from(e.subscribers);for(const n of t)n&&!n.disposed&&(n.state|=1,s(n),n.id);p()}return e.value}]}function $(n){const t=()=>{if(t.disposed)return void t.id;if(t.id,t.cleanupFns&&t.cleanupFns.length>0){t.id,t.cleanupFns.length;for(let n=0;n<t.cleanupFns.length;n++)t.cleanupFns[n]();t.cleanupFns=[]}if(t.sources&&t.sources.length>0){t.id,t.sources.length;for(let n=0;n<t.sources.length;n++)t.sources[n].subscribers.delete(t);t.sources.length=0,t.sourceSlots.length=0}if(t.running)return void t.id;t.running=!0;const e=o;i(t),t.id;try{const e=n();"function"==typeof e&&(t.id,t.cleanupFns.push(e))}catch(n){}finally{i(e),t.running=!1,t.id}};return t.id=(e.globalEffectId++,e.globalEffectId,e.globalEffectId,e.globalEffectId),t.disposed=!1,t.running=!1,t.state=0,t.cleanupFns=[],t.sources=[],t.sourceSlots=[],t.updatedAt=null,t.parentContext=r,t.id,r&&r.effects&&(r.effects.add(t),t.id),t.dispose=()=>{if(t.disposed)t.id;else{if(t.id,t.disposed=!0,t.cleanupFns.length>0){t.id,t.cleanupFns.length;for(let n=0;n<t.cleanupFns.length;n++)t.cleanupFns[n]();t.cleanupFns=[]}if(t.sources&&t.sources.length>0){t.id,t.sources.length;for(let n=0;n<t.sources.length;n++)t.sources[n].subscribers.delete(t);t.sources.length=0,t.sourceSlots.length=0}t.parentContext&&t.parentContext.effects&&(t.parentContext.effects.delete(t),t.id)}},t.id,t(),t.dispose}function E(n){const[t,e]=w(void 0);return $((()=>{e(n())})),t}let _=0;function S(n){_++;try{return n()}finally{_--}}var T=Object.freeze({__proto__:null,CLEAN:0,PENDING:2,STALE:1,batch:S,createEffect:$,createMemo:E,createSignal:w,get currentEffect(){return o},flushPendingEffects:d,isBatching:function(){return _>0},scheduleFlush:p,setCurrentEffect:i});function x(n){return!(null==n||(!Array.isArray(n)||2!==n.length||"function"!=typeof n[0]||"function"!=typeof n[1])&&!h(n))}function A(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 C=Object.freeze({__proto__:null,DEBUG:!1,generateId:function(n=""){return n+Math.random().toString(36).substr(2,9)},getSignalValue:A,isSignal:x,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 v=new Map;function k(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(x(e)){const t=A(e);O(o,n,t),v.has(o)||v.set(o,new Map),v.get(o).set(n,e),$((()=>{const t=A(e);O(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)N(o,n);return o}function N(n,t){if(null!=t)if(Array.isArray(t))for(const e of t)N(n,e);else if(t instanceof Node)n.appendChild(t);else{if(null!=(e=t)&&"string"!=typeof e&&"number"!=typeof e&&"boolean"!=typeof e&&(x(e)||"function"==typeof e&&(h(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]():x(t)?A(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)N(e,t);else!1===n||null==n||null!=n&&(e.textContent=String(n))}))}var e;n.appendChild(document.createTextNode(String(t)))}}function O(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 M(n,t){const e=n();return t.appendChild(e),e}function j(n){return document.querySelector(n)}var J=Object.freeze({__proto__:null,createApp:function(n,t="#app"){const e=j(t);e&&M(n,e)},h:k,mount:M,query:j,queryAll:function(n){return Array.from(document.querySelectorAll(n))}}),F=Object.freeze({__proto__:null});const I="__JELENJS_COMPONENT_STATE__",L="undefined"!=typeof globalThis?(globalThis[I]||(globalThis[I]={currentComponent:null,mountCallbacks:new WeakMap,unmountCallbacks:new WeakMap}),globalThis[I]):"undefined"!=typeof window?(window[I]||(window[I]={currentComponent:null,mountCallbacks:new WeakMap,unmountCallbacks:new WeakMap}),window[I]):{currentComponent:null,mountCallbacks:new WeakMap,unmountCallbacks:new WeakMap};function R(n){L.currentComponent=n}function q(n){"function"==typeof n&&L.currentComponent&&(L.unmountCallbacks.has(L.currentComponent)||L.unmountCallbacks.set(L.currentComponent,[]),L.unmountCallbacks.get(L.currentComponent).push(n))}function V(n){const t=L.mountCallbacks.get(n);t&&(t.forEach((n=>{try{n()}catch(n){}})),L.mountCallbacks.delete(n))}var P=Object.freeze({__proto__:null,getCurrentComponent:function(){return L.currentComponent},getCurrentElement:function(){return null},handleUnmount:function(n){const t=L.unmountCallbacks.get(n);t&&(t.forEach((n=>{try{n()}catch(n){}})),L.unmountCallbacks.delete(n))},markAsMounted:V,onMount:function(n){"function"==typeof n&&(L.currentComponent?(L.mountCallbacks.has(L.currentComponent)||L.mountCallbacks.set(L.currentComponent,[]),L.mountCallbacks.get(L.currentComponent).push(n)):setTimeout(n,0))},onUnmount:q,onUpdate:function(n){"function"==typeof n&&$((()=>{n()}))},setCurrentComponent:R,setCurrentElement:function(n){},useEffect:function(n,t){"function"==typeof n&&$((()=>{const t=n();"function"==typeof t&&q(t)}))}});function U(n){if("function"==typeof n)try{return n()}catch(t){return n}return x(n)?A(n):n}function W(n){return/\?\s*.*\s*:/.test(n)||/&&/.test(n)||/\|\|/.test(n)}function D(n){if("function"==typeof n)return n;if("string"==typeof n&&W(n))try{return new Function("count",`return ${n}`)}catch(t){return()=>n}return()=>n}function G(n){return null==n?[]:Array.isArray(n)?n.flatMap(G):"object"==typeof n&&null!==n&&("operator"in n&&"&&"===n.operator||"test"in n&&"consequent"in n&&"alternate"in n)?[()=>"operator"in n&&"&&"===n.operator?U(n.left)?n.right:null:"test"in n?U(n.test)?n.consequent:n.alternate:null]:"string"==typeof n&&W(n)?[D(n)]:("boolean"==typeof n||"function"==typeof n||x(n),[n])}function z(n,t={}){const{children:e,...o}=t;if("function"==typeof n){const t=n;R(t);try{const r=n({...o,children:e});return setTimeout((()=>{V(t)}),0),r}finally{R(null)}}return k(n,o,...void 0===e?[]:G(e))}const B=z,H=w,X=$,Y=E,{createApp:K,mount:Q,h:Z,query:nn,queryAll:tn}=J,{onMount:en,onUnmount:on,onUpdate:rn,useEffect:un}=P;n.Fragment=n=>{if(!n.children)return document.createDocumentFragment();const t=document.createDocumentFragment(),e=G(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},n.VERSION="0.1.6",n.batch=S,n.component=P,n.computed=Y,n.createApp=K,n.createEffect=$,n.createMemo=E,n.createSignal=w,n.debugJSX=function(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 x(n)?(o+="Type: Signal\n",o+=`Signal Value: ${JSON.stringify(A(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||x(n))&&$((()=>{o()})),e},n.debugReactive=function(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 u=()=>{let t="";if(x(n))t+="Signal detected\n",t+=`Current value: ${JSON.stringify(A(n))}\n`,t+=`Value type: ${typeof A(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 u(),("function"==typeof n||x(n))&&$((()=>{u()})),e.appendChild(r),e},n.dom=J,n.effect=X,n.h=Z,n.jsx=z,n.jsxs=B,n.mount=Q,n.onMount=en,n.onUnmount=on,n.onUpdate=rn,n.query=nn,n.queryAll=tn,n.reactive=T,n.runtime=F,n.showJSXTransform=function(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 u=(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+=u(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+=u(n.children[e],t+2)}return o}if("object"==typeof n){let o=`${e}Object:\n`;for(const[r,c]of Object.entries(n))o+=`${e} ${r}: `,o+=u(c,t+2).trim()+"\n";return o}return`${e}${typeof n}: ${String(n)}\n`};return r.textContent=u(n),e.appendChild(r),e},n.signal=H,n.useEffect=un,n.utils=C})); //# sourceMappingURL=index.umd.min.js.map