react18-global-store
Version:
A simple yet elegant, light weight, react18 global store to replace Zustand for better tree shaking.
2 lines (1 loc) • 1.55 kB
JavaScript
var g=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var A=(n,t)=>{for(var e in t)g(n,e,{get:t[e],enumerable:!0})},y=(n,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of x(t))!d.call(n,o)&&o!==e&&g(n,o,{get:()=>t[o],enumerable:!(s=P(t,o))||s.enumerable});return n};var R=n=>y(g({},"__esModule",{value:!0}),n);var p=(n,t,e)=>new Promise((s,o)=>{var i=r=>{try{a(e.next(r))}catch(c){o(c)}},l=r=>{try{a(e.throw(r))}catch(c){o(c)}},a=r=>r.done?s(r.value):Promise.resolve(r.value).then(i,l);a((e=e.apply(n,t)).next())});var b={};A(b,{create:()=>w,useRGSWithPlugins:()=>u,withPlugins:()=>W});module.exports=R(b);var f=require("react");var S=globalThis;S.rgs||(S.rgs={});var T=S.rgs,h=n=>n.l.forEach(t=>t());var G=n=>{let t=T[n],e=()=>t.v;return[(0,f.useSyncExternalStore)(o=>(t.l.push(o),()=>{t.l=t.l.filter(i=>i!==o)}),e,e),t.s]},v=!1,U=(n,t)=>p(void 0,null,function*(){var o;let e=T[n],s=i=>{e.v=i,h(e)};for(let i of t)yield(o=i.init)==null?void 0:o.call(i,n,e.v,s);v=!0}),m=(n,t,e=[],s=!1)=>{if(t=t instanceof Function?t():t,s){T[n]={v:t,l:[],s:null};return}let o=l=>{if(!v)return;let a=T[n];a.v=l instanceof Function?l(a.v):l,h(a),e.forEach(r=>{var c;return(c=r.onChange)==null?void 0:c.call(r,n,a.v)})},i=T[n];i?(i.v=t,i.s=o):T[n]={v:t,l:[],s:o},U(n,e)},u=(n,t,e,s=!1)=>{var o;return(o=T[n])!=null&&o.s||m(n,t,e,s),G(n)};var w=(n,t,e)=>()=>u(n,t,e),W=n=>(e,s,o=!1)=>u(e,s,n,o);0&&(module.exports={create,useRGSWithPlugins,withPlugins});
;