-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.min.js
More file actions
2 lines (2 loc) · 2.68 KB
/
Copy pathindex.min.js
File metadata and controls
2 lines (2 loc) · 2.68 KB
1
2
import{createContext as e,useCallback as r,useContext as t,useEffect as n,useMemo as s,useRef as c,useState as i}from"react";import{jsx as o,jsxs as u}from"react/jsx-runtime";var a=localStorage,f=e({}),{Provider:p}=f,l=(e,r)=>r?"function"==typeof r?r(e):r.split(".").reduce((e,r,t,n)=>{if(!e){const s=n.slice(0,t).join(".");throw new Error(`Cannot read '${s}.${r}' since '${s}' is '${e}'`)}return e[r]},e):e,d=(e,r,t)=>{if(!r)return t;const[n,...s]=r.split("."),c=s.join("."),i=c?d(e[n],c,t):t;return Array.isArray(e)?e.map((e,r)=>r===Number(n)?i:e):{...e,[n]:i}},h=e=>{if("object"!=typeof e)return e;if(Object.isFrozen(e))return e;for(const r of Object.getOwnPropertyNames(e))Array.isArray(e)&&"length"===r||(e[r]="object"==typeof e[r]?h(e[r]):e[r]);return Object.freeze(e)},b=e=>{const r=e??(e=>e),{state:c,subscribe:o}=t(f),[,u]=i();n(()=>o(e=>{try{if(l(e,r)===l(c.current,r))return;u({})}catch(t){}}),[e]);const a=l(c.current,r);return s(()=>h(a),[a])},m=e=>{b(e);const{state:n,setState:s}=t(f);return r(((e,r,t)=>{const n=l(e.current,r),s=n=>{const s=l(e.current,r);for(;"function"==typeof n;)n=n(h(s));const c=n=>t(d(e.current,r,n));return n&&n.then?n.then(c):c(n)};if(Array.isArray(n)){const e=e=>{s(r=>{const t=r.slice();return e(t),t})};s.fill=(...r)=>e(e=>e.fill(...r)),s.pop=(...r)=>e(e=>e.pop(...r)),s.push=(...r)=>e(e=>e.push(...r)),s.reverse=(...r)=>e(e=>e.reverse(...r)),s.shift=(...r)=>e(e=>e.shift(...r)),s.sort=(...r)=>e(e=>e.sort(...r)),s.splice=(...r)=>e(e=>e.splice(...r)),s.unshift=(...r)=>e(e=>e.unshift(...r)),s.concat=(...e)=>s(r=>r.concat(...e)),s.slice=(...e)=>s(r=>r.slice(...e)),s.filter=(...e)=>s(r=>r.filter(...e)),s.map=(...e)=>s(r=>r.map(...e)),s.reduce=(...e)=>s(r=>r.reduce(...e)),s.reduceRight=(...e)=>s(r=>r.reduceRight(...e)),s.append=s.push,s.prepend=s.unshift,s.remove=e=>s.splice(Number(e),1)}else"object"==typeof n?(s.assign=(...e)=>s(Object.assign({},n,...e)),s.remove=(...e)=>s(((e,r)=>{const t={};for(const n in e)r.includes(n)||(t[n]=e[n]);return t})(n,e)),s.extend=s.assign):"number"==typeof n&&(s.add=e=>s(r=>r+e),s.substract=e=>s(r=>r-e));return s})(n,e,s),[e])},y=e=>{const r=b(e),t=m(e);return s(()=>[r,t],[r,t])},j=({id:e})=>{const r=b(e);return n(()=>{a[e]=JSON.stringify(r)},[r]),null},g=({children:e,...r})=>{const t=Object.keys(r).filter(e=>e.startsWith("$")).map(e=>{const t=e.slice(1);return r[t]=a[t]?JSON.parse(a[t]):r[e],delete r[e],t}),n=c(r),s=[];/* @__PURE__ */
return u(p,{value:{state:n,setState:e=>{const r=n.current;n.current=e;for(let t=s.length-1;t>=0;t--)s[t](r)},subscribe:e=>(s.push(e),()=>s.splice(s.findIndex(r=>r===e),1))},children:[t.map(e=>/* @__PURE__ */o(j,{id:e},e)),e]})};export{f as Context,g as default,a as store,m as useActions,b as useSelector,y as useStore};