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