rvx
Version:
A signal based rendering library
2 lines (1 loc) • 10.8 kB
JavaScript
const t={push(t){n?.(t)}},e=[t];let n;const r=[!0],i=[],o=[[]];function s(t,e,n){try{return t.push(e),n()}finally{t.pop()}}function c(t){if(void 0!==n)throw new Error("G4");n=t}const l=t=>({context:t,value:t.current});class u{constructor(t){this.default=t}#t=[];#e=0;default;get current(){if(this.#e===o.length){const t=this.#t;return t[t.length-1]??this.default}return this.default}inject(t,e,...n){const r=o[o.length-1],i=this.#t,s=this.#e;try{return this.#e=o.length,r.push(this),i.push(t),e(...n)}finally{i.pop(),r.pop(),this.#e=s}}with(t){return{context:this,value:t}}static window(t,e,...n){try{return o.push([]),u.inject(t,e,...n)}finally{o.pop()}}static inject(t,e,...n){const r=[],i=o.length,s=o[i-1];for(let e=0;e<t.length;e++){const{context:n,value:o}=t[e];r.push({c:n,p:n.#e}),n.#e=i,n.#t.push(o),s.push(n)}try{return e(...n)}finally{for(let t=r.length-1;t>=0;t--){const{c:e,p:n}=r[t];e.#e=n,e.#t.pop(),s.pop()}}}static capture(){return o[o.length-1].map(l)}static wrap(t){const e=u.capture();return(...n)=>u.window(e,t,...n)}}function h(t){return"context"in t?t.context.inject(t.value,t.children):u.inject(t.states,t.children)}const a="http://www.w3.org/1999/xhtml",f="http://www.w3.org/2000/svg",d="http://www.w3.org/1998/Math/MathML",p=new u(a),v=Symbol.for("rvx:node"),g=new u(globalThis),w=()=>{};function y(n,...o){try{return e.push(t),i.push(void 0),r.push(!0),n(...o)}finally{e.pop(),i.pop(),r.pop()}}function m(t){for(let e=t.length-1;e>=0;e--)t[e]()}function b(t){const n=[];try{s(e,n,t)}catch(t){throw y(m,n),t}return 0===n.length?w:()=>y(m,n)}function x(t){let e,n=!1,r=w;return r=b(()=>{e=t(()=>{n=!0,r()})}),n&&r(),e}function C(t){return s(e,void 0,t)}function A(t){let e;return S(b(()=>{e=t()})),e}function S(t){const n=e.length;n>0&&e[n-1]?.push(t)}let B;const j=t=>t(),k=t=>B.add(t);class E{#n;#r=new Set;#i;#o;constructor(t,e){this.#n=t,this.#i=e,this.#o=e?e.#o:this}get value(){return this.access(),this.#n}set value(t){Object.is(this.#n,t)||(this.#n=t,this.notify())}get source(){return this.#i}get root(){return this.#o}update(t){!1!==t(this.#n)&&this.notify()}get active(){return this.#r.size>0}access(){if(r[r.length-1]){const t=i.length;t>0&&i[t-1]?.(this.#r)}}notify(){const t=this.#r;if(0!==t.size)if(void 0===B){const e=Array.from(t);t.clear(),e.forEach(j)}else t.forEach(k)}pipe(t,...e){return t(this,...e)}}function N(t,e){return new E(t,e)}const O=t=>{const e=[];return{c:()=>{for(let n=0;n<e.length;n++)e[n].delete(t);e.length=0},a:n=>{e.push(n),n.add(t)}}},T=(t,e)=>{try{return i.push(t),r.push(!0),e()}finally{i.pop(),r.pop()}};function I(t,e){const n=t instanceof E;if(n||"function"==typeof t){let r,i=!1,o=w;const s=n?()=>t.value:t,c=(t=>{let e=0;return()=>{if(e<2&&e++,1===e)try{for(;e>0;)t(),e--}finally{e=0}}})(u.wrap(()=>{i||(l(),y(o),o=b(()=>{r=T(h,s),e&&T(void 0,()=>e(r))}))})),{c:l,a:h}=O(c);S(()=>{i=!0,l(),o()}),c()}else e(t)}function M(t,e){let n,r=!1;return I(t,t=>{r?e(t):(n=t,r=!0)}),n}function Q(t){if(void 0===B){const e=new Set;let n;try{for(B=e,n=t();e.size>0;)e.forEach(t=>{e.delete(t),t()})}finally{B=void 0}return n}return t()}function G(t){const e=N(void 0);return I(()=>e.value=D(t)),()=>e.value}function L(t){return s(r,!1,t)}function z(t){return s(r,!0,t)}function F(){return r[r.length-1]&&void 0!==i[i.length-1]}function _(t){const e=u.wrap(()=>{n(),y(t)}),{c:n,a:r}=O(e);return S(n),t=>{n();try{const e=i.length;if(e>0){const t=i[e-1];i.push(e=>{r(e),t?.(e)})}else i.push(r);return D(t)}finally{i.pop()}}}function D(t){return t instanceof E?t.value:"function"==typeof t?t():t}function P(t,e){return t instanceof E?()=>e(t.value):"function"==typeof t?()=>e(t()):e(t)}function R(t,e){const n=e.document.createTextNode("");return I(t,t=>n.textContent=t??""),n}function W(t){return t.document.createComment("g")}function X(t){return t.document.createDocumentFragment()}function q(t,e){const n=W(e);t(n,n)}function H(t,e,n){null===e.firstChild?q(t,n):t(e.firstChild,e.lastChild)}function J(t){return t instanceof U?t:new U(e=>{const n=g.current;if(Array.isArray(t)){const r=t.flat(1/0);if(r.length>1){const t=X(n);for(let i=0;i<r.length;i++){let o=r[i];null==o?t.appendChild(W(n)):"object"==typeof o?(v in o&&(o=o[v]),o instanceof n.Node?11===o.nodeType&&0===o.childNodes.length?t.appendChild(W(n)):t.appendChild(o):o instanceof U?(o.appendTo(t),0===i?o.setBoundaryOwner((t,n)=>e(t,void 0)):i===r.length-1&&o.setBoundaryOwner((t,n)=>e(void 0,n))):t.appendChild(R(o,n))):t.appendChild(R(o,n))}return void H(e,t,n)}t=r[0]}if(null==t)q(e,n);else if("object"==typeof t)if(v in t&&(t=t[v]),t instanceof n.Node)11===t.nodeType?H(e,t,n):e(t,t);else if(t instanceof U)e(t.first,t.last),t.setBoundaryOwner(e);else{const r=R(t,n);e(r,r)}else{const r=R(t,n);e(r,r)}})}function K(t,e){const n=J(e);return n.appendTo(t),S(()=>n.detach()),n}class U{#s;#c;#l;constructor(t){if(t((t,e)=>{t&&(this.#s=t),e&&(this.#c=e),this.#l?.(this.#s,this.#c)},this),!this.#s||!this.#c)throw new Error("G1")}get first(){return this.#s}get last(){return this.#c}get parent(){return this.#s?.parentNode??void 0}setBoundaryOwner(t){if(void 0!==this.#l)throw new Error("G2");this.#l=t,S(()=>this.#l=void 0)}appendTo(t){let e=this.#s;const n=this.#c;for(;;){const r=e.nextSibling;if(t.appendChild(e),e===n)break;e=r}}insertBefore(t,e){if(null===e)return this.appendTo(t);let n=this.#s;const r=this.#c;for(;;){const i=n.nextSibling;if(t.insertBefore(n,e),n===r)break;n=i}}detach(){const t=this.#s;if(t===this.#c)return t.parentNode?.removeChild(t),t;{const t=g.current.document.createDocumentFragment();return this.appendTo(t),t}}}function*V(t){let e=t.first;for(;yield e,e!==t.last;)e=e.nextSibling}const Y=t=>t?.();function Z(t,e=Y){return new U((n,r)=>{I(t,t=>{const i=r.last,o=i?.parentNode;let s;if(o){const n=i.nextSibling;r.detach(),s=J(e(t)),s.insertBefore(o,n)}else s=J(e(t));n(s.first,s.last),s.setBoundaryOwner(n)})})}function $(t){return Z(t.watch,t.children)}function tt(t,e,n){return Z(G(t),t=>t?e(t):n?.())}function et(t){return tt(t.when,t.children,t.else)}function nt(t,e){return new U((n,r)=>{function i(t){for(let e=0;e<t.length;e++)t[e].v.detach()}const o=g.current;let s=0;const c=[],l=new Map,u=W(o);n(u,u),S(()=>{for(let t=0;t<c.length;t++)c[t].d()}),I(()=>{let h=r.parent;h||(h=X(o),h.appendChild(u));let a=0,f=u;try{for(const r of D(t)){let t=c[a];if(t&&Object.is(t.u,r))t.c=s,t.i.value=a,f=t.v.last,a++;else if(t=l.get(r),void 0===t){const t={u:r,c:s,i:N(a),d:void 0,v:void 0};t.d=y(b,()=>{t.v=J(e(r,()=>t.i.value)),t.v.setBoundaryOwner((e,r)=>{c[c.length-1]===t&&t.c===s&&n(void 0,r)})}),t.v.insertBefore(h,f.nextSibling),c.splice(a,0,t),l.set(r,t),f=t.v.last,a++}else if(t.c!==s){t.i.value=a,t.c=s;const e=c.indexOf(t,a);e<0?(i(c.splice(a,c.length-a,t)),t.v.insertBefore(h,f.nextSibling)):i(c.splice(a,e-a)),f=t.v.last,a++}}}finally{c.length>a&&i(c.splice(a));for(const[t,e]of l)e.c!==s&&(l.delete(t),e.v.detach(),e.d());s=s+1|0,n(void 0,f)}})})}function rt(t){return nt(t.each,t.children)}function it(t,e){return new U((n,r)=>{const i=g.current,o=W(i);n(o,o);const s=[];S(()=>{for(let t=0;t<s.length;t++)s[t].d()}),I(()=>{let c=r.parent;c||(c=X(i),c.appendChild(o));let l=0,u=o;try{for(const r of D(t)){if(l<s.length){const t=s[l];if(Object.is(t.u,r)){u=t.v.last,l++;continue}t.v.detach(),t.d(),t.d=w}const t={u:r,d:void 0,v:void 0};t.d=y(b,()=>{t.v=J(e(r,l)),t.v.setBoundaryOwner((e,r)=>{s[s.length-1]===t&&n(void 0,r)})}),t.v.insertBefore(c,u.nextSibling),s[l]=t,u=t.v.last,l++}}finally{if(s.length>l){for(let t=l;t<s.length;t++){const e=s[t];e.v.detach(),e.d()}s.length=l}n(void 0,u)}})})}function ot(t){return it(t.each,t.children)}class st{#u;#h=N();constructor(t){this.#u=t}move=()=>{this.#h.value=void 0;return Z(this.#h=N(this.#u),t=>t)};detach(){this.#h.value=void 0}}function ct(t){return new st(J(t))}function lt(t,e){return Z(t,t=>t?e:void 0)}function ut(t){return lt(t.when,t.children)}function ht(t,e,n){if(null!=e)if(Array.isArray(e))for(let r=0;r<e.length;r++)ht(t,e[r],n);else e instanceof n.Node?t.appendChild(e):e instanceof U?e.appendTo(t):"object"==typeof e&&v in e?t.appendChild(e[v]):t.appendChild(R(e,n))}class at{#h;#a=[];#f=[];#d=[];constructor(t){this.#h=t}a(t){const e=this.#a;S(()=>{const n=this.#f;for(let r=0;r<e.length;r++){const i=e[r];if(i.t===t)return void(0===--i.c&&(e.splice(r,1),n.push(t)))}});for(let n=0;n<e.length;n++){const r=e[n];if(r.t===t)return void r.c++}e.push({t:t,c:1}),this.#d.push(t)}f(){const t=this.#h,e=this.#f,n=this.#d;e.length>0&&(t.classList.remove(...e),e.length=0),n.length>0&&(t.hasAttribute("class")?t.classList.add(...n):t.setAttribute("class",n.join(" ")),n.length=0)}}function ft(t,e,n){I(t,t=>{if("string"==typeof t)e.a(t);else if(t)if(Array.isArray(t))for(let n=0;n<t.length;n++)ft(t[n],e,!1);else for(const r in t)I(t[r],t=>{t&&e.a(r),n&&e.f()});n?e.f():n=!0})}function dt(t,e){ft(e,new at(t),!0)}function pt(t,e){I(t,t=>{if(Array.isArray(t)){const n=[];for(let r=t.length-1;r>=0;r--){const i=[];n[r]=i,pt(t[r],(t,o)=>{i.includes(t)||i.push(t);for(let e=r+1;e<n.length;e++)if(n[e].includes(t))return;e(t,o)})}}else if(t)for(const n in t)I(t[n],t=>e(n,t))})}function vt(t,e){const n=t.style;pt(e,(t,e)=>n.setProperty(t,e?String(e):null))}class gt{#p=g.current;elem;get[v](){return this.elem}constructor(t){this.elem=t}on(t,e,n){const r=u.wrap(e);return this.elem.addEventListener(t,t=>y(r,t),n),this}style(t){return vt(this.elem,t),this}class(t){return dt(this.elem,t),this}set(t,e){return function(t,e,n){I(n,n=>{null==n||!1===n?t.removeAttribute(e):t.setAttribute(e,!0===n?"":n)})}(this.elem,t,e),this}prop(t,e){return I(e,e=>this.elem[t]=e),this}append(...t){return ht(this.elem,t,this.#p),this}}function wt(t){return new gt(g.current.document.createElementNS(p.current,t))}class yt{#v=new Set;event=t=>{this.#v.add(t),S(()=>this.#v.delete(t))};emit=(...t)=>{this.#v.forEach(e=>e(...t))}}const mt={value:0};function bt(){const t=mt.value;return"number"==typeof t&&t>=Number.MAX_SAFE_INTEGER?mt.value=BigInt(mt.value)+1n:mt.value++,"rvx_"+String(t)}function xt(t){return t(bt())}function Ct(t){return t.children(bt())}const At=new WeakMap;function St(t){let e=At.get(t);return void 0===e&&(e=bt(),At.set(t,e)),e}export{N as $,ut as Attach,u as Context,g as ENV,gt as ElementBuilder,yt as Emitter,rt as For,a as HTML,ot as Index,h as Inject,d as MATHML,st as MovableView,v as NODE,$ as Nest,f as SVG,et as Show,E as Signal,Ct as UseUniqueId,U as View,p as XMLNS,lt as attachWhen,Q as batch,b as capture,x as captureSelf,wt as e,nt as forEach,D as get,it as indexEach,F as isTracking,y as isolate,P as map,G as memo,K as mount,ct as movable,Z as nest,c as onLeak,J as render,S as teardown,A as teardownOnError,z as track,_ as trigger,C as uncapture,bt as uniqueId,St as uniqueIdFor,L as untrack,xt as useUniqueId,V as viewNodes,I as watch,M as watchUpdates,tt as when};