UNPKG

rvx

Version:

A signal based rendering library

2 lines (1 loc) 5.46 kB
import{Signal as t,isTracking as e,batch as r,$ as s,watchUpdates as i}from"./rvx.min.js";class a extends t{#t;constructor(t,e){super(e),this.#t=t}notify(){super.notify(),this.active||this.#t()}}class o{#e=new Map;#r;constructor(t){this.#r=t}access(t){if(e()){let e=this.#e.get(t);void 0===e&&(e=new a(()=>this.#e.delete(t),this.#r(t)),this.#e.set(t,e)),e.access()}}update(t,e){const r=this.#e.get(t);void 0!==r&&(r.value=e)}fill(t){for(const e of this.#e.values())e.value=t}}function n(t,i){const a=s(t.length),n=new o(e=>t[e]);return new Proxy(t,{get(t,e,r){if("length"===e)return a.access(),t.length;const s=h(e);return void 0!==s?(n.access(s),i.wrap(t[s])):Object.hasOwn(c,e)?c[e]:Reflect.get(t,e,r)},set(t,e,s,o){if("length"===e)return r(()=>{const e=t.length;t.length=s;for(let r=e;r>=t.length;r--)n.update(r,void 0);a.value=Number(s)}),!0;const c=h(e);return void 0!==c?(r(()=>{s=i.unwrap(s),t[c]=s,n.update(c,s)}),!0):Reflect.set(t,e,s,o)},has(t,e){const r=h(e);return void 0!==r?(n.access(r),r in t):Reflect.has(t,e)},deleteProperty(t,e){const s=h(e);return void 0!==s?(r(()=>{delete t[s],n.update(s,void 0)}),!0):Reflect.deleteProperty(t,e)},ownKeys(t){if(e()){a.access();for(let e=0;e<t.length;e++)n.access(e)}return Reflect.ownKeys(t)}})}const c=Object.create(null);for(const t of["copyWithin","fill","pop","push","reverse","shift","sort","splice","unshift"])c[t]=function(...e){return r(()=>Array.prototype[t].call(this,...e))};function h(t){if("symbol"==typeof t)return;const e=Number(t);return Number.isSafeInteger(e)&&e>=0&&e<=4294967295?e:void 0}class u extends Map{#s;#i;#a;#o;#n;#c;constructor(t,e){super(),this.#s=t,this.#i=e,this.#a=s(t.size),this.#o=s(),this.#n=new o(e=>t.get(e)),this.#c=new o(e=>t.has(e))}get size(){return this.#a.access(),this.#s.size}get(t){return this.#n.access(t),this.#i.wrap(this.#s.get(t))}has(t){return this.#c.access(t),this.#s.has(t)}set(t,e){return r(()=>{e=this.#i.unwrap(e),this.#s.set(t,e),this.#a.value=this.#s.size,this.#o.notify(),this.#n.update(t,e),this.#c.update(t,!0)}),this}delete(t){return r(()=>{const e=this.#s.delete(t);return e&&(this.#a.value=this.#s.size,this.#o.notify(),this.#n.update(t,void 0),this.#c.update(t,!1)),e})}clear(){r(()=>{this.#s.clear(),this.#a.value=0,this.#o.notify(),this.#n.fill(void 0),this.#c.fill(!1)})}*entries(){this.#o.access();for(const t of this.#s.entries())yield[t[0],this.#i.wrap(t[1])]}keys(){return this.#o.access(),this.#s.keys()}*values(){this.#o.access();for(const t of this.#s.values())yield this.#i.wrap(t)}forEach(t,e){return this.#o.access(),this.#s.forEach((r,s)=>t.call(e,this.#i.wrap(r),s,this))}*[Symbol.iterator](){this.#o.access();for(const t of this.#s.entries())yield[t[0],this.#i.wrap(t[1])]}get[Symbol.toStringTag](){return this.#s[Symbol.toStringTag]}}function l(t,e){const i=s(),a=new o(e=>t[e]),n=new o(e=>e in t),c=Object.getPrototypeOf(t);function h(t){return null===c||!(t in c)}return new Proxy(t,{get:(t,r,s)=>(h(r)&&a.access(r),e.wrap(Reflect.get(t,r,s))),has:(t,e)=>(h(e)&&n.access(e),Reflect.has(t,e)),set:(t,s,o,c)=>(o=e.unwrap(o),h(s)?r(()=>{const e=Reflect.set(t,s,o,c);return e&&(i.notify(),a.update(s,o),n.update(s,!0)),e}):Reflect.set(t,s,o,c)),deleteProperty:(t,e)=>r(()=>{const r=Reflect.deleteProperty(t,e);return r&&h(e)&&(i.notify(),a.update(e,void 0),n.update(e,!1)),r}),ownKeys:t=>(i.access(),Reflect.ownKeys(t))})}class p extends Set{#s;#i;#a;#o;#e;constructor(t,e){super(),this.#s=t,this.#i=e,this.#a=s(t.size),this.#o=s(),this.#e=new o(e=>t.has(e))}get size(){return this.#a.access(),this.#s.size}has(t){return t=this.#i.unwrap(t),this.#e.access(t),this.#s.has(t)}add(t){return r(()=>{t=this.#i.unwrap(t),this.#s.add(t),this.#a.value=this.#s.size,this.#o.notify(),this.#e.update(t,!0)}),this}delete(t){return r(()=>{t=this.#i.unwrap(t);const e=this.#s.delete(t);return e&&(this.#a.value=this.#s.size,this.#o.notify(),this.#e.update(t,!1)),e})}clear(){r(()=>{this.#s.clear(),this.#a.value=0,this.#o.notify(),this.#e.fill(!1)})}*entries(){this.#o.access();for(const t of this.#s.entries()){const e=this.#i.wrap(t[0]);yield[e,e]}}*keys(){this.#o.access();for(const t of this.#s.keys())yield this.#i.wrap(t)}*values(){this.#o.access();for(const t of this.#s.values())yield this.#i.wrap(t)}forEach(t,e){return this.#o.access(),this.#s.forEach(r=>{r=this.#i.wrap(r),t.call(e,r,r,this)},e)}*[Symbol.iterator](){this.#o.access();for(const t of this.#s)yield this.#i.wrap(t)}get[Symbol.toStringTag](){return this.#s[Symbol.toStringTag]}}const f=Symbol.for("rvx:store:wrap_instance"),g=new WeakMap,b=new WeakMap,d={wrap:y,unwrap:w};function y(t){if(null!==t&&"object"==typeof t){if(b.has(t))return t;let e=g.get(t);if(void 0!==e)return e;const r=t.constructor[f];if(r)e=r(t);else{switch(Object.getPrototypeOf(t)){case Object.prototype:e=l(t,d);break;case Array.prototype:e=n(t,d);break;case Map.prototype:e=new u(t,d);break;case Set.prototype:e=new p(t,d);break;default:return t}}return g.set(t,e),b.set(e,t),e}return t}function w(t){if(null!==t&&"object"==typeof t){const e=b.get(t);if(void 0!==e)return e}return t}function v(t){return l(t,d)}function z(t,e){Object.defineProperty(t,f,{configurable:!0,enumerable:!1,writable:!1,value:e??v})}function P(t,e){const r=s(i(()=>t[e],t=>r.value=t));return i(r,r=>t[e]=r),r}export{d as BARRIER,o as ProbeMap,a as ProbeSignal,u as ReactiveMap,p as ReactiveSet,n as createReactiveArrayProxy,l as createReactiveProxy,P as reflect,w as unwrap,y as wrap,z as wrapInstancesOf};