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.64 kB
JavaScript
var g=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var A=(e,t)=>{for(var n in t)g(e,n,{get:t[n],enumerable:!0})},G=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of R(t))!y.call(e,s)&&s!==n&&g(e,s,{get:()=>t[s],enumerable:!(o=x(t,s))||o.enumerable});return e};var P=e=>G(g({},"__esModule",{value:!0}),e);var u=(e,t,n)=>new Promise((o,s)=>{var r=i=>{try{T(n.next(i))}catch(c){s(c)}},l=i=>{try{T(n.throw(i))}catch(c){s(c)}},T=i=>i.done?o(i.value):Promise.resolve(i.value).then(r,l);T((n=n.apply(e,t)).next())});var m={};A(m,{createHook:()=>v,createSetter:()=>w,globalRGS:()=>a,initWithPlugins:()=>h,useRGSWithPlugins:()=>b});module.exports=P(m);var f=require("react");var S=globalThis;S.rgs||(S.rgs={});var a=S.rgs,p=e=>e.l.forEach(t=>t()),w=e=>t=>{let n=a[e];n.v=typeof t=="function"?t(n.v):t,n.l.forEach(o=>o())},v=e=>{let t=a[e],n=()=>t.v;return[(0,f.useSyncExternalStore)(s=>(t.l.push(s),()=>{t.l=t.l.filter(r=>r!==s)}),n,n),t.s]},d=!1,E=(e,t)=>u(void 0,null,function*(){var s;let n=a[e],o=r=>{n.v=r,p(n)};for(let r of t)yield(s=r.init)==null?void 0:s.call(r,e,n.v,o);d=!0}),h=(e,t,n=[],o=!1)=>{if(t=t instanceof Function?t():t,o){a[e]={v:t,l:[],s:null};return}let s=l=>{if(!d)return;let T=a[e];T.v=l instanceof Function?l(T.v):l,p(T),n.forEach(i=>{var c;return(c=i.onChange)==null?void 0:c.call(i,e,T.v)})},r=a[e];r?(r.v=t,r.s=s):a[e]={v:t,l:[],s},E(e,n)},b=(e,t,n,o=!1)=>{var s;return(s=a[e])!=null&&s.s||h(e,t,n,o),v(e)};0&&(module.exports={createHook,createSetter,globalRGS,initWithPlugins,useRGSWithPlugins});
;