UNPKG

0x1

Version:

0x1: Lightning-fast web framework for JavaScript/TypeScript with zero overhead and maximum performance, powered by Bun

2 lines (1 loc) 2.8 kB
function $(J,Q={}){let{persist:G=!1,key:H="store"}=Q,z=J;if(G&&typeof window!=="undefined")try{let q=window.localStorage.getItem(H);if(q)z={...J,...JSON.parse(q)}}catch(q){console.warn("Failed to load persisted state:",q)}let B=new Set,N=()=>{if(G&&typeof window!=="undefined")try{window.localStorage.setItem(H,JSON.stringify(z))}catch(q){console.warn("Failed to persist state:",q)}};return{getState:()=>z,setState:(q)=>{z=typeof q==="function"?{...z,...q(z)}:{...z,...q},N(),B.forEach((U)=>U(z))},subscribe:(q)=>{return B.add(q),()=>B.delete(q)},reset:()=>{z=J,N(),B.forEach((q)=>q(z))}}}function _(J,Q,G=[]){let H=Q,z=new Set,B=new Map,N=(q)=>{return H=J(H,q),B.clear(),z.forEach((O)=>O(H)),q};if(G.length>0){let q={getState:()=>H,dispatch:(U)=>N(U)};N=G.map((U)=>U(q)).reduce((U,W)=>W(U),N)}return{getState:()=>H,dispatch:N,subscribe:(q)=>{return z.add(q),()=>{z.delete(q)}},select:function(q,O){let U=()=>{if(B.has(q))return B.get(q);let W=q(H);return B.set(q,W),W};if(O){let W=U(),X=this.subscribe((Y)=>{let Z=q(Y);if(Z!==W)W=Z,O(Z)});return()=>{return X(),U()}}return U}}}function L(J){return(Q={},G)=>{let H={},z=!1;return Object.entries(J).forEach(([B,N])=>{let q=Q[B],O=N(q,G);H[B]=O,z=z||O!==q}),z?{...Q,...H}:Q}}var M=(J)=>(Q)=>(G)=>{if(typeof G==="function")return G(J.dispatch,J.getState);return Q(G)};function E(J,Q){let G,H,z;return(B)=>{if(B!==z){let N=J(B);if(N!==G)H=Q(N);G=N,z=B}return H}}function A(J,Q,G){let H,z,B,N;return(q)=>{if(q!==N){let O=J(q),U=Q(q);if(O!==H||U!==z)B=G(O,U);H=O,z=U,N=q}return B}}function K({name:J,initialState:Q,reducers:G}){let H={},z=(B=Q,N)=>{let q=G[N.type.replace(`${J}/`,"")];if(q){let O=q(B,N.payload);return O===void 0?B:O}return B};return Object.keys(G).forEach((B)=>{let N=`${J}/${B}`,q=(O)=>({type:N,payload:O});Object.defineProperty(q,"type",{value:N,writable:!1}),H[B]=q}),{name:J,reducer:z,actions:H}}var f=(J)=>(Q)=>(G)=>{console.group(`Action: ${G.type}`),console.log("Previous State:",J.getState()),console.log("Action:",G);let H=Q(G);return console.log("Next State:",J.getState()),console.groupEnd(),H};function j(J,Q){return(G)=>{return(H)=>{let z=window.__0x1_STORE__;if(!z)return console.error("Store not found. Make sure to initialize store first."),G(H);let B=J(z.getState()),N={};if(Q)N=Object.entries(Q).reduce((U,[W,X])=>{return U[W]=(...Y)=>X(z.dispatch)(...Y),U},{});let q={...H,...B,...N},O=G(q);return z.subscribe((U)=>{let W=J(U),X={...H,...W,...N};if(JSON.stringify(W)!==JSON.stringify(B)){let Y=G(X);if(O.parentNode)O.parentNode.replaceChild(Y,O)}}),O}}}function D(J){window.__0x1_STORE__=J}export{M as thunk,f as logger,D as initializeStore,_ as createStore,K as createSlice,$ as createSimpleStore,A as createSelector2,E as createSelector,_ as createReduxStore,_ as createAdvancedStore,j as connect,L as combineReducers};