@krate/runtime 0.1.0
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/LICENSE +201 -0
- package/README.md +3 -0
- package/dist/context.d.ts +11 -0
- package/dist/context.js +56 -0
- package/dist/css-modules.d.ts +7 -0
- package/dist/css-modules.js +1 -0
- package/dist/dom.d.ts +29 -0
- package/dist/dom.js +212 -0
- package/dist/error-overlay.d.ts +6 -0
- package/dist/error-overlay.js +113 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +8 -0
- package/dist/jsx-dev-runtime.d.ts +8 -0
- package/dist/jsx-dev-runtime.js +5 -0
- package/dist/jsx-runtime.d.ts +8 -0
- package/dist/jsx-runtime.js +29 -0
- package/dist/jsx-types.d.ts +1023 -0
- package/dist/jsx-types.js +1 -0
- package/dist/krate-hydrate.js +2 -0
- package/dist/krate-runtime.js +2 -0
- package/dist/reconcile.d.ts +35 -0
- package/dist/reconcile.js +312 -0
- package/dist/resource.d.ts +13 -0
- package/dist/resource.js +73 -0
- package/dist/router.d.ts +3 -0
- package/dist/router.js +519 -0
- package/dist/server-jsx-runtime.d.ts +1 -0
- package/dist/server-jsx-runtime.js +4 -0
- package/dist/server-renderer.d.ts +1 -0
- package/dist/server-renderer.js +468 -0
- package/dist/server.d.ts +87 -0
- package/dist/server.js +214 -0
- package/dist/signal.d.ts +9 -0
- package/dist/signal.js +178 -0
- package/package.json +66 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var __krate_exports=(()=>{var _=Object.defineProperty,De=Object.defineProperties,Ie=Object.getOwnPropertyDescriptor,Be=Object.getOwnPropertyDescriptors,Ke=Object.getOwnPropertyNames,ae=Object.getOwnPropertySymbols;var de=Object.prototype.hasOwnProperty,We=Object.prototype.propertyIsEnumerable;var ue=(e,t,n)=>t in e?_(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,pe=(e,t)=>{for(var n in t||(t={}))de.call(t,n)&&ue(e,n,t[n]);if(ae)for(var n of ae(t))We.call(t,n)&&ue(e,n,t[n]);return e},he=(e,t)=>De(e,Be(t));var Ve=(e,t)=>{for(var n in t)_(e,n,{get:t[n],enumerable:!0})},Ue=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let l of Ke(t))!de.call(e,l)&&l!==n&&_(e,l,{get:()=>t[l],enumerable:!(r=Ie(t,l))||r.enumerable});return e};var Fe=e=>Ue(_({},"__esModule",{value:!0}),e);var ft={};Ve(ft,{clearNodes:()=>Te,conditional:()=>it,createEffect:()=>w,createMemo:()=>Ge,createSignal:()=>Ee,disposeAll:()=>je,disposeNode:()=>v,forwardRef:()=>Je,h:()=>tt,hydrate:()=>ot,initRouter:()=>ke,insert:()=>et,mount:()=>nt,onCleanup:()=>Xe,onMount:()=>Ye,reconcile:()=>rt,reconcileTrees:()=>z,reinitRouter:()=>ct});var A=[],me=[],K=!1,M=new Set,W=[],O=!1;function ge(e){return{fn:e,cleanups:[],disposed:!1,sources:new Set,running:!1,rerun:!1,queued:!1}}function V(e){if(!e.disposed){e.disposed=!0;for(let t of e.sources)t.delete(e);e.sources.clear();for(let t of e.cleanups)t();e.cleanups.length=0}}function je(){for(let e of M)V(e);M.clear(),W.length=0,O=!1}function Ee(e){let t=e,n=new Set;return[()=>{let f=A[A.length-1];return f&&!f.disposed&&(n.add(f),f.sources.add(n)),t},f=>{let u=typeof f=="function"?f(t):f;if(u!==t){t=u;let E=Array.from(n);for(let h of E){if(h.disposed){n.delete(h);continue}if(h.running){h.rerun=!0;continue}h.queued||(h.queued=!0,W.push(h))}$e()}}]}function $e(){O||(O=!0,queueMicrotask(ze))}function ze(){O=!1;let e=W.splice(0);for(let t of e)t.disposed||(t.queued=!1,t.rerun=!1,q(t),t.rerun&&!t.disposed&&(t.rerun=!1,q(t)))}function q(e){e.running=!0;for(let t of e.cleanups)t();e.cleanups.length=0,A.push(e);try{let t=e.fn();typeof t=="function"&&e.cleanups.push(t)}finally{A.pop(),e.running=!1}}function w(e){let t=ge(e);return M.add(t),q(t),()=>{V(t),M.delete(t)}}function Xe(e){let t=A[A.length-1];t&&t.cleanups.push(e)}function Ye(e){me.push(e),K||(K=!0,queueMicrotask(Qe))}function Qe(){K=!1;let e=me.splice(0);for(let t of e)t()}function Ge(e){let[t,n]=Ee(void 0),r=ge(()=>{let f=e();n(f)});M.add(r),q(r);let l=A[A.length-1];return l&&l.cleanups.push(()=>{r.disposed||(V(r),M.delete(r))}),t}function Je(e){return t=>e(t,t.ref||(()=>{}))}var U=new WeakMap;function Ne(e,t){let n=U.get(e);n||(n=[],U.set(e,n)),n.push(t)}function v(e){let t=U.get(e);if(t){for(let n of t)n();t.length=0}for(let n=0;n<e.childNodes.length;n++)v(e.childNodes[n])}function Ze(e){let t=e.nextSibling;for(;t;){if(t.nodeType===8&&t.nodeValue==="k")return t;t=t.nextSibling}return null}function Te(e,t){let n=e.parentNode;if(!n)return;let r=e.nextSibling;for(;r&&r!==t;){let l=r.nextSibling;v(r),n.removeChild(r),r=l}}function et(e,t,n){let r=Ze(n);r?(Te(n,r),F(e,t,r)):F(e,t,null)}function F(e,t,n){if(!(t==null||t===!1||t===!0)){if(typeof t=="string"||typeof t=="number")e.insertBefore(document.createTextNode(String(t)),n);else if(t instanceof Node)e.insertBefore(t,n);else if(Array.isArray(t))for(let r of t)F(e,r,n)}}function ye(e,t,n){if(t==="class"||t==="className")n===!1||n===null||n===void 0?e.className="":e.className=String(n);else if(t==="style")typeof n=="string"?e.setAttribute("style",n):n&&typeof n=="object"&&Object.assign(e.style,n);else if(t.startsWith("on")&&typeof n=="function"){let r=t.slice(2).toLowerCase(),l=`__krate_${r}`,f=e[l];f&&e.removeEventListener(r,f),e.addEventListener(r,n),e[l]=n}else t==="ref"?typeof n=="function"&&n(e):t==="key"||n!=null&&n!==!1&&e.setAttribute(t,String(n))}function ve(e,t){if(!(t==null||t===!1||t===!0)){if(Array.isArray(t)){for(let n of t)ve(e,n);return}if(typeof t=="function"){let n=document.createTextNode("");e.appendChild(n),Ne(n,w(()=>{n.nodeValue=String(t())}));return}e.appendChild(t instanceof Node?t:document.createTextNode(String(t)))}}function tt(e,t,...n){if(typeof e=="function")return e(he(pe({},t||{}),{children:n}));let r=document.createElement(e);if(t)for(let[l,f]of Object.entries(t))typeof f=="function"&&!l.startsWith("on")?Ne(r,w(()=>ye(r,l,f()))):ye(r,l,f);for(let l of n)ve(r,l);return r}function be(e){if(typeof e=="string"){let t=document.querySelector(e);if(!t)throw new Error(`Container "${e}" not found`);return t}return e}function nt(e,t){let n=be(t),r=!1;w(()=>{r||(r=!0,n.appendChild(e()))})}function ot(e,t){let n=be(t);if(n.childNodes.length===0){n.appendChild(e());return}let r=e();r&&v(r)}function rt(e,t,n,r,l,f){let u=[],E=t.nextSibling;for(;E&&E!==n;)u.push(E),E=E.nextSibling;if(f){let h=new Map;for(let d of u){let a=d.__k;a!=null&&h.set(a,d)}let m=[],S=new Set;for(let d=0;d<r.length;d++){let a=r[d],g=f(a);S.add(g);let T=l(a,d);T.__k=g,m.push(T)}for(let[d,a]of h)!S.has(d)&&a.parentNode===e&&(v(a),e.removeChild(a));for(let d=0;d<m.length;d++){let a=m[d],g=d===0?t:m[d-1];a!==g.nextSibling&&e.insertBefore(a,g.nextSibling)}}else{let h=Math.max(u.length,r.length);for(let m=0;m<h;m++)if(m<r.length){let S=l(r[m],m);m<u.length?u[m]!==S&&(v(u[m]),e.replaceChild(S,u[m])):e.insertBefore(S,n)}else m<u.length&&u[m].parentNode===e&&(v(u[m]),e.removeChild(u[m]))}}function it(e,t,n,r,l,f){let u=null;w(()=>{let h=r()?l():f?f():null;u?h?(v(u),e.replaceChild(h,u)):(v(u),e.removeChild(u)):h&&e.insertBefore(h,n),u=h})}function C(e){if(e.nodeType===Node.COMMENT_NODE){let t=e.nodeValue||"";return t.indexOf("k:")===0?t:null}if(e.nodeType===Node.ELEMENT_NODE){let t=e,n=t.getAttribute("data-k");if(n)return n;let r=t.getAttribute("id");if(r)return"#"+r}return null}function Se(e,t){let n=new Set;for(let r=0;r<t.attributes.length;r++){let l=t.attributes[r].name,f=t.attributes[r].value;n.add(l),e.getAttribute(l)!==f&&e.setAttribute(l,f)}for(let r=0;r<e.attributes.length;r++){let l=e.attributes[r].name;n.has(l)||e.removeAttribute(l)}}function j(e,t){let n=document.importNode(t,!0);return e.parentNode&&(v(e),e.parentNode.replaceChild(n,e)),n}function xe(e,t){return e.nodeType!==t.nodeType?!1:e.nodeType===Node.ELEMENT_NODE?e.tagName===t.tagName:!0}function $(e,t){if(!xe(e,t))return j(e,t);if(e.nodeType===Node.TEXT_NODE)return e.nodeValue!==t.nodeValue&&(e.nodeValue=t.nodeValue),e;if(e.nodeType===Node.COMMENT_NODE)return e.nodeValue!==t.nodeValue?j(e,t):e;if(e.nodeType!==Node.ELEMENT_NODE)return e;let n=e,r=t;return C(n)!==C(r)?j(n,r):(Se(n,r),Ae(n,r),n)}function Ae(e,t){let n=Array.from(e.childNodes),r=Array.from(t.childNodes);if(n.length===r.length){let d=!0;for(let a=0;a<r.length;a++)if(C(n[a])!==C(r[a])){d=!1;break}if(d){for(let a=0;a<r.length;a++)$(n[a],r[a]);return}}let l=new Map,f=[];n.forEach((d,a)=>{let g=C(d);g!=null?l.has(g)||l.set(g,a):f.push(a)});let u=new Set,E=0,h=null,m=0,S=()=>{for(;m<f.length&&u.has(f[m]);)m++;return m<f.length?f[m]:-1};for(let d of r){let a=C(d),g;if(a!=null&&l.has(a)){let T=l.get(a);l.delete(a),u.add(T);let N=n[T];T<E?e.insertBefore(N,h):E=T,g=$(N,d)}else if(a==null){let T=S();if(T>=0&&xe(n[T],d)){u.add(T);let N=n[T];N.parentNode===e&&N.nextSibling!==h&&e.insertBefore(N,h),g=$(N,d)}else g=document.importNode(d,!0),e.insertBefore(g,h)}else g=document.importNode(d,!0),e.insertBefore(g,h);h=g.nextSibling}n.forEach((d,a)=>{!u.has(a)&&d.parentNode===e&&(v(d),e.removeChild(d))})}function z(e,t){if(e.nodeType!==Node.ELEMENT_NODE||t.nodeType!==Node.ELEMENT_NODE)return;let n=e,r=t;if(n.tagName!==r.tagName){let l=document.importNode(t,!0);n.parentNode&&n.parentNode.replaceChild(l,n);return}Se(n,r),Ae(n,r)}var X=!1,H,R=null,k=new Map,st=30,P=new Map,lt=50,we=/\/chunks\/runtime\.[^/]+\.js$/;function ke(){if(typeof document=="undefined"||X)return;X=!0;let e=".docs-content, main, #root",t=150,n=200,r=null,l=null,f=new Map,u=null;function E(o){for(let i of e.split(", ")){let s=o.querySelector(i.trim());if(s)return s}return o.body}function h(o){if(o.hasAttribute("data-krate-external"))return!1;let i=o.getAttribute("href");return!(!i||i.startsWith("#")||i.startsWith("javascript:")||o.origin!==location.origin||o.target==="_blank")}function m(o){k.has(o)||fetch(o,{headers:{Accept:"text/html"}}).then(i=>i.ok?i.text():null).then(i=>{if(i){if(k.size>=st){let s=k.keys().next().value;s!==void 0&&k.delete(s)}k.set(o,i)}}).catch(()=>{})}function S(){document.addEventListener("mouseover",g),document.addEventListener("focusin",g),typeof IntersectionObserver!="undefined"&&(u=new IntersectionObserver(o=>{for(let i of o)if(i.isIntersecting){let s=i.target,c=s.getAttribute("href");if(c&&h(s)){let p=new URL(c,location.origin).href;m(p)}u.unobserve(i.target)}},{rootMargin:"200px"}),d())}function d(){u&&document.querySelectorAll("a[data-prefetch]").forEach(o=>{u.observe(o)})}function a(){document.removeEventListener("mouseover",g),document.removeEventListener("focusin",g);for(let o of f.values())clearTimeout(o);f.clear(),u&&(u.disconnect(),u=null)}function g(o){let i=o.target.closest("a[data-prefetch]");if(!i||!h(i))return;let s=i.getAttribute("href");if(!s)return;let c=new URL(s,location.origin).href;if(k.has(c))return;let p=f.get(i);p&&clearTimeout(p);let y=setTimeout(()=>{f.delete(i),m(c)},n);f.set(i,y)}function T(o){let i=document.createTreeWalker(o,NodeFilter.SHOW_ELEMENT),s=i.nextNode();for(;s;){let c=s;for(let p of Object.keys(c))p.indexOf("__krate_")===0&&delete c[p];s=i.nextNode()}}let N=null;function Le(){let o=E(document),i=document.querySelector("template[data-krate-loading]");i&&i.content&&o&&(N=i.content.cloneNode(!0),document.body.appendChild(N)),o&&(o.style.transition=`opacity ${t}ms ease`,o.style.opacity="0")}function Y(){N&&N.parentNode&&N.parentNode.removeChild(N),N=null;let o=E(document);o&&(o.style.opacity="1")}function Q(o,i,s,c={}){let{replace:p=!1,scroll:y=!0,restoreScroll:L=!1}=c,D=new DOMParser().parseFromString(o,"text/html"),ie=E(D),I=E(document);if(!ie||!I){location.href=i;return}let se=D.querySelector("title");se&&(document.title=se.textContent||""),typeof globalThis.disposeAll=="function"&&globalThis.disposeAll(),T(I),z(I,ie);let le=D.querySelectorAll('link[rel="stylesheet"]'),ce=new Set;le.forEach(b=>{let x=b.getAttribute("href");x&&ce.add(x)}),document.querySelectorAll('link[rel="stylesheet"]').forEach(b=>{let x=b.getAttribute("href");x&&!ce.has(x)&&b.remove()});let fe=new Set;document.querySelectorAll('link[rel="stylesheet"]').forEach(b=>{fe.add(b.href)}),le.forEach(b=>{let x=b.getAttribute("href");if(x&&!fe.has(x)){let B=document.createElement("link");B.rel="stylesheet",B.href=x,document.head.appendChild(B)}}),Ce(s,i).then(()=>{Pe(),d()}),Y();let Re=location.pathname+location.search;if(p)history.replaceState({},"",i);else{if(!L){if(P.size>=lt){let b=P.keys().next().value;b!==void 0&&P.delete(b)}P.set(Re,window.scrollY)}history.pushState({},"",i)}G(i,{restoreScroll:L,scroll:y}),J(),window.dispatchEvent(new CustomEvent("krate:navigate",{detail:{url:i}}))}function Me(o){let i=o.indexOf("#");if(i<0||i===o.length-1)return!1;let s=o.slice(i);try{let c=document.querySelector(s);if(c)return c.scrollIntoView({behavior:"smooth",block:"start"}),!0}catch(c){return!1}return!1}function G(o,i){if(i.restoreScroll){let s=P.get(o);if(s!==void 0){window.scrollTo(0,s);return}}Me(o)||i.scroll&&window.scrollTo(0,0)}function J(){let o=location.pathname;document.querySelectorAll("a[data-krate-link]").forEach(i=>{let s=i.getAttribute("href");if(!s){i.removeAttribute("aria-current");return}let c=s;try{c=new URL(s,location.origin).pathname}catch(y){return}c===o||o==="/"&&(c===""||c==="/")||c!=="/"&&c.length>1&&o.startsWith(c)?i.setAttribute("aria-current","page"):i.removeAttribute("aria-current")})}function Ce(o,i){return o.length===0?Promise.resolve():Promise.all(o.map(s=>{let c=s.getAttribute("src");return c?He(c,i):Promise.resolve()})).then(()=>{})}function He(o,i){return new Promise(s=>{let c=document.createElement("script"),p=Z(o,i);c.src=p,we.test(p)||c.setAttribute("data-krate-spa",""),c.onload=()=>s(),c.onerror=()=>s(),document.body.appendChild(c)})}function Z(o,i){if(/^(https?:)?\/\//.test(o)||o.startsWith("/"))return new URL(o,i).href;let s=i;if(!s.endsWith("/")){let c=s.lastIndexOf("/");s=c>0?s.slice(0,c+1):"/"}return new URL(o,s).href}function Pe(){typeof globalThis.__krate_hydrate=="function"&&globalThis.__krate_hydrate()}function _e(o,i){typeof globalThis.disposeAll=="function"&&globalThis.disposeAll(),history.pushState({},"",i),document.open(),document.write(o),document.close()}function Oe(){return r!==null?Promise.resolve(r):fetch("/404.html",{headers:{Accept:"text/html"}}).then(o=>o.ok?o.text():null).then(o=>(r=o,o)).catch(()=>null)}function qe(){return l!==null?Promise.resolve(l):fetch("/500.html",{headers:{Accept:"text/html"}}).then(o=>o.ok?o.text():null).then(o=>(l=o,o)).catch(()=>null)}function ee(o,i){document.querySelectorAll("script[data-krate-spa]").forEach(p=>p.remove());let s=[],c=new Set;return document.querySelectorAll("script[src]").forEach(p=>{c.add(p.src)}),o.querySelectorAll("script[src]").forEach(p=>{let y=p.getAttribute("src");y&&!c.has(Z(y,i))&&s.push(p)}),s}function te(o,i={}){let s=k.get(o);if(s){let y=new DOMParser().parseFromString(s,"text/html");Q(s,o,ee(y,o),i);return}Le(),H&&H.abort(),H=new AbortController;let c=H.signal;fetch(o,{headers:{Accept:"text/html"},signal:c}).then(p=>p.ok?p.text().then(y=>{let re=new DOMParser().parseFromString(y,"text/html");Q(y,o,ee(re,o),i)}):(p.status>=500?qe():Oe()).then(L=>{L?_e(L,o):location.href=o})).catch(p=>{(p==null?void 0:p.name)!=="AbortError"&&(Y(),location.href=o)}).finally(()=>{H&&!c.aborted&&(H=void 0)})}let ne=o=>{if(o.defaultPrevented||o.button!==0||o.metaKey||o.ctrlKey||o.shiftKey||o.altKey)return;let i=o.target.closest("a[data-krate-link]");if(!i||!h(i))return;o.preventDefault();let s=i.getAttribute("href");if(!s)return;let c=new URL(s,location.origin).href,p=i.hasAttribute("data-krate-replace"),y=i.getAttribute("data-krate-scroll")!=="false";if(s===location.pathname+location.search+location.hash){p&&history.replaceState({},"",s),G(c,{scroll:y});return}te(c,{replace:p,scroll:y})},oe=()=>{te(location.href,{restoreScroll:!0})};document.addEventListener("click",ne),window.addEventListener("popstate",oe),S(),J(),document.querySelectorAll("script[src]").forEach(o=>{we.test(o.src)||o.setAttribute("data-krate-spa","")}),R=()=>{document.removeEventListener("click",ne),window.removeEventListener("popstate",oe),a()}}function ct(){R&&(R(),R=null),X=!1,ke()}return Fe(ft);})();
|
|
2
|
+
for (const k in __krate_exports) { window[k] = __krate_exports[k]; }
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var __krate_exports=(()=>{var D=Object.defineProperty,Ue=Object.defineProperties,Ve=Object.getOwnPropertyDescriptor,ze=Object.getOwnPropertyDescriptors,Ke=Object.getOwnPropertyNames,O=Object.getOwnPropertySymbols;var V=Object.prototype.hasOwnProperty,me=Object.prototype.propertyIsEnumerable;var be=(e,t,n)=>t in e?D(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,fe=(e,t)=>{for(var n in t||(t={}))V.call(t,n)&&be(e,n,t[n]);if(O)for(var n of O(t))me.call(t,n)&&be(e,n,t[n]);return e},pe=(e,t)=>Ue(e,ze(t));var he=(e,t)=>{var n={};for(var r in e)V.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(e!=null&&O)for(var r of O(e))t.indexOf(r)<0&&me.call(e,r)&&(n[r]=e[r]);return n};var We=(e,t)=>{for(var n in t)D(e,n,{get:t[n],enumerable:!0})},je=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Ke(t))!V.call(e,o)&&o!==n&&D(e,o,{get:()=>t[o],enumerable:!(r=Ve(t,o))||r.enumerable});return e};var Xe=e=>je(D({},"__esModule",{value:!0}),e);var bt={};We(bt,{Fragment:()=>ot,clearNodes:()=>Ae,conditional:()=>lt,createEffect:()=>x,createMemo:()=>et,createSignal:()=>He,disposeAll:()=>Ge,disposeNode:()=>E,forwardRef:()=>tt,h:()=>_,hydrate:()=>st,initRouter:()=>Ce,insert:()=>rt,jsx:()=>xe,jsxs:()=>at,mount:()=>it,onCleanup:()=>$e,onMount:()=>Qe,reconcile:()=>ut,reconcileTrees:()=>Y,reinitRouter:()=>gt});var y=[],Te=[],z=!1,w=new Set,K=[],I=!1;function ve(e){return{fn:e,cleanups:[],disposed:!1,sources:new Set,running:!1,rerun:!1,queued:!1}}function W(e){if(!e.disposed){e.disposed=!0;for(let t of e.sources)t.delete(e);e.sources.clear();for(let t of e.cleanups)t();e.cleanups.length=0}}function Ge(){for(let e of w)W(e);w.clear(),K.length=0,I=!1}function He(e){let t=e,n=new Set;return[()=>{let l=y[y.length-1];return l&&!l.disposed&&(n.add(l),l.sources.add(n)),t},l=>{let c=typeof l=="function"?l(t):l;if(c!==t){t=c;let h=Array.from(n);for(let m of h){if(m.disposed){n.delete(m);continue}if(m.running){m.rerun=!0;continue}m.queued||(m.queued=!0,K.push(m))}Je()}}]}function Je(){I||(I=!0,queueMicrotask(Ye))}function Ye(){I=!1;let e=K.splice(0);for(let t of e)t.disposed||(t.queued=!1,t.rerun=!1,B(t),t.rerun&&!t.disposed&&(t.rerun=!1,B(t)))}function B(e){e.running=!0;for(let t of e.cleanups)t();e.cleanups.length=0,y.push(e);try{let t=e.fn();typeof t=="function"&&e.cleanups.push(t)}finally{y.pop(),e.running=!1}}function x(e){let t=ve(e);return w.add(t),B(t),()=>{W(t),w.delete(t)}}function $e(e){let t=y[y.length-1];t&&t.cleanups.push(e)}function Qe(e){Te.push(e),z||(z=!0,queueMicrotask(Ze))}function Ze(){z=!1;let e=Te.splice(0);for(let t of e)t()}function et(e){let[t,n]=He(void 0),r=ve(()=>{let l=e();n(l)});w.add(r),B(r);let o=y[y.length-1];return o&&o.cleanups.push(()=>{r.disposed||(W(r),w.delete(r))}),t}function tt(e){return t=>e(t,t.ref||(()=>{}))}var j=new WeakMap;function Me(e,t){let n=j.get(e);n||(n=[],j.set(e,n)),n.push(t)}function E(e){let t=j.get(e);if(t){for(let n of t)n();t.length=0}for(let n=0;n<e.childNodes.length;n++)E(e.childNodes[n])}function nt(e){let t=e.nextSibling;for(;t;){if(t.nodeType===8&&t.nodeValue==="k")return t;t=t.nextSibling}return null}function Ae(e,t){let n=e.parentNode;if(!n)return;let r=e.nextSibling;for(;r&&r!==t;){let o=r.nextSibling;E(r),n.removeChild(r),r=o}}function rt(e,t,n){let r=nt(n);r?(Ae(n,r),X(e,t,r)):X(e,t,null)}function X(e,t,n){if(!(t==null||t===!1||t===!0)){if(typeof t=="string"||typeof t=="number")e.insertBefore(document.createTextNode(String(t)),n);else if(t instanceof Node)e.insertBefore(t,n);else if(Array.isArray(t))for(let r of t)X(e,r,n)}}function Ee(e,t,n){if(t==="class"||t==="className")n===!1||n===null||n===void 0?e.className="":e.className=String(n);else if(t==="style")typeof n=="string"?e.setAttribute("style",n):n&&typeof n=="object"&&Object.assign(e.style,n);else if(t.startsWith("on")&&typeof n=="function"){let r=t.slice(2).toLowerCase(),o=`__krate_${r}`,l=e[o];l&&e.removeEventListener(r,l),e.addEventListener(r,n),e[o]=n}else t==="ref"?typeof n=="function"&&n(e):t==="key"||n!=null&&n!==!1&&e.setAttribute(t,String(n))}function Le(e,t){if(!(t==null||t===!1||t===!0)){if(Array.isArray(t)){for(let n of t)Le(e,n);return}if(typeof t=="function"){let n=document.createTextNode("");e.appendChild(n),Me(n,x(()=>{n.nodeValue=String(t())}));return}e.appendChild(t instanceof Node?t:document.createTextNode(String(t)))}}function _(e,t,...n){if(typeof e=="function")return e(pe(fe({},t||{}),{children:n}));let r=document.createElement(e);if(t)for(let[o,l]of Object.entries(t))typeof l=="function"&&!o.startsWith("on")?Me(r,x(()=>Ee(r,o,l()))):Ee(r,o,l);for(let o of n)Le(r,o);return r}function ye(e){if(typeof e=="string"){let t=document.querySelector(e);if(!t)throw new Error(`Container "${e}" not found`);return t}return e}function it(e,t){let n=ye(t),r=!1;x(()=>{r||(r=!0,n.appendChild(e()))})}function st(e,t){let n=ye(t);if(n.childNodes.length===0){n.appendChild(e());return}let r=e();r&&E(r)}function ot(e){let t=document.createDocumentFragment(),n=e.children;if(n){let r=Array.isArray(n)?n:[n];for(let o of r)o instanceof Node?t.appendChild(o):t.appendChild(document.createTextNode(String(o)))}return t}function xe(e,t,n){let r=e;if(!t)return _(r,null);let h=t,{children:o}=h,l=he(h,["children"]),c=o!==void 0?Array.isArray(o)?o:[o]:[];return _(r,Object.keys(l).length>0?l:null,...c)}function at(e,t,n){return xe(e,t,n)}function ut(e,t,n,r,o,l){let c=[],h=t.nextSibling;for(;h&&h!==n;)c.push(h),h=h.nextSibling;if(l){let m=new Map;for(let g of c){let d=g.__k;d!=null&&m.set(d,g)}let f=[],A=new Set;for(let g=0;g<r.length;g++){let d=r[g],p=l(d);A.add(p);let H=o(d,g);H.__k=p,f.push(H)}for(let[g,d]of m)!A.has(g)&&d.parentNode===e&&(E(d),e.removeChild(d));for(let g=0;g<f.length;g++){let d=f[g],p=g===0?t:f[g-1];d!==p.nextSibling&&e.insertBefore(d,p.nextSibling)}}else{let m=Math.max(c.length,r.length);for(let f=0;f<m;f++)if(f<r.length){let A=o(r[f],f);f<c.length?c[f]!==A&&(E(c[f]),e.replaceChild(A,c[f])):e.insertBefore(A,n)}else f<c.length&&c[f].parentNode===e&&(E(c[f]),e.removeChild(c[f]))}}function lt(e,t,n,r,o,l){let c=null;x(()=>{let m=r()?o():l?l():null;c?m?(E(c),e.replaceChild(m,c)):(E(c),e.removeChild(c)):m&&e.insertBefore(m,n),c=m})}function N(e){if(e.nodeType===Node.COMMENT_NODE){let t=e.nodeValue||"";return t.indexOf("k:")===0?t:null}if(e.nodeType===Node.ELEMENT_NODE){let t=e,n=t.getAttribute("data-k");if(n)return n;let r=t.getAttribute("id");if(r)return"#"+r}return null}function Se(e,t){let n=new Set;for(let r=0;r<t.attributes.length;r++){let o=t.attributes[r].name,l=t.attributes[r].value;n.add(o),e.getAttribute(o)!==l&&e.setAttribute(o,l)}for(let r=0;r<e.attributes.length;r++){let o=e.attributes[r].name;n.has(o)||e.removeAttribute(o)}}function G(e,t){let n=document.importNode(t,!0);return e.parentNode&&(E(e),e.parentNode.replaceChild(n,e)),n}function ke(e,t){return e.nodeType!==t.nodeType?!1:e.nodeType===Node.ELEMENT_NODE?e.tagName===t.tagName:!0}function J(e,t){if(!ke(e,t))return G(e,t);if(e.nodeType===Node.TEXT_NODE)return e.nodeValue!==t.nodeValue&&(e.nodeValue=t.nodeValue),e;if(e.nodeType===Node.COMMENT_NODE)return e.nodeValue!==t.nodeValue?G(e,t):e;if(e.nodeType!==Node.ELEMENT_NODE)return e;let n=e,r=t;return N(n)!==N(r)?G(n,r):(Se(n,r),we(n,r),n)}function we(e,t){let n=Array.from(e.childNodes),r=Array.from(t.childNodes);if(n.length===r.length){let g=!0;for(let d=0;d<r.length;d++)if(N(n[d])!==N(r[d])){g=!1;break}if(g){for(let d=0;d<r.length;d++)J(n[d],r[d]);return}}let o=new Map,l=[];n.forEach((g,d)=>{let p=N(g);p!=null?o.has(p)||o.set(p,d):l.push(d)});let c=new Set,h=0,m=null,f=0,A=()=>{for(;f<l.length&&c.has(l[f]);)f++;return f<l.length?l[f]:-1};for(let g of r){let d=N(g),p;if(d!=null&&o.has(d)){let H=o.get(d);o.delete(d),c.add(H);let v=n[H];H<h?e.insertBefore(v,m):h=H,p=J(v,g)}else if(d==null){let H=A();if(H>=0&&ke(n[H],g)){c.add(H);let v=n[H];v.parentNode===e&&v.nextSibling!==m&&e.insertBefore(v,m),p=J(v,g)}else p=document.importNode(g,!0),e.insertBefore(p,m)}else p=document.importNode(g,!0),e.insertBefore(p,m);m=p.nextSibling}n.forEach((g,d)=>{!c.has(d)&&g.parentNode===e&&(E(g),e.removeChild(g))})}function Y(e,t){if(e.nodeType!==Node.ELEMENT_NODE||t.nodeType!==Node.ELEMENT_NODE)return;let n=e,r=t;if(n.tagName!==r.tagName){let o=document.importNode(t,!0);n.parentNode&&n.parentNode.replaceChild(o,n);return}Se(n,r),we(n,r)}var $=!1,C,R=null,S=new Map,ct=30,P=new Map,dt=50,Ne=/\/chunks\/runtime\.[^/]+\.js$/;function Ce(){if(typeof document=="undefined"||$)return;$=!0;let e=".docs-content, main, #root",t=150,n=200,r=null,o=null,l=new Map,c=null;function h(i){for(let s of e.split(", ")){let a=i.querySelector(s.trim());if(a)return a}return i.body}function m(i){if(i.hasAttribute("data-krate-external"))return!1;let s=i.getAttribute("href");return!(!s||s.startsWith("#")||s.startsWith("javascript:")||i.origin!==location.origin||i.target==="_blank")}function f(i){S.has(i)||fetch(i,{headers:{Accept:"text/html"}}).then(s=>s.ok?s.text():null).then(s=>{if(s){if(S.size>=ct){let a=S.keys().next().value;a!==void 0&&S.delete(a)}S.set(i,s)}}).catch(()=>{})}function A(){document.addEventListener("mouseover",p),document.addEventListener("focusin",p),typeof IntersectionObserver!="undefined"&&(c=new IntersectionObserver(i=>{for(let s of i)if(s.isIntersecting){let a=s.target,u=a.getAttribute("href");if(u&&m(a)){let b=new URL(u,location.origin).href;f(b)}c.unobserve(s.target)}},{rootMargin:"200px"}),g())}function g(){c&&document.querySelectorAll("a[data-prefetch]").forEach(i=>{c.observe(i)})}function d(){document.removeEventListener("mouseover",p),document.removeEventListener("focusin",p);for(let i of l.values())clearTimeout(i);l.clear(),c&&(c.disconnect(),c=null)}function p(i){let s=i.target.closest("a[data-prefetch]");if(!s||!m(s))return;let a=s.getAttribute("href");if(!a)return;let u=new URL(a,location.origin).href;if(S.has(u))return;let b=l.get(s);b&&clearTimeout(b);let T=setTimeout(()=>{l.delete(s),f(u)},n);l.set(s,T)}function H(i){let s=document.createTreeWalker(i,NodeFilter.SHOW_ELEMENT),a=s.nextNode();for(;a;){let u=a;for(let b of Object.keys(u))b.indexOf("__krate_")===0&&delete u[b];a=s.nextNode()}}let v=null;function Pe(){let i=h(document),s=document.querySelector("template[data-krate-loading]");s&&s.content&&i&&(v=s.content.cloneNode(!0),document.body.appendChild(v)),i&&(i.style.transition=`opacity ${t}ms ease`,i.style.opacity="0")}function Q(){v&&v.parentNode&&v.parentNode.removeChild(v),v=null;let i=h(document);i&&(i.style.opacity="1")}function Z(i,s,a,u={}){let{replace:b=!1,scroll:T=!0,restoreScroll:k=!1}=u,q=new DOMParser().parseFromString(i,"text/html"),ue=h(q),F=h(document);if(!ue||!F){location.href=s;return}let le=q.querySelector("title");le&&(document.title=le.textContent||""),typeof globalThis.disposeAll=="function"&&globalThis.disposeAll(),H(F),Y(F,ue);let ce=q.querySelectorAll('link[rel="stylesheet"]'),de=new Set;ce.forEach(M=>{let L=M.getAttribute("href");L&&de.add(L)}),document.querySelectorAll('link[rel="stylesheet"]').forEach(M=>{let L=M.getAttribute("href");L&&!de.has(L)&&M.remove()});let ge=new Set;document.querySelectorAll('link[rel="stylesheet"]').forEach(M=>{ge.add(M.href)}),ce.forEach(M=>{let L=M.getAttribute("href");if(L&&!ge.has(L)){let U=document.createElement("link");U.rel="stylesheet",U.href=L,document.head.appendChild(U)}}),De(a,s).then(()=>{Be(),g()}),Q();let Fe=location.pathname+location.search;if(b)history.replaceState({},"",s);else{if(!k){if(P.size>=dt){let M=P.keys().next().value;M!==void 0&&P.delete(M)}P.set(Fe,window.scrollY)}history.pushState({},"",s)}ee(s,{restoreScroll:k,scroll:T}),te(),window.dispatchEvent(new CustomEvent("krate:navigate",{detail:{url:s}}))}function Oe(i){let s=i.indexOf("#");if(s<0||s===i.length-1)return!1;let a=i.slice(s);try{let u=document.querySelector(a);if(u)return u.scrollIntoView({behavior:"smooth",block:"start"}),!0}catch(u){return!1}return!1}function ee(i,s){if(s.restoreScroll){let a=P.get(i);if(a!==void 0){window.scrollTo(0,a);return}}Oe(i)||s.scroll&&window.scrollTo(0,0)}function te(){let i=location.pathname;document.querySelectorAll("a[data-krate-link]").forEach(s=>{let a=s.getAttribute("href");if(!a){s.removeAttribute("aria-current");return}let u=a;try{u=new URL(a,location.origin).pathname}catch(T){return}u===i||i==="/"&&(u===""||u==="/")||u!=="/"&&u.length>1&&i.startsWith(u)?s.setAttribute("aria-current","page"):s.removeAttribute("aria-current")})}function De(i,s){return i.length===0?Promise.resolve():Promise.all(i.map(a=>{let u=a.getAttribute("src");return u?Ie(u,s):Promise.resolve()})).then(()=>{})}function Ie(i,s){return new Promise(a=>{let u=document.createElement("script"),b=ne(i,s);u.src=b,Ne.test(b)||u.setAttribute("data-krate-spa",""),u.onload=()=>a(),u.onerror=()=>a(),document.body.appendChild(u)})}function ne(i,s){if(/^(https?:)?\/\//.test(i)||i.startsWith("/"))return new URL(i,s).href;let a=s;if(!a.endsWith("/")){let u=a.lastIndexOf("/");a=u>0?a.slice(0,u+1):"/"}return new URL(i,a).href}function Be(){typeof globalThis.__krate_hydrate=="function"&&globalThis.__krate_hydrate()}function _e(i,s){typeof globalThis.disposeAll=="function"&&globalThis.disposeAll(),history.pushState({},"",s),document.open(),document.write(i),document.close()}function Re(){return r!==null?Promise.resolve(r):fetch("/404.html",{headers:{Accept:"text/html"}}).then(i=>i.ok?i.text():null).then(i=>(r=i,i)).catch(()=>null)}function qe(){return o!==null?Promise.resolve(o):fetch("/500.html",{headers:{Accept:"text/html"}}).then(i=>i.ok?i.text():null).then(i=>(o=i,i)).catch(()=>null)}function re(i,s){document.querySelectorAll("script[data-krate-spa]").forEach(b=>b.remove());let a=[],u=new Set;return document.querySelectorAll("script[src]").forEach(b=>{u.add(b.src)}),i.querySelectorAll("script[src]").forEach(b=>{let T=b.getAttribute("src");T&&!u.has(ne(T,s))&&a.push(b)}),a}function ie(i,s={}){let a=S.get(i);if(a){let T=new DOMParser().parseFromString(a,"text/html");Z(a,i,re(T,i),s);return}Pe(),C&&C.abort(),C=new AbortController;let u=C.signal;fetch(i,{headers:{Accept:"text/html"},signal:u}).then(b=>b.ok?b.text().then(T=>{let ae=new DOMParser().parseFromString(T,"text/html");Z(T,i,re(ae,i),s)}):(b.status>=500?qe():Re()).then(k=>{k?_e(k,i):location.href=i})).catch(b=>{(b==null?void 0:b.name)!=="AbortError"&&(Q(),location.href=i)}).finally(()=>{C&&!u.aborted&&(C=void 0)})}let se=i=>{if(i.defaultPrevented||i.button!==0||i.metaKey||i.ctrlKey||i.shiftKey||i.altKey)return;let s=i.target.closest("a[data-krate-link]");if(!s||!m(s))return;i.preventDefault();let a=s.getAttribute("href");if(!a)return;let u=new URL(a,location.origin).href,b=s.hasAttribute("data-krate-replace"),T=s.getAttribute("data-krate-scroll")!=="false";if(a===location.pathname+location.search+location.hash){b&&history.replaceState({},"",a),ee(u,{scroll:T});return}ie(u,{replace:b,scroll:T})},oe=()=>{ie(location.href,{restoreScroll:!0})};document.addEventListener("click",se),window.addEventListener("popstate",oe),A(),te(),document.querySelectorAll("script[src]").forEach(i=>{Ne.test(i.src)||i.setAttribute("data-krate-spa","")}),R=()=>{document.removeEventListener("click",se),window.removeEventListener("popstate",oe),d()}}function gt(){R&&(R(),R=null),$=!1,Ce()}return Xe(bt);})();
|
|
2
|
+
for (const k in __krate_exports) { window[k] = __krate_exports[k]; }
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DOM-level list reconciliation.
|
|
3
|
+
* Diffs old children against new items and mutates the DOM directly:
|
|
4
|
+
* reuses keyed nodes, creates new ones, removes stale ones.
|
|
5
|
+
*
|
|
6
|
+
* @param parent - the parent DOM node
|
|
7
|
+
* @param startMarker - comment boundary marking start of list region
|
|
8
|
+
* @param endMarker - comment boundary marking end of list region
|
|
9
|
+
* @param items - new array of items
|
|
10
|
+
* @param mapFn - creates a DOM node for each item
|
|
11
|
+
* @param keyFn - optional key extractor for keyed reconciliation
|
|
12
|
+
*/
|
|
13
|
+
export declare function reconcile<T>(parent: Node, startMarker: Node, endMarker: Node, items: T[], mapFn: (item: T, index: number) => Node, keyFn?: (item: T) => string | number): void;
|
|
14
|
+
/**
|
|
15
|
+
* Conditional branch swapping between two comment markers.
|
|
16
|
+
* When the signal changes, tears down the current branch and inserts the new one.
|
|
17
|
+
*
|
|
18
|
+
* @param parent - the parent DOM node
|
|
19
|
+
* @param startMarker - comment boundary marking start of conditional region
|
|
20
|
+
* @param endMarker - comment boundary marking end of conditional region
|
|
21
|
+
* @param test - reactive function returning boolean
|
|
22
|
+
* @param trueContent - function returning DOM node for true branch
|
|
23
|
+
* @param falseContent - function returning DOM node for false branch, or null for empty
|
|
24
|
+
*/
|
|
25
|
+
export declare function conditional(parent: Node, startMarker: Node, endMarker: Node, test: () => boolean, trueContent: () => Node, falseContent: (() => Node) | null): void;
|
|
26
|
+
/**
|
|
27
|
+
* Reconciles a live DOM subtree against freshly-parsed markup.
|
|
28
|
+
* Keeps unchanged nodes (and their state), patches attributes/text, and only
|
|
29
|
+
* inserts/replaces/removes nodes that actually differ. Used by the SPA router
|
|
30
|
+
* on navigation so persistent components don't get destroyed and re-created.
|
|
31
|
+
*
|
|
32
|
+
* @param oldRoot - the live element currently in the document
|
|
33
|
+
* @param newRoot - the parsed element from the navigated-to page
|
|
34
|
+
*/
|
|
35
|
+
export declare function reconcileTrees(oldRoot: Node, newRoot: Node): void;
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { createEffect } from './signal.js';
|
|
2
|
+
import { disposeNode } from './dom.js';
|
|
3
|
+
/**
|
|
4
|
+
* DOM-level list reconciliation.
|
|
5
|
+
* Diffs old children against new items and mutates the DOM directly:
|
|
6
|
+
* reuses keyed nodes, creates new ones, removes stale ones.
|
|
7
|
+
*
|
|
8
|
+
* @param parent - the parent DOM node
|
|
9
|
+
* @param startMarker - comment boundary marking start of list region
|
|
10
|
+
* @param endMarker - comment boundary marking end of list region
|
|
11
|
+
* @param items - new array of items
|
|
12
|
+
* @param mapFn - creates a DOM node for each item
|
|
13
|
+
* @param keyFn - optional key extractor for keyed reconciliation
|
|
14
|
+
*/
|
|
15
|
+
export function reconcile(parent, startMarker, endMarker, items, mapFn, keyFn) {
|
|
16
|
+
// Collect existing child nodes between startMarker and endMarker
|
|
17
|
+
const existing = [];
|
|
18
|
+
let node = startMarker.nextSibling;
|
|
19
|
+
while (node && node !== endMarker) {
|
|
20
|
+
existing.push(node);
|
|
21
|
+
node = node.nextSibling;
|
|
22
|
+
}
|
|
23
|
+
if (keyFn) {
|
|
24
|
+
// Keyed reconciliation
|
|
25
|
+
const keyMap = new Map();
|
|
26
|
+
for (const child of existing) {
|
|
27
|
+
const key = child.__k;
|
|
28
|
+
if (key != null)
|
|
29
|
+
keyMap.set(key, child);
|
|
30
|
+
}
|
|
31
|
+
const newNodes = [];
|
|
32
|
+
const usedKeys = new Set();
|
|
33
|
+
for (let i = 0; i < items.length; i++) {
|
|
34
|
+
const item = items[i];
|
|
35
|
+
const key = keyFn(item);
|
|
36
|
+
usedKeys.add(key);
|
|
37
|
+
const newNode = mapFn(item, i);
|
|
38
|
+
newNode.__k = key;
|
|
39
|
+
newNodes.push(newNode);
|
|
40
|
+
}
|
|
41
|
+
// Remove nodes whose keys are no longer present
|
|
42
|
+
for (const [key, child] of keyMap) {
|
|
43
|
+
if (!usedKeys.has(key) && child.parentNode === parent) {
|
|
44
|
+
disposeNode(child);
|
|
45
|
+
parent.removeChild(child);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// Reorder: insert nodes in the correct order before endMarker
|
|
49
|
+
for (let i = 0; i < newNodes.length; i++) {
|
|
50
|
+
const newNode = newNodes[i];
|
|
51
|
+
// Find the node that should be before this one
|
|
52
|
+
const prevNode = i === 0 ? startMarker : newNodes[i - 1];
|
|
53
|
+
if (newNode !== prevNode.nextSibling) {
|
|
54
|
+
parent.insertBefore(newNode, prevNode.nextSibling);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// Unkeyed: simple index-based reconciliation
|
|
60
|
+
const maxLen = Math.max(existing.length, items.length);
|
|
61
|
+
for (let i = 0; i < maxLen; i++) {
|
|
62
|
+
if (i < items.length) {
|
|
63
|
+
const newNode = mapFn(items[i], i);
|
|
64
|
+
if (i < existing.length) {
|
|
65
|
+
// Replace existing node
|
|
66
|
+
if (existing[i] !== newNode) {
|
|
67
|
+
disposeNode(existing[i]);
|
|
68
|
+
parent.replaceChild(newNode, existing[i]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
// Append new node before endMarker
|
|
73
|
+
parent.insertBefore(newNode, endMarker);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (i < existing.length) {
|
|
77
|
+
// Remove excess existing nodes
|
|
78
|
+
if (existing[i].parentNode === parent) {
|
|
79
|
+
disposeNode(existing[i]);
|
|
80
|
+
parent.removeChild(existing[i]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Conditional branch swapping between two comment markers.
|
|
88
|
+
* When the signal changes, tears down the current branch and inserts the new one.
|
|
89
|
+
*
|
|
90
|
+
* @param parent - the parent DOM node
|
|
91
|
+
* @param startMarker - comment boundary marking start of conditional region
|
|
92
|
+
* @param endMarker - comment boundary marking end of conditional region
|
|
93
|
+
* @param test - reactive function returning boolean
|
|
94
|
+
* @param trueContent - function returning DOM node for true branch
|
|
95
|
+
* @param falseContent - function returning DOM node for false branch, or null for empty
|
|
96
|
+
*/
|
|
97
|
+
export function conditional(parent, startMarker, endMarker, test, trueContent, falseContent) {
|
|
98
|
+
let current = null;
|
|
99
|
+
createEffect(() => {
|
|
100
|
+
const value = test();
|
|
101
|
+
const newNode = value ? trueContent() : (falseContent ? falseContent() : null);
|
|
102
|
+
if (current) {
|
|
103
|
+
if (newNode) {
|
|
104
|
+
disposeNode(current);
|
|
105
|
+
parent.replaceChild(newNode, current);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
disposeNode(current);
|
|
109
|
+
parent.removeChild(current);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (newNode) {
|
|
113
|
+
parent.insertBefore(newNode, endMarker);
|
|
114
|
+
}
|
|
115
|
+
current = newNode;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
// ─── Tree reconciliation (SPA navigation) ───────────────────────────────────
|
|
119
|
+
// Diffs a live DOM subtree against a freshly-parsed one, patching only what
|
|
120
|
+
// changed. Unchanged nodes are kept in place so their state survives navigation:
|
|
121
|
+
// element listeners attached at hydration (`__krate_<event>` props), media
|
|
122
|
+
// playback, focus, scroll, CSS animations. Slot markers (`<!--k:id-->`) and
|
|
123
|
+
// `data-k` elements act as stable identity anchors between pages.
|
|
124
|
+
function nodeKey(n) {
|
|
125
|
+
if (n.nodeType === Node.COMMENT_NODE) {
|
|
126
|
+
const v = n.nodeValue || '';
|
|
127
|
+
if (v.indexOf('k:') === 0)
|
|
128
|
+
return v;
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
if (n.nodeType === Node.ELEMENT_NODE) {
|
|
132
|
+
const el = n;
|
|
133
|
+
const dk = el.getAttribute('data-k');
|
|
134
|
+
if (dk)
|
|
135
|
+
return dk;
|
|
136
|
+
const id = el.getAttribute('id');
|
|
137
|
+
if (id)
|
|
138
|
+
return '#' + id;
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
function syncAttributes(oldEl, newEl) {
|
|
143
|
+
// Pass 1: apply (or update) attributes present on the new element.
|
|
144
|
+
const newNames = new Set();
|
|
145
|
+
for (let i = 0; i < newEl.attributes.length; i++) {
|
|
146
|
+
const name = newEl.attributes[i].name;
|
|
147
|
+
const value = newEl.attributes[i].value;
|
|
148
|
+
newNames.add(name);
|
|
149
|
+
if (oldEl.getAttribute(name) !== value) {
|
|
150
|
+
oldEl.setAttribute(name, value);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// Pass 2: remove attributes no longer present.
|
|
154
|
+
for (let i = 0; i < oldEl.attributes.length; i++) {
|
|
155
|
+
const name = oldEl.attributes[i].name;
|
|
156
|
+
if (!newNames.has(name)) {
|
|
157
|
+
oldEl.removeAttribute(name);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function replaceNode(oldNode, newNode) {
|
|
162
|
+
const imported = document.importNode(newNode, true);
|
|
163
|
+
if (oldNode.parentNode) {
|
|
164
|
+
disposeNode(oldNode);
|
|
165
|
+
oldNode.parentNode.replaceChild(imported, oldNode);
|
|
166
|
+
return imported;
|
|
167
|
+
}
|
|
168
|
+
return imported;
|
|
169
|
+
}
|
|
170
|
+
function compatible(oldNode, newNode) {
|
|
171
|
+
if (oldNode.nodeType !== newNode.nodeType)
|
|
172
|
+
return false;
|
|
173
|
+
if (oldNode.nodeType === Node.ELEMENT_NODE) {
|
|
174
|
+
return oldNode.tagName === newNode.tagName;
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
function reconcileNode(oldNode, newNode) {
|
|
179
|
+
if (!compatible(oldNode, newNode))
|
|
180
|
+
return replaceNode(oldNode, newNode);
|
|
181
|
+
if (oldNode.nodeType === Node.TEXT_NODE) {
|
|
182
|
+
if (oldNode.nodeValue !== newNode.nodeValue)
|
|
183
|
+
oldNode.nodeValue = newNode.nodeValue;
|
|
184
|
+
return oldNode;
|
|
185
|
+
}
|
|
186
|
+
if (oldNode.nodeType === Node.COMMENT_NODE) {
|
|
187
|
+
if (oldNode.nodeValue !== newNode.nodeValue)
|
|
188
|
+
return replaceNode(oldNode, newNode);
|
|
189
|
+
return oldNode;
|
|
190
|
+
}
|
|
191
|
+
if (oldNode.nodeType !== Node.ELEMENT_NODE)
|
|
192
|
+
return oldNode;
|
|
193
|
+
const oldEl = oldNode;
|
|
194
|
+
const newEl = newNode;
|
|
195
|
+
if (nodeKey(oldEl) !== nodeKey(newEl))
|
|
196
|
+
return replaceNode(oldEl, newEl);
|
|
197
|
+
syncAttributes(oldEl, newEl);
|
|
198
|
+
reconcileChildren(oldEl, newEl);
|
|
199
|
+
return oldEl;
|
|
200
|
+
}
|
|
201
|
+
function reconcileChildren(parent, newParent) {
|
|
202
|
+
const oldKids = Array.from(parent.childNodes);
|
|
203
|
+
const newKids = Array.from(newParent.childNodes);
|
|
204
|
+
// Fast path: identical child counts and identical node keys in order. This
|
|
205
|
+
// is the common case for static navigation, and it avoids building the
|
|
206
|
+
// key maps and per-node bookkeeping entirely.
|
|
207
|
+
if (oldKids.length === newKids.length) {
|
|
208
|
+
let same = true;
|
|
209
|
+
for (let i = 0; i < newKids.length; i++) {
|
|
210
|
+
if (nodeKey(oldKids[i]) !== nodeKey(newKids[i])) {
|
|
211
|
+
same = false;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (same) {
|
|
216
|
+
for (let i = 0; i < newKids.length; i++) {
|
|
217
|
+
reconcileNode(oldKids[i], newKids[i]);
|
|
218
|
+
}
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const keyToIndex = new Map();
|
|
223
|
+
const unkeyedOld = [];
|
|
224
|
+
oldKids.forEach((c, i) => {
|
|
225
|
+
const k = nodeKey(c);
|
|
226
|
+
if (k != null) {
|
|
227
|
+
if (!keyToIndex.has(k))
|
|
228
|
+
keyToIndex.set(k, i);
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
unkeyedOld.push(i);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
const consumed = new Set();
|
|
235
|
+
let lastPlacedIndex = 0;
|
|
236
|
+
let anchor = null;
|
|
237
|
+
let unkeyedPtr = 0;
|
|
238
|
+
const nextUnkeyed = () => {
|
|
239
|
+
while (unkeyedPtr < unkeyedOld.length && consumed.has(unkeyedOld[unkeyedPtr]))
|
|
240
|
+
unkeyedPtr++;
|
|
241
|
+
return unkeyedPtr < unkeyedOld.length ? unkeyedOld[unkeyedPtr] : -1;
|
|
242
|
+
};
|
|
243
|
+
for (const newChild of newKids) {
|
|
244
|
+
const k = nodeKey(newChild);
|
|
245
|
+
let placedNode;
|
|
246
|
+
if (k != null && keyToIndex.has(k)) {
|
|
247
|
+
const idx = keyToIndex.get(k);
|
|
248
|
+
keyToIndex.delete(k);
|
|
249
|
+
consumed.add(idx);
|
|
250
|
+
const oldChild = oldKids[idx];
|
|
251
|
+
if (idx < lastPlacedIndex) {
|
|
252
|
+
parent.insertBefore(oldChild, anchor);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
lastPlacedIndex = idx;
|
|
256
|
+
}
|
|
257
|
+
placedNode = reconcileNode(oldChild, newChild);
|
|
258
|
+
}
|
|
259
|
+
else if (k == null) {
|
|
260
|
+
const candIdx = nextUnkeyed();
|
|
261
|
+
if (candIdx >= 0 && compatible(oldKids[candIdx], newChild)) {
|
|
262
|
+
consumed.add(candIdx);
|
|
263
|
+
const oldChild = oldKids[candIdx];
|
|
264
|
+
if (oldChild.parentNode === parent && oldChild.nextSibling !== anchor) {
|
|
265
|
+
parent.insertBefore(oldChild, anchor);
|
|
266
|
+
}
|
|
267
|
+
placedNode = reconcileNode(oldChild, newChild);
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
placedNode = document.importNode(newChild, true);
|
|
271
|
+
parent.insertBefore(placedNode, anchor);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
// Keyed but not present in old tree → insert new
|
|
276
|
+
placedNode = document.importNode(newChild, true);
|
|
277
|
+
parent.insertBefore(placedNode, anchor);
|
|
278
|
+
}
|
|
279
|
+
anchor = placedNode.nextSibling;
|
|
280
|
+
}
|
|
281
|
+
oldKids.forEach((c, i) => {
|
|
282
|
+
if (!consumed.has(i) && c.parentNode === parent) {
|
|
283
|
+
disposeNode(c);
|
|
284
|
+
parent.removeChild(c);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Reconciles a live DOM subtree against freshly-parsed markup.
|
|
290
|
+
* Keeps unchanged nodes (and their state), patches attributes/text, and only
|
|
291
|
+
* inserts/replaces/removes nodes that actually differ. Used by the SPA router
|
|
292
|
+
* on navigation so persistent components don't get destroyed and re-created.
|
|
293
|
+
*
|
|
294
|
+
* @param oldRoot - the live element currently in the document
|
|
295
|
+
* @param newRoot - the parsed element from the navigated-to page
|
|
296
|
+
*/
|
|
297
|
+
export function reconcileTrees(oldRoot, newRoot) {
|
|
298
|
+
if (oldRoot.nodeType !== Node.ELEMENT_NODE || newRoot.nodeType !== Node.ELEMENT_NODE) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const oldEl = oldRoot;
|
|
302
|
+
const newEl = newRoot;
|
|
303
|
+
if (oldEl.tagName !== newEl.tagName) {
|
|
304
|
+
const imported = document.importNode(newRoot, true);
|
|
305
|
+
if (oldEl.parentNode) {
|
|
306
|
+
oldEl.parentNode.replaceChild(imported, oldEl);
|
|
307
|
+
}
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
syncAttributes(oldEl, newEl);
|
|
311
|
+
reconcileChildren(oldEl, newEl);
|
|
312
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface ResourceReturn<T> {
|
|
2
|
+
(): T | undefined;
|
|
3
|
+
loading: boolean;
|
|
4
|
+
error: unknown;
|
|
5
|
+
state: 'unresolved' | 'loading' | 'ready' | 'error' | 'refreshing';
|
|
6
|
+
}
|
|
7
|
+
export interface ResourceActions<T> {
|
|
8
|
+
mutate: (fn: (prev: T | undefined) => T) => void;
|
|
9
|
+
refetch: () => void;
|
|
10
|
+
}
|
|
11
|
+
export declare function createResource<T, S = void>(source: (() => S) | S, fetcher: (source: S, opts?: {
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
}) => Promise<T>): [ResourceReturn<T>, ResourceActions<T>];
|
package/dist/resource.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { createSignal, createEffect, onCleanup } from './signal.js';
|
|
2
|
+
export function createResource(source, fetcher) {
|
|
3
|
+
const [data, setData] = createSignal(undefined);
|
|
4
|
+
const [loading, setLoading] = createSignal(true);
|
|
5
|
+
const [error, setError] = createSignal(undefined);
|
|
6
|
+
const [state, setState] = createSignal('unresolved');
|
|
7
|
+
const [version, setVersion] = createSignal(0);
|
|
8
|
+
let abortController;
|
|
9
|
+
const getSourceValue = () => {
|
|
10
|
+
return typeof source === 'function' ? source() : source;
|
|
11
|
+
};
|
|
12
|
+
const fetch = async () => {
|
|
13
|
+
if (abortController) {
|
|
14
|
+
abortController.abort();
|
|
15
|
+
}
|
|
16
|
+
abortController = new AbortController();
|
|
17
|
+
// Fix 2.3: Capture local reference so concurrent fetches don't interfere
|
|
18
|
+
// with the abort check after await
|
|
19
|
+
const controller = abortController;
|
|
20
|
+
const src = getSourceValue();
|
|
21
|
+
setState(version() > 0 ? 'refreshing' : 'loading');
|
|
22
|
+
setLoading(true);
|
|
23
|
+
setError(undefined);
|
|
24
|
+
try {
|
|
25
|
+
const result = await fetcher(src, { signal: controller.signal });
|
|
26
|
+
if (!controller.signal.aborted) {
|
|
27
|
+
setData(() => result);
|
|
28
|
+
setState('ready');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
if (e?.name !== 'AbortError' && !controller.signal.aborted) {
|
|
33
|
+
setError(e);
|
|
34
|
+
setState('error');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
if (!controller.signal.aborted) {
|
|
39
|
+
setLoading(false);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
// Auto-fetch when source changes
|
|
44
|
+
createEffect(() => {
|
|
45
|
+
// Read source to track it for reactivity
|
|
46
|
+
if (typeof source === 'function' && source.length === 0) {
|
|
47
|
+
source();
|
|
48
|
+
}
|
|
49
|
+
// Read version to track refetches
|
|
50
|
+
version();
|
|
51
|
+
fetch();
|
|
52
|
+
onCleanup(() => {
|
|
53
|
+
if (abortController) {
|
|
54
|
+
abortController.abort();
|
|
55
|
+
abortController = undefined;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
const resource = Object.assign(data, {
|
|
60
|
+
get loading() { return loading(); },
|
|
61
|
+
get error() { return error(); },
|
|
62
|
+
get state() { return state(); },
|
|
63
|
+
});
|
|
64
|
+
const actions = {
|
|
65
|
+
mutate: (fn) => {
|
|
66
|
+
setData(fn);
|
|
67
|
+
},
|
|
68
|
+
refetch: () => {
|
|
69
|
+
setVersion(v => v + 1);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
return [resource, actions];
|
|
73
|
+
}
|
package/dist/router.d.ts
ADDED