rvx
Version:
A signal based rendering library
2 lines (1 loc) • 10.9 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 k=t=>t(),j=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(k)}else t.forEach(j)}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){for(;t.size>0;)try{t.forEach(e=>{t.delete(e),e()})}finally{Q(t)}}function G(t){if(void 0===B){const e=new Set;let n;try{B=e,n=t(),Q(e)}finally{B=void 0}return n}return t()}function L(t){const e=N(void 0);return I(()=>e.value=P(t)),()=>e.value}function z(t){return s(r,!1,t)}function F(t){return s(r,!0,t)}function _(){return r[r.length-1]&&void 0!==i[i.length-1]}function D(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 P(t)}finally{i.pop()}}}function P(t){return t instanceof E?t.value:"function"==typeof t?t():t}function R(t,e){return t instanceof E?()=>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=g.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?(v in o&&(o=o[v]),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(v in t&&(t=t[v]),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),S(()=>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,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*Y(t){let e=t.first;for(;;){const n=e.nextSibling;if(yield e,e===t.last)break;e=n}}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=g.current;let s=0;const c=[],l=new Map,u=X(o);n(u,u),S(()=>{for(let t=0;t<c.length;t++)c[t].d()}),I(()=>{let h=r.parent;h||(h=q(o),h.appendChild(u));let a=0,f=u;try{for(const r of 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:N(a),d:void 0,v:void 0};t.d=y(b,()=>{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(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 it(t){return rt(t.each,t.children)}function ot(t,e){return new V((n,r)=>{const i=g.current,o=X(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=q(i),c.appendChild(o));let l=0,u=o;try{for(const r of 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=w}const t={u:r,d:void 0,v:void 0};t.d=y(b,()=>{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=N();constructor(t){this.#u=t}move=()=>{this.#h.value=void 0;return $(this.#h=N(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&&v in e?t.appendChild(e[v]):t.appendChild(W(e,n))}class ft{#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 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=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 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(g.current.document.createElementNS(p.current,t))}class mt{#v=new Set;event=t=>{this.#v.add(t),S(()=>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{N as $,ht as Attach,u as Context,g as ENV,wt as ElementBuilder,mt as Emitter,it as For,a as HTML,st as Index,h as Inject,d as MATHML,ct as MovableView,v as NODE,tt as Nest,f as SVG,nt as Show,E as Signal,At as UseUniqueId,V as View,p as XMLNS,ut as attachWhen,G as batch,b as capture,x as captureSelf,yt as e,rt as forEach,P as get,ot as indexEach,_ as isTracking,y as isolate,R as map,L as memo,U as mount,lt as movable,$ as nest,c as onLeak,K as render,S as teardown,A as teardownOnError,F as track,D as trigger,C 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};