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