UNPKG

pinia-persist

Version:

Persist and rehydrate your Pinia state between page reloads

2 lines (1 loc) 1.19 kB
function r(r,t,e,i){return!/^(__proto__|constructor|prototype)$/.test(t)&&((t=t.split?t.split("."):t.slice(0)).slice(0,-1).reduce((function(r,t){return r[t]=r[t]||{}}),r)[t.pop()]=e),r}function t(r,t,e,i){var n,o;return null!==(o=null!==(n=t[e])&&void 0!==n?n:r[e])&&void 0!==o?o:i}var e=function(e){var i=window&&window.localStorage,n=null!=e?e:{};return function(e){var o,u,s,l=null!==(u=null===(o=e.options)||void 0===o?void 0:o.persist)&&void 0!==u?u:{},c=null!==(s=l.key)&&void 0!==s?s:e.store.$id,a=t(n,l,"overwrite",!1),d=t(n,l,"storage",i),f=t(n,l,"filter",(function(){return!0})),v=t(n,l,"serialization",JSON.stringify),p=t(n,l,"deserialization",JSON.parse);try{var y=d.getItem(c);if(null!==y){var h=p(y);a?e.store.$state=h:e.store.$patch(h)}}catch(r){}e.store.$subscribe((function(t,e){if(!1!==f(t,e)){Array.isArray(l.includePaths)?e=l.includePaths.reduce((function(t,i){return r(t,i,function(r,t,e){return void 0===(r=(t.split?t.split("."):t).reduce((function(r,t){return r&&r[t]}),r))?e:r}(e,i))}),{}):Array.isArray(l.excludePaths)&&(e=p(v(e)),l.excludePaths.forEach((function(t){return r(e,t,void 0)}),{}));var i=v(e);d.setItem(c,i)}}))}};export{e as createPersistPlugin};