react19-store
Version:
A simple yet elegant, light weight, react18 global store to replace Zustand for better tree shaking.
2 lines (1 loc) • 865 B
JavaScript
import{a as S}from"./chunk-IDU63W3I.mjs";import{useSyncExternalStore as p}from"react";var l=globalThis;l.rgs||(l.rgs={});var i=l.rgs,u=e=>e.l.forEach(t=>t()),R=e=>t=>{let n=i[e];n.v=typeof t=="function"?t(n.v):t,n.l.forEach(r=>r())},v=e=>{let t=i[e],n=()=>t.v;return[p(s=>(t.l.push(s),()=>{t.l=t.l.filter(o=>o!==s)}),n,n),t.s]},f=!1,d=(e,t)=>S(void 0,null,function*(){var s;let n=i[e],r=o=>{n.v=o,u(n)};for(let o of t)yield(s=o.init)==null?void 0:s.call(o,e,n.v,r);f=!0}),h=(e,t,n=[],r=!1)=>{if(t=t instanceof Function?t():t,r){i[e]={v:t,l:[],s:null};return}let s=T=>{if(!f)return;let a=i[e];a.v=T instanceof Function?T(a.v):T,u(a),n.forEach(c=>{var g;return(g=c.onChange)==null?void 0:g.call(c,e,a.v)})},o=i[e];o?(o.v=t,o.s=s):i[e]={v:t,l:[],s},d(e,n)},y=(e,t,n,r=!1)=>{var s;return(s=i[e])!=null&&s.s||h(e,t,n,r),v(e)};export{i as a,R as b,v as c,h as d,y as e};