stunk
Version:
Stunk is a lightweight, framework-agnostic state management library for JavaScript and TypeScript. It uses chunk-based state units for efficient updates, reactivity, and performance optimization in React, Vue, Svelte, and Vanilla JS/TS applications.
2 lines • 1.24 kB
JavaScript
var d=(e,n)=>{console.log("Setting value:",e),n(e);};var u=(e,n)=>{if(e<0)throw new Error("Value must be non-negative!");n(e);};function g(e,n={}){let{maxHistory:l=100}=n,t=[e.get()],r=0,s=false,o={...e,set:i=>{if(s){e.set(i);return}let c;if(typeof i=="function"){let a=e.get();c=i(a);}else c=i;if(t.splice(r+1),t.push(c),t.length>l){console.warn("History limit reached. Removing oldest entries.");let a=t.length-l;t.splice(0,a),r=Math.max(0,r-a);}r=t.length-1,e.set(c);},undo:()=>{o.canUndo()&&(s=true,r--,o.set(t[r]),s=false);},redo:()=>{o.canRedo()&&(s=true,r++,o.set(t[r]),s=false);},canUndo:()=>r>0,canRedo:()=>r<t.length-1,getHistory:()=>[...t],clearHistory:()=>{let i=e.get();t.length=0,t.push(i),r=0;},destroy:()=>{t.length=0,e.destroy();}};return o}function h(e,n){let{key:l,storage:t=localStorage,serialize:r=JSON.stringify,deserialize:s=JSON.parse}=n;try{let o=t.getItem(l);if(o){let i=s(o);e.set(i);}}catch(o){console.error("Failed to load persisted state:",o);}return e.subscribe(o=>{try{let i=r(o);t.setItem(l,i);}catch(i){console.log("Failed to persist chunk",i);}}),e}export{d as logger,u as nonNegativeValidator,g as withHistory,h as withPersistence};//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map