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