Comparing version 0.5.12 to 0.5.13
# rko | ||
## 0.5.13 | ||
- Updates `resetState` so that it returns to the provided initial state, rather than the restored persisted state. | ||
- Deep copies the initial state (or restored persisted state) to prevent clashes from mutations. | ||
- Adds additional tests. | ||
## 0.5.9 | ||
@@ -4,0 +10,0 @@ |
@@ -1,1 +0,1 @@ | ||
var U=Object.defineProperty;var _=Object.getOwnPropertySymbols;var D=Object.prototype.hasOwnProperty,I=Object.prototype.propertyIsEnumerable;var P=(n,t,e)=>t in n?U(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,v=(n,t)=>{for(var e in t||(t={}))D.call(t,e)&&P(n,e,t[e]);if(_)for(var e of _(t))I.call(t,e)&&P(n,e,t[e]);return n};var k=(n,t,e)=>new Promise((r,i)=>{var o=u=>{try{f(e.next(u))}catch(s){i(s)}},d=u=>{try{f(e.throw(u))}catch(s){i(s)}},f=u=>u.done?r(u.value):Promise.resolve(u.value).then(o,d);f((e=e.apply(n,t)).next())});function R(n){let t,e=new Set,r=(s,c)=>{let a=typeof s=="function"?s(t):s;if(a!==t){let h=t;t=c?a:Object.assign({},t,a),e.forEach(p=>p(t,h))}},i=()=>t,o=(s,c=i,a=Object.is)=>{let h=c(t);function p(){let S=c(t);if(!a(h,S)){let l=h;s(h=S,l)}}return e.add(p),()=>e.delete(p)},u={setState:r,getState:i,subscribe:(s,c,a)=>c||a?o(s,c,a):(e.add(s),()=>e.delete(s)),destroy:()=>e.clear()};return t=n(r,i,u),u}import{useReducer as B,useRef as b,useEffect as V,useLayoutEffect as z}from"react";function G(n){let t,e=new Set,r=(s,c)=>{let a=typeof s=="function"?s(t):s;if(a!==t){let h=t;t=c?a:Object.assign({},t,a),e.forEach(p=>p(t,h))}},i=()=>t,o=(s,c=i,a=Object.is)=>{let h=c(t);function p(){let S=c(t);if(!a(h,S)){let l=h;s(h=S,l)}}return e.add(p),()=>e.delete(p)},u={setState:r,getState:i,subscribe:(s,c,a)=>c||a?o(s,c,a):(e.add(s),()=>e.delete(s)),destroy:()=>e.clear()};return t=n(r,i,u),u}var H=typeof window=="undefined"||!window.navigator||/ServerSideRendering|^Deno\//.test(window.navigator.userAgent),j=H?V:z;function A(n){let t=typeof n=="function"?G(n):n,e=(r=t.getState,i=Object.is)=>{let[,o]=B(l=>l+1,0),d=t.getState(),f=b(d),u=b(r),s=b(i),c=b(!1),a=b();a.current===void 0&&(a.current=r(d));let h,p=!1;(f.current!==d||u.current!==r||s.current!==i||c.current)&&(h=r(d),p=!i(a.current,h)),j(()=>{p&&(a.current=h),f.current=d,u.current=r,s.current=i,c.current=!1});let S=b(d);return j(()=>{let l=()=>{try{let g=t.getState(),w=u.current(g);s.current(a.current,w)||(f.current=g,a.current=w,o())}catch(g){c.current=!0,o()}},E=t.subscribe(l);return t.getState()!==S.current&&l(),E},[]),p?h:a.current};return Object.assign(e,t),e[Symbol.iterator]=function(){console.warn("[useStore, api] = create() is deprecated and will be removed in v4");let r=[e,t];return{next(){let i=r.length<=0;return{value:r.shift(),done:i}}}},e}function L(){if(!(!navigator.userAgentData&&/Safari\//.test(navigator.userAgent)&&!/Chrom(e|ium)\//.test(navigator.userAgent))||!indexedDB.databases)return Promise.resolve();let t;return new Promise(e=>{let r=()=>indexedDB.databases().finally(e);t=setInterval(r,100),r()}).finally(()=>clearInterval(t))}var M=L;function y(n){return new Promise((t,e)=>{n.oncomplete=n.onsuccess=()=>t(n.result),n.onabort=n.onerror=()=>e(n.error)})}function W(n,t){let e=M().then(()=>{let r=indexedDB.open(n);return r.onupgradeneeded=()=>r.result.createObjectStore(t),y(r)});return(r,i)=>e.then(o=>i(o.transaction(t,r).objectStore(t)))}var m;function O(){return m||(m=W("keyval-store","keyval")),m}function x(n,t=O()){return t("readonly",e=>y(e.get(n)))}function T(n,t,e=O()){return e("readwrite",r=>(r.put(t,n),y(r.transaction)))}function C(n,t){let e=v({},n),r=Object.entries(t);for(let[i,o]of r)e[i]=o===Object(o)&&!Array.isArray(o)?C(e[i],o):o;return e}var $=class{constructor(t,e,r,i){this.stack=[];this.pointer=-1;this._status="loading";this.persist=()=>{this.id&&T(this.id,this._state)};this.applyPatch=t=>{let e=this._state,r=C(this._state,t);return this._state=this.cleanup(r,e,t),this.store.setState(this._state,!0),this};this.cleanup=(t,e,r)=>t;this.patchState=t=>(this.applyPatch(t),this);this.replaceState=t=>(this._state=this.cleanup(t,this._state,t),this.store.setState(this._state,!0),this);this.setState=t=>(this.pointer++,this.stack=this.stack.slice(0,this.pointer),this.stack.push(t),this.applyPatch(t.after),this.persist(),this);this.reset=()=>(this._state=this.initialState,this.store.setState(this.initialState,!0),this.resetHistory(),this.persist(),this);this.resetHistory=()=>(this.stack=[],this.pointer=-1,this);this.undo=()=>{if(!this.canUndo)return this;let t=this.stack[this.pointer];return this.pointer--,this.applyPatch(t.before),this.persist(),this};this.redo=()=>{if(!this.canRedo)return this;this.pointer++;let t=this.stack[this.pointer];return this.applyPatch(t.after),this.persist(),this};this.setSnapshot=()=>(this.snapshot=v({},this._state),this);this.forceUpdate=()=>{this.store.setState(this._state,!0)};this.id=e,this._state=t,this.snapshot=t,this.initialState=t,this.store=R(()=>t),this.useStore=A(this.store),this.id&&x(this.id).then(o=>k(this,null,function*(){if(o){let d=o;if(r){let f=yield x(e+"_version");f&&f<r&&(d=i?i(o,t,f):t)}yield T(e+"_version",r||-1),this._state=d,this.snapshot=d,this.initialState=d,this._status="ready",this.store.setState(this._state,!0)}else yield T(e+"_version",r||-1),this._status="ready"}))}get canUndo(){return this.pointer>-1}get canRedo(){return this.pointer<this.stack.length-1}get state(){return this._state}get status(){return this._status}};export{$ as StateManager}; | ||
var U=Object.defineProperty;var P=Object.getOwnPropertySymbols;var I=Object.prototype.hasOwnProperty,B=Object.prototype.propertyIsEnumerable;var R=(r,t,e)=>t in r?U(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,m=(r,t)=>{for(var e in t||(t={}))I.call(t,e)&&R(r,e,t[e]);if(P)for(var e of P(t))B.call(t,e)&&R(r,e,t[e]);return r};var j=(r,t,e)=>new Promise((s,i)=>{var o=u=>{try{f(e.next(u))}catch(n){i(n)}},p=u=>{try{f(e.throw(u))}catch(n){i(n)}},f=u=>u.done?s(u.value):Promise.resolve(u.value).then(o,p);f((e=e.apply(r,t)).next())});function M(r){let t,e=new Set,s=(n,c)=>{let a=typeof n=="function"?n(t):n;if(a!==t){let h=t;t=c?a:Object.assign({},t,a),e.forEach(d=>d(t,h))}},i=()=>t,o=(n,c=i,a=Object.is)=>{let h=c(t);function d(){let b=c(t);if(!a(h,b)){let l=h;n(h=b,l)}}return e.add(d),()=>e.delete(d)},u={setState:s,getState:i,subscribe:(n,c,a)=>c||a?o(n,c,a):(e.add(n),()=>e.delete(n)),destroy:()=>e.clear()};return t=r(s,i,u),u}import{useReducer as V,useRef as y,useEffect as z,useLayoutEffect as G}from"react";function H(r){let t,e=new Set,s=(n,c)=>{let a=typeof n=="function"?n(t):n;if(a!==t){let h=t;t=c?a:Object.assign({},t,a),e.forEach(d=>d(t,h))}},i=()=>t,o=(n,c=i,a=Object.is)=>{let h=c(t);function d(){let b=c(t);if(!a(h,b)){let l=h;n(h=b,l)}}return e.add(d),()=>e.delete(d)},u={setState:s,getState:i,subscribe:(n,c,a)=>c||a?o(n,c,a):(e.add(n),()=>e.delete(n)),destroy:()=>e.clear()};return t=r(s,i,u),u}var L=typeof window=="undefined"||!window.navigator||/ServerSideRendering|^Deno\//.test(window.navigator.userAgent),A=L?z:G;function O(r){let t=typeof r=="function"?H(r):r,e=(s=t.getState,i=Object.is)=>{let[,o]=V(l=>l+1,0),p=t.getState(),f=y(p),u=y(s),n=y(i),c=y(!1),a=y();a.current===void 0&&(a.current=s(p));let h,d=!1;(f.current!==p||u.current!==s||n.current!==i||c.current)&&(h=s(p),d=!i(a.current,h)),A(()=>{d&&(a.current=h),f.current=p,u.current=s,n.current=i,c.current=!1});let b=y(p);return A(()=>{let l=()=>{try{let g=t.getState(),k=u.current(g);n.current(a.current,k)||(f.current=g,a.current=k,o())}catch(g){c.current=!0,o()}},E=t.subscribe(l);return t.getState()!==b.current&&l(),E},[]),d?h:a.current};return Object.assign(e,t),e[Symbol.iterator]=function(){console.warn("[useStore, api] = create() is deprecated and will be removed in v4");let s=[e,t];return{next(){let i=s.length<=0;return{value:s.shift(),done:i}}}},e}function W(){if(!(!navigator.userAgentData&&/Safari\//.test(navigator.userAgent)&&!/Chrom(e|ium)\//.test(navigator.userAgent))||!indexedDB.databases)return Promise.resolve();let t;return new Promise(e=>{let s=()=>indexedDB.databases().finally(e);t=setInterval(s,100),s()}).finally(()=>clearInterval(t))}var C=W;function v(r){return new Promise((t,e)=>{r.oncomplete=r.onsuccess=()=>t(r.result),r.onabort=r.onerror=()=>e(r.error)})}function K(r,t){let e=C().then(()=>{let s=indexedDB.open(r);return s.onupgradeneeded=()=>s.result.createObjectStore(t),v(s)});return(s,i)=>e.then(o=>i(o.transaction(t,s).objectStore(t)))}var _;function D(){return _||(_=K("keyval-store","keyval")),_}function w(r,t=D()){return t("readonly",e=>v(e.get(r)))}function T(r,t,e=D()){return e("readwrite",s=>(s.put(t,r),v(s.transaction)))}function S(r){if(r===null)return r;if(r instanceof Date)return new Date(r.getTime());if(typeof r=="object")if(typeof r[Symbol.iterator]=="function"){let t=[];if(r.length>0)for(let e of r)t.push(S(e));return t}else{let t=Object.keys(r),e={};if(t.length>0)for(let s of t)e[s]=S(r[s]);return e}return r}function x(r,t){let e=m({},r),s=Object.entries(t);for(let[i,o]of s)e[i]=o===Object(o)&&!Array.isArray(o)?x(e[i],o):o;return e}var $=class{constructor(t,e,s,i){this.stack=[];this.pointer=-1;this._status="loading";this.persist=()=>{this.id&&T(this.id,this._state)};this.applyPatch=t=>{let e=this._state,s=x(this._state,t);return this._state=this.cleanup(s,e,t),this.store.setState(this._state,!0),this};this.cleanup=(t,e,s)=>t;this.patchState=t=>(this.applyPatch(t),this);this.replaceState=t=>(this._state=this.cleanup(t,this._state,t),this.store.setState(this._state,!0),this);this.setState=t=>(this.pointer++,this.stack=this.stack.slice(0,this.pointer),this.stack.push(t),this.applyPatch(t.after),this.persist(),this);this.reset=()=>(this._state=this.initialState,this.store.setState(this.initialState,!0),this.resetHistory(),this.persist(),this);this.resetHistory=()=>(this.stack=[],this.pointer=-1,this);this.undo=()=>{if(!this.canUndo)return this;let t=this.stack[this.pointer];return this.pointer--,this.applyPatch(t.before),this.persist(),this};this.redo=()=>{if(!this.canRedo)return this;this.pointer++;let t=this.stack[this.pointer];return this.applyPatch(t.after),this.persist(),this};this.setSnapshot=()=>(this._snapshot=m({},this._state),this);this.forceUpdate=()=>{this.store.setState(this._state,!0)};this.id=e,this._state=S(t),this._snapshot=S(t),this.initialState=S(t),this.store=M(()=>this._state),this.useStore=O(this.store),this.id&&w(this.id).then(o=>j(this,null,function*(){if(o){let p=o;if(s){let f=yield w(e+"_version");f&&f<s&&(p=i?i(o,t,f):t)}yield T(e+"_version",s||-1),this._state=S(p),this._snapshot=S(p),this._status="ready",this.store.setState(this._state,!0)}else yield T(e+"_version",s||-1),this._status="ready"}))}get canUndo(){return this.pointer>-1}get canRedo(){return this.pointer<this.stack.length-1}get state(){return this._state}get status(){return this._status}get snapshot(){return this._snapshot}};export{$ as StateManager}; |
@@ -1,1 +0,1 @@ | ||
var V=Object.create;var T=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames,P=Object.getOwnPropertySymbols,H=Object.getPrototypeOf,k=Object.prototype.hasOwnProperty,L=Object.prototype.propertyIsEnumerable;var R=(r,t,e)=>t in r?T(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,y=(r,t)=>{for(var e in t||(t={}))k.call(t,e)&&R(r,e,t[e]);if(P)for(var e of P(t))L.call(t,e)&&R(r,e,t[e]);return r};var j=r=>T(r,"__esModule",{value:!0});var W=(r,t)=>{j(r);for(var e in t)T(r,e,{get:t[e],enumerable:!0})},$=(r,t,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of G(t))!k.call(r,n)&&n!=="default"&&T(r,n,{get:()=>t[n],enumerable:!(e=z(t,n))||e.enumerable});return r},J=r=>$(j(T(r!=null?V(H(r)):{},"default",r&&r.__esModule&&"default"in r?{get:()=>r.default,enumerable:!0}:{value:r,enumerable:!0})),r);var A=(r,t,e)=>new Promise((n,i)=>{var o=u=>{try{f(e.next(u))}catch(s){i(s)}},d=u=>{try{f(e.throw(u))}catch(s){i(s)}},f=u=>u.done?n(u.value):Promise.resolve(u.value).then(o,d);f((e=e.apply(r,t)).next())});W(exports,{StateManager:()=>I});function M(r){let t,e=new Set,n=(s,c)=>{let a=typeof s=="function"?s(t):s;if(a!==t){let h=t;t=c?a:Object.assign({},t,a),e.forEach(p=>p(t,h))}},i=()=>t,o=(s,c=i,a=Object.is)=>{let h=c(t);function p(){let b=c(t);if(!a(h,b)){let S=h;s(h=b,S)}}return e.add(p),()=>e.delete(p)},u={setState:n,getState:i,subscribe:(s,c,a)=>c||a?o(s,c,a):(e.add(s),()=>e.delete(s)),destroy:()=>e.clear()};return t=r(n,i,u),u}var l=J(require("react"));function K(r){let t,e=new Set,n=(s,c)=>{let a=typeof s=="function"?s(t):s;if(a!==t){let h=t;t=c?a:Object.assign({},t,a),e.forEach(p=>p(t,h))}},i=()=>t,o=(s,c=i,a=Object.is)=>{let h=c(t);function p(){let b=c(t);if(!a(h,b)){let S=h;s(h=b,S)}}return e.add(p),()=>e.delete(p)},u={setState:n,getState:i,subscribe:(s,c,a)=>c||a?o(s,c,a):(e.add(s),()=>e.delete(s)),destroy:()=>e.clear()};return t=r(n,i,u),u}var Q=typeof window=="undefined"||!window.navigator||/ServerSideRendering|^Deno\//.test(window.navigator.userAgent),O=Q?l.useEffect:l.useLayoutEffect;function C(r){let t=typeof r=="function"?K(r):r,e=(n=t.getState,i=Object.is)=>{let[,o]=(0,l.useReducer)(S=>S+1,0),d=t.getState(),f=(0,l.useRef)(d),u=(0,l.useRef)(n),s=(0,l.useRef)(i),c=(0,l.useRef)(!1),a=(0,l.useRef)();a.current===void 0&&(a.current=n(d));let h,p=!1;(f.current!==d||u.current!==n||s.current!==i||c.current)&&(h=n(d),p=!i(a.current,h)),O(()=>{p&&(a.current=h),f.current=d,u.current=n,s.current=i,c.current=!1});let b=(0,l.useRef)(d);return O(()=>{let S=()=>{try{let v=t.getState(),_=u.current(v);s.current(a.current,_)||(f.current=v,a.current=_,o())}catch(v){c.current=!0,o()}},B=t.subscribe(S);return t.getState()!==b.current&&S(),B},[]),p?h:a.current};return Object.assign(e,t),e[Symbol.iterator]=function(){console.warn("[useStore, api] = create() is deprecated and will be removed in v4");let n=[e,t];return{next(){let i=n.length<=0;return{value:n.shift(),done:i}}}},e}function X(){if(!(!navigator.userAgentData&&/Safari\//.test(navigator.userAgent)&&!/Chrom(e|ium)\//.test(navigator.userAgent))||!indexedDB.databases)return Promise.resolve();let t;return new Promise(e=>{let n=()=>indexedDB.databases().finally(e);t=setInterval(n,100),n()}).finally(()=>clearInterval(t))}var E=X;function m(r){return new Promise((t,e)=>{r.oncomplete=r.onsuccess=()=>t(r.result),r.onabort=r.onerror=()=>e(r.error)})}function Y(r,t){let e=E().then(()=>{let n=indexedDB.open(r);return n.onupgradeneeded=()=>n.result.createObjectStore(t),m(n)});return(n,i)=>e.then(o=>i(o.transaction(t,n).objectStore(t)))}var x;function U(){return x||(x=Y("keyval-store","keyval")),x}function w(r,t=U()){return t("readonly",e=>m(e.get(r)))}function g(r,t,e=U()){return e("readwrite",n=>(n.put(t,r),m(n.transaction)))}function D(r,t){let e=y({},r),n=Object.entries(t);for(let[i,o]of n)e[i]=o===Object(o)&&!Array.isArray(o)?D(e[i],o):o;return e}var I=class{constructor(t,e,n,i){this.stack=[];this.pointer=-1;this._status="loading";this.persist=()=>{this.id&&g(this.id,this._state)};this.applyPatch=t=>{let e=this._state,n=D(this._state,t);return this._state=this.cleanup(n,e,t),this.store.setState(this._state,!0),this};this.cleanup=(t,e,n)=>t;this.patchState=t=>(this.applyPatch(t),this);this.replaceState=t=>(this._state=this.cleanup(t,this._state,t),this.store.setState(this._state,!0),this);this.setState=t=>(this.pointer++,this.stack=this.stack.slice(0,this.pointer),this.stack.push(t),this.applyPatch(t.after),this.persist(),this);this.reset=()=>(this._state=this.initialState,this.store.setState(this.initialState,!0),this.resetHistory(),this.persist(),this);this.resetHistory=()=>(this.stack=[],this.pointer=-1,this);this.undo=()=>{if(!this.canUndo)return this;let t=this.stack[this.pointer];return this.pointer--,this.applyPatch(t.before),this.persist(),this};this.redo=()=>{if(!this.canRedo)return this;this.pointer++;let t=this.stack[this.pointer];return this.applyPatch(t.after),this.persist(),this};this.setSnapshot=()=>(this.snapshot=y({},this._state),this);this.forceUpdate=()=>{this.store.setState(this._state,!0)};this.id=e,this._state=t,this.snapshot=t,this.initialState=t,this.store=M(()=>t),this.useStore=C(this.store),this.id&&w(this.id).then(o=>A(this,null,function*(){if(o){let d=o;if(n){let f=yield w(e+"_version");f&&f<n&&(d=i?i(o,t,f):t)}yield g(e+"_version",n||-1),this._state=d,this.snapshot=d,this.initialState=d,this._status="ready",this.store.setState(this._state,!0)}else yield g(e+"_version",n||-1),this._status="ready"}))}get canUndo(){return this.pointer>-1}get canRedo(){return this.pointer<this.stack.length-1}get state(){return this._state}get status(){return this._status}}; | ||
var z=Object.create;var m=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames,R=Object.getOwnPropertySymbols,L=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty,W=Object.prototype.propertyIsEnumerable;var M=(r,t,e)=>t in r?m(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,T=(r,t)=>{for(var e in t||(t={}))j.call(t,e)&&M(r,e,t[e]);if(R)for(var e of R(t))W.call(t,e)&&M(r,e,t[e]);return r};var A=r=>m(r,"__esModule",{value:!0});var K=(r,t)=>{A(r);for(var e in t)m(r,e,{get:t[e],enumerable:!0})},$=(r,t,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of H(t))!j.call(r,s)&&s!=="default"&&m(r,s,{get:()=>t[s],enumerable:!(e=G(t,s))||e.enumerable});return r},J=r=>$(A(m(r!=null?z(L(r)):{},"default",r&&r.__esModule&&"default"in r?{get:()=>r.default,enumerable:!0}:{value:r,enumerable:!0})),r);var O=(r,t,e)=>new Promise((s,i)=>{var o=u=>{try{f(e.next(u))}catch(n){i(n)}},p=u=>{try{f(e.throw(u))}catch(n){i(n)}},f=u=>u.done?s(u.value):Promise.resolve(u.value).then(o,p);f((e=e.apply(r,t)).next())});K(exports,{StateManager:()=>B});function C(r){let t,e=new Set,s=(n,c)=>{let a=typeof n=="function"?n(t):n;if(a!==t){let h=t;t=c?a:Object.assign({},t,a),e.forEach(d=>d(t,h))}},i=()=>t,o=(n,c=i,a=Object.is)=>{let h=c(t);function d(){let y=c(t);if(!a(h,y)){let S=h;n(h=y,S)}}return e.add(d),()=>e.delete(d)},u={setState:s,getState:i,subscribe:(n,c,a)=>c||a?o(n,c,a):(e.add(n),()=>e.delete(n)),destroy:()=>e.clear()};return t=r(s,i,u),u}var l=J(require("react"));function Q(r){let t,e=new Set,s=(n,c)=>{let a=typeof n=="function"?n(t):n;if(a!==t){let h=t;t=c?a:Object.assign({},t,a),e.forEach(d=>d(t,h))}},i=()=>t,o=(n,c=i,a=Object.is)=>{let h=c(t);function d(){let y=c(t);if(!a(h,y)){let S=h;n(h=y,S)}}return e.add(d),()=>e.delete(d)},u={setState:s,getState:i,subscribe:(n,c,a)=>c||a?o(n,c,a):(e.add(n),()=>e.delete(n)),destroy:()=>e.clear()};return t=r(s,i,u),u}var X=typeof window=="undefined"||!window.navigator||/ServerSideRendering|^Deno\//.test(window.navigator.userAgent),D=X?l.useEffect:l.useLayoutEffect;function E(r){let t=typeof r=="function"?Q(r):r,e=(s=t.getState,i=Object.is)=>{let[,o]=(0,l.useReducer)(S=>S+1,0),p=t.getState(),f=(0,l.useRef)(p),u=(0,l.useRef)(s),n=(0,l.useRef)(i),c=(0,l.useRef)(!1),a=(0,l.useRef)();a.current===void 0&&(a.current=s(p));let h,d=!1;(f.current!==p||u.current!==s||n.current!==i||c.current)&&(h=s(p),d=!i(a.current,h)),D(()=>{d&&(a.current=h),f.current=p,u.current=s,n.current=i,c.current=!1});let y=(0,l.useRef)(p);return D(()=>{let S=()=>{try{let v=t.getState(),P=u.current(v);n.current(a.current,P)||(f.current=v,a.current=P,o())}catch(v){c.current=!0,o()}},V=t.subscribe(S);return t.getState()!==y.current&&S(),V},[]),d?h:a.current};return Object.assign(e,t),e[Symbol.iterator]=function(){console.warn("[useStore, api] = create() is deprecated and will be removed in v4");let s=[e,t];return{next(){let i=s.length<=0;return{value:s.shift(),done:i}}}},e}function Y(){if(!(!navigator.userAgentData&&/Safari\//.test(navigator.userAgent)&&!/Chrom(e|ium)\//.test(navigator.userAgent))||!indexedDB.databases)return Promise.resolve();let t;return new Promise(e=>{let s=()=>indexedDB.databases().finally(e);t=setInterval(s,100),s()}).finally(()=>clearInterval(t))}var U=Y;function _(r){return new Promise((t,e)=>{r.oncomplete=r.onsuccess=()=>t(r.result),r.onabort=r.onerror=()=>e(r.error)})}function Z(r,t){let e=U().then(()=>{let s=indexedDB.open(r);return s.onupgradeneeded=()=>s.result.createObjectStore(t),_(s)});return(s,i)=>e.then(o=>i(o.transaction(t,s).objectStore(t)))}var w;function I(){return w||(w=Z("keyval-store","keyval")),w}function x(r,t=I()){return t("readonly",e=>_(e.get(r)))}function g(r,t,e=I()){return e("readwrite",s=>(s.put(t,r),_(s.transaction)))}function b(r){if(r===null)return r;if(r instanceof Date)return new Date(r.getTime());if(typeof r=="object")if(typeof r[Symbol.iterator]=="function"){let t=[];if(r.length>0)for(let e of r)t.push(b(e));return t}else{let t=Object.keys(r),e={};if(t.length>0)for(let s of t)e[s]=b(r[s]);return e}return r}function k(r,t){let e=T({},r),s=Object.entries(t);for(let[i,o]of s)e[i]=o===Object(o)&&!Array.isArray(o)?k(e[i],o):o;return e}var B=class{constructor(t,e,s,i){this.stack=[];this.pointer=-1;this._status="loading";this.persist=()=>{this.id&&g(this.id,this._state)};this.applyPatch=t=>{let e=this._state,s=k(this._state,t);return this._state=this.cleanup(s,e,t),this.store.setState(this._state,!0),this};this.cleanup=(t,e,s)=>t;this.patchState=t=>(this.applyPatch(t),this);this.replaceState=t=>(this._state=this.cleanup(t,this._state,t),this.store.setState(this._state,!0),this);this.setState=t=>(this.pointer++,this.stack=this.stack.slice(0,this.pointer),this.stack.push(t),this.applyPatch(t.after),this.persist(),this);this.reset=()=>(this._state=this.initialState,this.store.setState(this.initialState,!0),this.resetHistory(),this.persist(),this);this.resetHistory=()=>(this.stack=[],this.pointer=-1,this);this.undo=()=>{if(!this.canUndo)return this;let t=this.stack[this.pointer];return this.pointer--,this.applyPatch(t.before),this.persist(),this};this.redo=()=>{if(!this.canRedo)return this;this.pointer++;let t=this.stack[this.pointer];return this.applyPatch(t.after),this.persist(),this};this.setSnapshot=()=>(this._snapshot=T({},this._state),this);this.forceUpdate=()=>{this.store.setState(this._state,!0)};this.id=e,this._state=b(t),this._snapshot=b(t),this.initialState=b(t),this.store=C(()=>this._state),this.useStore=E(this.store),this.id&&x(this.id).then(o=>O(this,null,function*(){if(o){let p=o;if(s){let f=yield x(e+"_version");f&&f<s&&(p=i?i(o,t,f):t)}yield g(e+"_version",s||-1),this._state=b(p),this._snapshot=b(p),this._status="ready",this.store.setState(this._state,!0)}else yield g(e+"_version",s||-1),this._status="ready"}))}get canUndo(){return this.pointer>-1}get canRedo(){return this.pointer<this.stack.length-1}get state(){return this._state}get status(){return this._status}get snapshot(){return this._snapshot}}; |
@@ -259,2 +259,10 @@ ![logo](./rko-logo.svg) | ||
#### `canUndo` | ||
Whether the state can undo, given its undo/redo stack. Readonly. | ||
#### `canRedo` | ||
Whether the state can redo, given its undo/redo stack. Readonly. | ||
## Advanced Usage | ||
@@ -261,0 +269,0 @@ |
import { UseStore } from 'zustand'; | ||
export declare type Patch<T> = Partial<{ | ||
[P in keyof T]: Patch<T[P]>; | ||
}>; | ||
export interface Command<T extends object> { | ||
before: Patch<T>; | ||
after: Patch<T>; | ||
} | ||
import type { Patch, Command } from './types'; | ||
export declare class StateManager<T extends object> { | ||
@@ -41,3 +35,3 @@ /** | ||
*/ | ||
protected snapshot: T; | ||
protected _snapshot: T; | ||
/** | ||
@@ -128,2 +122,6 @@ * A React hook for accessing the zustand store. | ||
get status(): string; | ||
/** | ||
* The most-recent snapshot. | ||
*/ | ||
protected get snapshot(): T; | ||
} |
{ | ||
"private": false, | ||
"version": "0.5.12", | ||
"version": "0.5.13", | ||
"name": "rko", | ||
@@ -52,3 +52,3 @@ "author": { | ||
}, | ||
"gitHead": "1e3cf27b077caaeb79b09e4c370fec8172692033" | ||
"gitHead": "33d236519f1de4acb9e15989b72fe43cd1b7c6f4" | ||
} |
@@ -259,2 +259,10 @@ ![logo](./rko-logo.svg) | ||
#### `canUndo` | ||
Whether the state can undo, given its undo/redo stack. Readonly. | ||
#### `canRedo` | ||
Whether the state can redo, given its undo/redo stack. Readonly. | ||
## Advanced Usage | ||
@@ -261,0 +269,0 @@ |
import { StateManager } from '../index' | ||
import { del } from 'idb-keyval' | ||
@@ -12,2 +13,3 @@ export interface Todo { | ||
todos: Record<string, Todo> | ||
items: string[] | ||
} | ||
@@ -116,2 +118,14 @@ | ||
} | ||
addItem = () => { | ||
const before = [...this.state.items] | ||
// Bad! Mutation! | ||
this.state.items.push('a') | ||
return this.setState({ | ||
before: { items: before }, | ||
after: { items: this.state.items }, | ||
}) | ||
} | ||
} | ||
@@ -134,2 +148,3 @@ | ||
}, | ||
items: [], | ||
} | ||
@@ -143,3 +158,4 @@ | ||
it('Patches the state', () => { | ||
const todoState = new TodoState(initialState) | ||
del('t0') | ||
const todoState = new TodoState(initialState, 't0') | ||
todoState.patchTodoText('todo0', 'hello world') | ||
@@ -151,4 +167,5 @@ expect(todoState.state.todos.todo0.text).toBe('hello world') | ||
it('Replaces the state', () => { | ||
const todoState = new TodoState(initialState) | ||
it('Replaces the state', (done) => { | ||
del('t0') | ||
const todoState = new TodoState(initialState, 't0') | ||
todoState.loadTodos({ | ||
@@ -169,2 +186,3 @@ todos: { | ||
}, | ||
items: [], | ||
}) | ||
@@ -176,7 +194,13 @@ expect(todoState.state.todos.todo0).toBe(undefined) | ||
// TODO: Confirm that the new state IS NOT persisted | ||
// Does NOT persist state | ||
const todoState2 = new TodoState(initialState, 't0') | ||
setTimeout(() => { | ||
expect(todoState2.state.todos.todo0.isComplete).toBe(false) | ||
done() | ||
}, 100) | ||
}) | ||
it('Does an command', () => { | ||
const todoState = new TodoState(initialState) | ||
it('Does an command', (done) => { | ||
del('t0') | ||
const todoState = new TodoState(initialState, 't0') | ||
expect(todoState.state.todos.todo0.isComplete).toBe(false) | ||
@@ -186,7 +210,13 @@ todoState.toggleTodoComplete('todo0') | ||
// TODO: Confirm that the new state IS persisted | ||
// Persists state | ||
const todoState2 = new TodoState(initialState, 't0') | ||
setTimeout(() => { | ||
expect(todoState2.state.todos.todo0.isComplete).toBe(true) | ||
done() | ||
}, 100) | ||
}) | ||
it('Undoes an command', () => { | ||
const todoState = new TodoState(initialState) | ||
it('Undoes an command', (done) => { | ||
del('t0') | ||
const todoState = new TodoState(initialState, 't0') | ||
todoState.toggleTodoComplete('todo0') | ||
@@ -196,6 +226,13 @@ expect(todoState.state.todos.todo0.isComplete).toBe(true) | ||
expect(todoState.state.todos.todo0.isComplete).toBe(false) | ||
const todoState2 = new TodoState(initialState, 't0') | ||
setTimeout(() => { | ||
expect(todoState2.state.todos.todo0.isComplete).toBe(false) | ||
done() | ||
}, 100) | ||
}) | ||
it('Redoes an command', () => { | ||
const todoState = new TodoState(initialState) | ||
it('Redoes an command', (done) => { | ||
del('t0') | ||
const todoState = new TodoState(initialState, 't0') | ||
todoState.toggleTodoComplete('todo0') | ||
@@ -206,6 +243,13 @@ expect(todoState.state.todos.todo0.isComplete).toBe(true) | ||
expect(todoState.state.todos.todo0.isComplete).toBe(true) | ||
const todoState2 = new TodoState(initialState, 't0') | ||
setTimeout(() => { | ||
expect(todoState2.state.todos.todo0.isComplete).toBe(true) | ||
done() | ||
}, 100) | ||
}) | ||
it('Resets the history', () => { | ||
const todoState = new TodoState(initialState) | ||
it('Resets the history', (done) => { | ||
del('t0') | ||
const todoState = new TodoState(initialState, 't0') | ||
todoState.toggleTodoComplete('todo0') | ||
@@ -216,14 +260,38 @@ expect(todoState.state.todos.todo0.isComplete).toBe(true) | ||
expect(todoState.state.todos.todo0.isComplete).toBe(true) | ||
const todoState2 = new TodoState(initialState, 't0') | ||
setTimeout(() => { | ||
expect(todoState2.state.todos.todo0.isComplete).toBe(true) | ||
done() | ||
}, 100) | ||
}) | ||
it('Resets the state', () => { | ||
const todoState = new TodoState(initialState) | ||
it('Resets the state', (done) => { | ||
del('t0') | ||
const todoState = new TodoState(initialState, 't0') | ||
todoState.toggleTodoComplete('todo0') | ||
expect(todoState.state.todos.todo0.isComplete).toBe(true) | ||
todoState.reset() | ||
expect(todoState.state.todos.todo0.isComplete).toBe(false) | ||
todoState.undo() | ||
expect(todoState.state.todos.todo0.isComplete).toBe(false) | ||
// TODO: Confirm that the new state IS persisted | ||
const todoState1 = new TodoState(initialState, 't0') | ||
todoState1.toggleTodoComplete('todo0') | ||
expect(todoState1.state.todos.todo0.isComplete).toBe(true) | ||
todoState1.reset() | ||
// The state should be reset | ||
expect(todoState1.state.todos.todo0.isComplete).toBe(false) | ||
// Undo and redo should be false | ||
expect(todoState1.canUndo).toBe(false) | ||
expect(todoState1.canRedo).toBe(false) | ||
// The reset should not be undoable | ||
todoState1.undo() | ||
expect(todoState1.state.todos.todo0.isComplete).toBe(false) | ||
// The reset state should be persisted | ||
const todoState2 = new TodoState(initialState, 't0') | ||
setTimeout(() => { | ||
expect(todoState2.state.todos.todo0.isComplete).toBe(false) | ||
done() | ||
}, 100) | ||
}) | ||
@@ -282,2 +350,3 @@ | ||
}, | ||
items: prev.items, | ||
} | ||
@@ -293,2 +362,33 @@ } | ||
}) | ||
it('Correctly sets canUndo', () => { | ||
const state = new TodoState(initialState) | ||
expect(state.canUndo).toBe(false) | ||
state.toggleTodoComplete('todo0') | ||
expect(state.canUndo).toBe(true) | ||
state.undo() | ||
expect(state.canUndo).toBe(false) | ||
state.reset() | ||
expect(state.canUndo).toBe(false) | ||
}) | ||
it('Correctly sets canRedo', () => { | ||
const state = new TodoState(initialState) | ||
expect(state.canRedo).toBe(false) | ||
state.toggleTodoComplete('todo0') | ||
state.undo() | ||
expect(state.canRedo).toBe(true) | ||
state.redo() | ||
expect(state.canRedo).toBe(false) | ||
state.reset() | ||
expect(state.canRedo).toBe(false) | ||
}) | ||
it('Correctly resets, even with mutations.', () => { | ||
const state = new TodoState(initialState) | ||
state.addItem() | ||
expect(state.state.items.length).toBe(1) | ||
state.reset() | ||
expect(state.state.items.length).toBe(0) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
105411
23
904
491