UNPKG

esor

Version:

Lightweight JavaScript framework for dynamic Web applications based on Web Components with auto-batching and optimized reconciliation

3 lines (2 loc) 8.12 kB
var q=Object.defineProperty;var v=e=>{throw TypeError(e)};var G=(e,t,n)=>t in e?q(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var M=(e,t,n)=>G(e,typeof t!="symbol"?t+"":t,n),I=(e,t,n)=>t.has(e)||v("Cannot "+n);var T=(e,t,n)=>(I(e,t,"read from private field"),n?n.call(e):t.get(e)),N=(e,t,n)=>t.has(e)?v("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(e):t.set(e,n);var w=(e,t,n)=>(I(e,t,"access private method"),n);var k=null,S=0,d=null,L=!1;function z(){for(L=!1;d;){let e=d;d=null;for(let t of e)t()}}var D=e=>{let t=e,n=new Set,r=(...a)=>{if(!a.length)return k&&n.add(k),t;let c=a[0];if(t!==c)if(t=c,S){d||(d=new Set);for(let i of n)d.add(i)}else{d||(d=new Set);for(let i of n)d.add(i);L||(L=!0,queueMicrotask(z))}return t};return r._isSignal=!0,r},g=e=>{let t=()=>{k=t,e(),k=null};return t(),t},K=e=>{let t=D(void 0);return g(()=>t(e())),t},J=e=>{S++;let t=e();return!--S&&d&&z(),t};var F=[],Y=50,Z=()=>F.pop()||document.createElement("div"),Q=e=>{e.textContent="",e.innerHTML="",F.length<Y&&F.push(e)};function j(e,t){let n=new Map,r=e.children;for(let c=0;c<r.length;c++){let i=r[c];i._key!==void 0&&n.set(i._key,i)}let a=[];for(let c=0;c<t.length;c++){let i=t[c],l=i._key,o=n.get(l),u=Z();_(u,i);let f=u.firstElementChild;o&&f?(b(o,f),a.push(o),n.delete(l)):f&&(f._key=l,a.push(f)),Q(u)}for(let c of n.values())c._cleanup?.(),e.removeChild(c);for(let c=0;c<a.length;c++){let i=a[c],l=r[c];l!==i&&e.insertBefore(i,l||null)}}function P(e,t){return e.nodeType===t.nodeType&&(e.nodeType!==1||e.tagName===t.tagName)}function ee(e,t,n){let r=t.length,a=n.length;if(r<20&&a<20){let u=Math.max(r,a);for(let f=0;f<u;f++){let m=t[f],s=n[f];m?s?b(m,s):(m._cleanup?.(),e.removeChild(m)):e.appendChild(s.cloneNode(!0))}return}let c=0;for(;c<r&&c<a&&P(t[c],n[c]);)b(t[c],n[c]),c++;let i=r-1,l=a-1;for(;i>=c&&l>=c&&P(t[i],n[l]);)b(t[i],n[l]),i--,l--;if(c>i&&c<=l){let u=n[l+1]?.nextSibling||null;for(let f=c;f<=l;f++)e.insertBefore(n[f].cloneNode(!0),u);return}if(c>l){for(let u=c;u<=i;u++)t[u]._cleanup?.(),e.removeChild(t[u]);return}let o=Math.max(i-c+1,l-c+1);for(let u=0;u<o;u++){let f=c+u,m=c+u,s=f<=i?t[f]:null,p=m<=l?n[m]:null;!s&&p?e.insertBefore(p.cloneNode(!0),t[f]||null):s&&!p?(s._cleanup?.(),e.removeChild(s)):s&&p&&b(s,p)}}function b(e,t){let n=e.nodeType,r=t.nodeType;if(n===1&&r===1){if(e.tagName!==t.tagName){e.replaceWith(t.cloneNode(!0));return}let a=e.attributes,c=t.attributes,i=[];for(let l=a.length-1;l>=0;l--){let{name:o}=a[l];t.hasAttribute(o)||i.push(o)}for(let l=0;l<c.length;l++){let{name:o,value:u}=c[l];o==="value"||o==="checked"?e[o]!==u&&(e[o]=u):e.getAttribute(o)!==u&&e.setAttribute(o,u)}for(let l=0;l<i.length;l++)e.removeAttribute(i[l]);ee(e,e.childNodes,t.childNodes)}else n===3&&r===3?e.textContent!==t.textContent&&(e.textContent=t.textContent):e.replaceWith(t.cloneNode(!0))}var A="\uFEFF",R=new WeakMap,h=new WeakMap,H=20,te=(e,...t)=>{let n=R.get(e);if(!n){let u=document.createElement("template");u.innerHTML=e.join(A);let f=e.findIndex(m=>m.trim().endsWith("key="));n={template:u,keyAttrIndex:f},R.set(e,n)}let{template:r,keyAttrIndex:a}=n,c,i=[...t];a!==-1&&(c=t[a],i.splice(a,1));let l=i.some(u=>u?._isSignal||typeof u=="function"),o=i.length===0;return{template:r,values:i,_isTemplate:!0,_key:c,_isStatic:o,_hasReactiveValues:l}},O=(e,t,n=!0)=>{if(Array.isArray(t)){if(t.length&&t[0]?._key!==void 0){j(e,t);return}n&&!(e instanceof DocumentFragment)&&(e.textContent="");for(let r=0;r<t.length;r++){let a=document.createDocumentFragment();O(a,t[r],!1),e.appendChild(a)}return}n&&!(e instanceof DocumentFragment)&&(e.textContent=""),!(t==null||t===!1)&&(t._isTemplate?_(e,t):t instanceof Node?e.appendChild(t):e.appendChild(document.createTextNode(String(t))))},_=(e,{template:t,values:n,_isStatic:r,_hasReactiveValues:a})=>{if(r){let o=h.get(t);o?o=o.cloneNode(!0):(o=t.content.cloneNode(!0),h.size<H&&h.set(t,t.content.cloneNode(!0))),e.appendChild(o);return}if(!a&&h.size<H){let o=h.get(t);if(o){e.appendChild(o.cloneNode(!0));return}}let c=t.content.cloneNode(!0),i=0,l=o=>{if(o.nodeType===Node.TEXT_NODE&&o.nodeValue.includes(A)){let u=o.nodeValue.split(A),f=document.createDocumentFragment();for(let m=0;m<u.length;m++){if(m>0){let s=n[i++];if(s?._isSignal||typeof s=="function"){let p=document.createElement("span");f.appendChild(p);let y=s._isSignal?()=>s():s;g(()=>O(p,y()))}else O(f,s,!1)}u[m]&&f.appendChild(document.createTextNode(u[m]))}o.parentNode.replaceChild(f,o)}else if(o.nodeType===Node.ELEMENT_NODE){let u=[];for(let f=0;f<o.attributes.length;f++){let m=o.attributes[f];m.name!=="key"&&m.value===A&&u.push(m)}for(let f=0;f<u.length;f++){let m=u[f],s=n[i++],p=m.name;if(o.removeAttribute(p),p==="ref")typeof s=="function"?s(o):s&&(s.current=o);else if(p==="style"&&s&&typeof s=="object")typeof s=="function"?g(()=>Object.assign(o.style,s())):Object.assign(o.style,s);else if(p[0]==="o"&&p[1]==="n"){let y=p.slice(2).toLowerCase();typeof s=="function"&&(o._cleanup?.(),o.addEventListener(y,s),o._cleanup=()=>o.removeEventListener(y,s))}else if(s?._isSignal||typeof s=="function")if(o.tagName?.includes("-"))o._functionProps||(o._functionProps={}),o._functionProps[p]=s;else{let y=s._isSignal?()=>s():s;g(()=>{let x=y();p==="value"||p==="checked"||p==="selected"?o[p]=x:x==null||x===!1?o.removeAttribute(p):o.setAttribute(p,x===!0?"":x)})}else p==="value"||p==="checked"||p==="selected"?o[p]=s:s!=null&&s!==!1&&o.setAttribute(p,s===!0?"":s)}o.hasAttribute("key")&&o.removeAttribute("key");for(let f=0;f<o.childNodes.length;f++)l(o.childNodes[f])}};for(let o=0;o<c.childNodes.length;o++)l(c.childNodes[o]);!a&&h.size<H&&h.set(t,c.cloneNode(!0)),e.appendChild(c)};var E=null,U=["beforeMount","mount","beforeUpdate","update","destroy"],V=e=>{E=e,e._lifecycles=Object.fromEntries(U.map(t=>[t,[]])),e.runHook=t=>{let n=e._lifecycles?.[t];n?.length&&queueMicrotask(()=>{for(let r=0;r<n.length;r++)n[r].call(e)})}},W=(e,t)=>{if(!E?._lifecycles)throw new Error(`[Esor] Hook called outside ctx setup for "${e}"`);E._lifecycles[e].push(t)},B={};U.forEach(e=>{let t=e.startsWith("before")?e:`on${e[0].toUpperCase()}${e.slice(1)}`;B[t]=n=>W(e,n)});var ne=e=>{let t=e();return typeof t=="function"&&W("destroy",t),()=>{}},oe=()=>(E||console.warn("getCurrentContext called outside of component lifecycle"),E),{beforeMount:ce,onMount:se,beforeUpdate:re,onUpdate:ie,onDestroy:le}=B;var fe=e=>{if(e==null)return"";if(e==="true")return!0;if(e==="false")return!1;let t=e[0];if(t==="-"||t>="0"&&t<="9"){let n=+e;if(n===n)return n}if(t==="{"||t==="[")try{return JSON.parse(e)}catch{}return e},X=e=>{e._functionProps&&Object.assign(e.props,e._functionProps);let t=e.attributes;for(let n=0;n<t.length;n++){let{name:r,value:a}=t[n],c=r[0],i=r[1];c==="o"&&i==="n"||c==="r"&&i==="e"&&r[2]==="f"||a==="function"&&e._functionProps?.[r]||(e.props[r]=fe(a))}};var C=()=>C.cache||(C.cache=[...document.querySelectorAll("head style, head link[rel=stylesheet]")]);var ue=/^[a-z][a-z0-9]*-[a-z0-9-]*$/,ae=(e,t={})=>{var n,r,$,c;return c=class extends HTMLElement{constructor(){super();N(this,r);N(this,n,this.attachShadow({mode:t.shadowMode||"open"}));M(this,"props",Object.create(null));M(this,"_cleanup",[]);w(this,r,$).call(this),this.runHook("beforeMount")}connectedCallback(){this.runHook("mount")}disconnectedCallback(){this._cleanup.forEach(l=>l()),this._cleanup=[],this.runHook("destroy")}},n=new WeakMap,r=new WeakSet,$=function(){V(this),X(this),t.globalStyles&&C().forEach(o=>T(this,n).appendChild(o.cloneNode(!0)));let l=e?.call(this,this.props);_(T(this,n),l)},c},pe=(e,t,n={})=>{typeof customElements<"u"&&ue.test(e)&&!customElements.get(e)&&customElements.define(e,ae(t,n))};function me(e=null){let t=e;return(...n)=>n.length===0?t:t=n[0]}function de(e,t,n=null){let r=new CustomEvent(e,{detail:t,bubbles:!0,composed:!0,cancelable:!0});return n?.dispatchEvent(r),r}export{J as batch,ce as beforeMount,re as beforeUpdate,pe as component,K as computed,g as effect,de as emit,oe as getCurrentContext,te as html,le as onDestroy,ne as onEffect,se as onMount,ie as onUpdate,me as ref,D as signal}; //# sourceMappingURL=esor.min.js.map