fluxel
Version:
An ultra-lightweight, high-performance library for efficient DOM building and dynamic web UIs
13 lines (12 loc) • 11.2 kB
JavaScript
/**
* fluxel-reactive v0.6.5
*
* Copyright (c) 2026 mtripg6666tdr
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @see https://github.com/mtripg6666tdr/fluxel
* @license MIT
*/
;var __Fluxel_esm=(()=>{var I=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var X=(e,t)=>{for(var r in t)I(e,r,{get:t[r],enumerable:!0})},Y=(e,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let l of K(t))!Q.call(e,l)&&l!==r&&I(e,l,{get:()=>t[l],enumerable:!(s=G(t,l))||s.enumerable});return e};var Z=e=>Y(I({},"__esModule",{value:!0}),e);var ce={};X(ce,{default:()=>le});Object.defineProperty(window,"Fluxel",{get:()=>__Fluxel_esm.default,configurable:!0});var x=new WeakMap;function T(e){var t;(t=x.get(e))===null||t===void 0||t.forEach(r=>r()),x.delete(e),e.childNodes.forEach(r=>T(r))}function $(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var v=class z{get value(){return this._accessor.get()}set value(t){this._accessor.set(t)}derive(t){let r={get:()=>t(this.value),set:s=>{throw new TypeError("Cannot set value of derived dependency")}};return new z(r,this.addDependency)}constructor(t,r){$(this,"_accessor",void 0),$(this,"addDependency",void 0),this._accessor=t,this.addDependency=r}};var k=["object","a","abbr","address","area","article","aside","audio","b","base","bdi","bdo","blockquote","body","br","button","canvas","caption","cite","code","col","colgroup","data","datalist","dd","del","details","dfn","dialog","div","dl","dt","em","embed","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","kbd","label","legend","li","link","main","map","mark","menu","meta","meter","nav","noscript","ol","optgroup","option","output","p","picture","pre","progress","q","rp","rt","ruby","s","samp","script","search","section","select","slot","small","source","span","strong","style","sub","summary","sup","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","track","u","ul","var","video","wbr"],H=k;function P(){return typeof window<"u"&&"fluxelH"in window?window.fluxelH:null}function b(e,t){if(typeof e!="string")throw new TypeError("Expected string for tagName, got ".concat(typeof e));let r=P(),s=r?r.getElementByEid("".concat(r.count++)):document.createElement(e);return L(s,t)}function L(e,t){let r=!!P(),s=null,l=null,a=null,h=!1,o=null;if(t)if(typeof t=="string"||Array.isArray(t)||t instanceof Node||t instanceof v){l=t;let n=O(t);a=n.children,h=n.variableChildrenLength,o=n.reactiveIndex}else{if(s=Object.assign(Object.create(null),t),"textContent"in s)throw new TypeError("textContent is not allowed");if("children"in s&&s.children!==null&&s.children!==void 0){l=s.children;let n=O(s.children);a=n.children,h=n.variableChildrenLength,o=n.reactiveIndex,delete s.children}}s=s||{},a=a||null;let f=new Map;for(let n in s)if(n.startsWith("on")&&s[n]){let i=n.slice(2).toLowerCase(),d=f.get(i)||[];f.set(i,d);let c=s[n];if(Array.isArray(c))c.forEach(p=>{if(typeof p=="function")d.push(p);else throw new TypeError("Expected function for event handler ".concat(n,", got ").concat(typeof p))});else if(typeof c=="function")d.push(c);else throw new TypeError("Expected function for event handler ".concat(n,", got ").concat(typeof c));delete s[n]}let u=Object.assign(Object.create(null),"style"in s?s.style:null);if(u&&typeof u=="object"&&!Array.isArray(u)){for(let n in u)if(u[n]instanceof v){let i=u[n];i.addDependency(e,()=>{n.startsWith("--")?e.style.setProperty(n,i.value):e.style[n]=i.value}),u[n]=i.value}}delete s.style;let w=new Set,C=n=>{if(!n)return;let i=n.trim();i&&w.add(i)};if(e.classList.forEach(n=>C(n)),s.classList){let n=c=>{if(typeof c=="string")C(c);else if("forEach"in c)c.forEach(p=>C(p));else if(c instanceof v){let p=c;p.addDependency(e,()=>{e.className="",w.clear(),n(p.value),w.forEach(g=>e.classList.add(g))}),n(p.value)}else throw new TypeError("Expected string or iterable for 'classList', got ".concat(typeof s.classList))},i=s.classList,d=Array.isArray(i)&&i.some(c=>c instanceof v)?new v({get:()=>i.map(c=>c instanceof v?c.value:c),set:()=>{throw new TypeError("Cannot set value of classList directly")}},(c,p)=>{i.forEach(g=>g instanceof v&&g.addDependency(c,p))}):s.classList;n(d),delete s.classList}"className"in s&&delete s.className;let m=s.dataset||null;if(m&&typeof m=="object"&&!Array.isArray(m)){for(let n in m)if(m[n]instanceof v){let i=m[n];i.addDependency(e,()=>{e.dataset[n]=i.value}),m[n]=i.value}}delete s.dataset;for(let n in s)if(s[n]instanceof v){let i=s[n];i.addDependency(e,()=>{e[n]=i.value}),r||(e[n]=i.value),delete s[n]}if(r||(Object.assign(e,s),e.style&&u&&(Object.assign(e.style,u),Object.keys(u).forEach(n=>{n.startsWith("--")&&e.style.setProperty(n,u[n])})),e.dataset&&m&&Object.assign(e.dataset,m),w.size>0&&w.forEach(n=>e.classList.add(n))),h&&l&&l instanceof v)l.addDependency(e,()=>{let n=ne(e.childNodes),i=O(l).children,d=W(n),c=W(i);if(n.length===i.length&&n.every((y,E)=>y.isSameNode(i[E])))return;let p=n.reduce((y,E,F)=>{var M;let R=(M=c.get(E))!==null&&M!==void 0?M:-1;if(R<0)return y;let S=new Set([F]),V=F;for(let N=R+1;N<i.length;N++){var j;let A=(j=d.get(i[N]))!==null&&j!==void 0?j:-1;A<V||(S.add(A),V=A)}return S.size>y.size?S:y},new Set),g=new Set;if(p.size!==n.length){for(let y=n.length-1;y>=0;y--)if(!p.has(y)){let E=n[y];e.removeChild(E),g.add(E)}}e.childNodes.length!==i.length&&(i.forEach((y,E)=>{g.delete(y),e.childNodes[E]?e.childNodes[E]!==y&&e.insertBefore(y,e.childNodes[E]):e.appendChild(y)}),g.forEach(y=>T(y)))});else if(o&&o.length>0&&l)for(let n of o){let i=l instanceof v?l:l[n];i instanceof v&&(i.addDependency(e,()=>{let d=O(i.value).children[0],c=e.childNodes[n];c!==d&&(e.replaceChild(d,c),T(c))}),a[n]=i.value)}return a&&(r?a.forEach((n,i)=>{n instanceof Text&&(e.childNodes[i]&&e.childNodes[i]instanceof Text?e.replaceChild(n,e.childNodes[i]):e.insertBefore(n,e.childNodes[i]))}):e.append(...a)),f.forEach((n,i)=>{n.forEach(d=>{e.addEventListener(i,d)})}),e}function O(e){let t=!1,r=[],s=(l,a=0,h=-1)=>{if(a>1)throw new TypeError("Children cannot be nested more than one level deep");if(typeof l=="string")return[document.createTextNode(l)];if(Array.isArray(l)){l=l.flat(1/0);let o=l.length;return l.flatMap((u,w)=>s(u,a+(o===1?0:1),w))}else{if(l instanceof Node)return[l];if(l instanceof v){let o=l.value;if(Array.isArray(o))return a===0&&(t=!0),o.flat(1/0).flatMap((f,u)=>s(f,a+1,u));if(o instanceof Node)return a===0&&(t=!0),r.push(h),[o];{let f=document.createTextNode(o);return l.addDependency(f,()=>{f.textContent=String(l.value)}),[f]}}else return[]}};return{children:s(e),variableChildrenLength:t,reactiveIndex:r}}b.fragment=function(e,t){let r=O(e),s=r.children,l=r.variableChildrenLength,a=r.reactiveIndex;if(l&&e instanceof v)return e.derive(h=>{let o=O(h).children;return o.forEach(f=>L(f,t)),o});{let h=s;if(h.forEach(o=>{o instanceof HTMLElement&&L(o,t)}),a.length>0)for(let o of a)h[o]=e[o].derive(f=>L(f,t));return h}};function ee(){let e=document.querySelector("style[data-fluxel]");if(e)return e;let t=document.createElement("style");return t.dataset.fluxel="true",document.head.appendChild(t),t}b.forwardStyle=function(e){P()||b.forwardStyleCache.includes(e)||(b.forwardStyleCache.push(e),typeof window<"u"&&(ee().textContent+=e))};b.forwardStyleCache=[];function te(){return"".concat(Date.now().toString(36)).concat(Math.random().toString(36).substring(2))}function ne(e){if(typeof Array.from=="function")return Array.from(e);let t=[];for(let r=0;r<e.length;r++)t.push(e[r]);return t}function W(e){let t=new Map;return e.forEach((r,s)=>{t.set(r,s)}),t}b.useUniqueString=function(e){let t=te();return e(t)};b.createComponent=function(e){return(t={})=>e(t)};b.createElement=b;var q=b;H.forEach(e=>{q[e]=t=>b(e,t)});var B=q;function re(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function se(e){for(var t=1;t<arguments.length;t++){var r=arguments[t]!=null?arguments[t]:{},s=Object.keys(r);typeof Object.getOwnPropertySymbols=="function"&&(s=s.concat(Object.getOwnPropertySymbols(r).filter(function(l){return Object.getOwnPropertyDescriptor(r,l).enumerable}))),s.forEach(function(l){re(e,l,r[l])})}return e}function ie(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);t&&(s=s.filter(function(l){return Object.getOwnPropertyDescriptor(e,l).enumerable})),r.push.apply(r,s)}return r}function oe(e,t){return t=t!=null?t:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ie(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}),e}var D=B,ae=["render","use","useWithMemo","listenTarget"];D.reactive=function(e,t){let r=Object.create(null),s=oe(se({},e),{render:(a,h=!1)=>{if(!h&&a&&s.listenTarget.dispatchEvent(new CustomEvent(a,{detail:{newValue:s[a]}})),a){var o;(o=r[a])===null||o===void 0||o.listeners.forEach(f=>f.call(null))}else for(let f in r)r[f].listeners.forEach(u=>u.call(null))},use:(a,h)=>{let o=-1,f=-1,u=[],w=new Map,C=new Map,m=(i,d,c=!1)=>{if(c){let p=JSON.stringify(d);if(C.has(p)){let y=C.get(p);return w.set(p,y),y}let g=i();return w.set(p,g),g}if(u[o]){if(u[o].deps.every((g,y)=>g===d[y]))return u[o++].memoValue;u[o].deps=d;let p=u[o].memoValue=i();return o++,p}else return u.push({deps:d,memoValue:i()}),u[o++].memoValue},n=h?i=>{o===-1&&f!==-1&&(f=o),o=0,w=new Map;let d=h(i,m);if(f!==-1&&f!==o)throw new TypeError("Memoization count mismatch");return C=w,d}:void 0;if(Array.isArray(a)){let i=a.map(d=>[d,s.use(d)]);return new v({get:()=>Object.fromEntries(i.map(d=>[d[0],d[1].value])),set:d=>{throw new TypeError("Invalid")}},(d,c)=>{let p=U(c);i.forEach(g=>g[1].addDependency(d,p))}).derive(n)}else{let i;return r[a]?i=r[a].dep:s[a]instanceof v?i=s[a]:(i=new v({get:()=>s[a],set:d=>{s[a]=d}},function(c,p){if(!r[a].listeners.includes(p)){r[a].listeners.push(p);let g=()=>{r[a].listeners=r[a].listeners.filter(y=>y!==p)};x.has(c)?x.get(c).add(g):x.set(c,new Set([g]))}}),r[a]={dep:i,listeners:[]}),n?i.derive(n):i}},useWithMemo:a=>{let h=null,o=s.use(a,(f,u)=>(h=u,f));return o.value,[o,h]},listenTarget:new EventTarget}),l=new Proxy(s,{set(a,h,o){if(ae.includes(h))throw new TypeError("Cannot set the specified property: ".concat(String(h)));let f=a[h];return f===o||(a[h]=o,a.listenTarget.dispatchEvent(new CustomEvent(h,{detail:{oldValue:f,newValue:o}})),a.render(h)),!0}});return t(l)};D.schedule=function(e){typeof window<"u"&&setTimeout(e,0)};D.mergeDependencies=function(...e){return new v({get:()=>e.map(t=>t.value),set:t=>{throw new TypeError("Invalid operation")}},(t,r)=>{let s=U(r);e.forEach(l=>l.addDependency(t,s))})};function U(e){let t=!1;return()=>{t||(t=!0,window.setTimeout(()=>{t=!1,e()},0))}}var J=D;var _=J;_.createStatefulComponent=function(e,t){return(r={})=>_.reactive(typeof t=="function"?t(r):t||{},s=>e(r,s))};var le=_;return Z(ce);})();