UNPKG

@reffect/localstore

Version:

localStorage middleware for Reffect

3 lines (2 loc) 501 B
import{manage as e}from"@reffect/core";const t=(t,o)=>{const r=e(t);if("unknown"===r.name)throw"store should have unique name to use localstore middleware";const n=`@reffect/store/${r.name}`,s=localStorage.getItem(n);if(null!==s){const e=JSON.parse(s);r.initialState=o(e),Object.assign(t,o(e))}let a=null;return r.subscribe((e,t,o)=>{clearTimeout(a),a=setTimeout(()=>{localStorage.setItem(n,JSON.stringify({...o}))},144)}),t};export{t as localstore}; //# sourceMappingURL=reffect-localstore.es.js.map