pinia-plugin-persist-uni
Advanced tools
Comparing version 1.1.5 to 1.2.0
@@ -10,2 +10,3 @@ import { PiniaPluginContext } from 'pinia'; | ||
detached?: true; | ||
enforceCustomStorage?: boolean; | ||
H5Storage?: Storage; | ||
@@ -12,0 +13,0 @@ strategies?: PersistStrategy[]; |
const isH5 = typeof alert === "function"; | ||
const updateStorage = (strategy, store) => { | ||
const updateStorage = (strategy, store, options) => { | ||
const storage = strategy.storage; | ||
const storeKey = strategy.key || store.$id; | ||
const isCustomStorage = isH5 || (options == null ? void 0 : options.enforceCustomStorage); | ||
if (strategy.paths) { | ||
@@ -10,3 +11,3 @@ const partialState = strategy.paths.reduce((finalObj, key) => { | ||
}, {}); | ||
if (isH5 && storage) { | ||
if (isCustomStorage && storage) { | ||
storage.setItem(storeKey, JSON.stringify(partialState)); | ||
@@ -16,3 +17,3 @@ } else { | ||
} | ||
} else if (isH5 && storage) { | ||
} else if (isCustomStorage && storage) { | ||
storage.setItem(storeKey, JSON.stringify(store.$state)); | ||
@@ -34,7 +35,7 @@ } else { | ||
strategies.forEach((strategy) => { | ||
var _a2; | ||
var _a2, _b2; | ||
const storage = strategy.storage || ((_a2 = options.persist) == null ? void 0 : _a2.H5Storage) || (window == null ? void 0 : window.sessionStorage); | ||
const storeKey = strategy.key || store.$id; | ||
let storageResult; | ||
if (isH5) { | ||
if (isH5 || ((_b2 = options.persist) == null ? void 0 : _b2.enforceCustomStorage)) { | ||
storageResult = storage.getItem(storeKey); | ||
@@ -46,3 +47,3 @@ } else { | ||
store.$patch(JSON.parse(storageResult)); | ||
updateStorage(strategy, store); | ||
updateStorage(strategy, store, options.persist); | ||
} | ||
@@ -52,3 +53,3 @@ }); | ||
strategies.forEach((strategy) => { | ||
updateStorage(strategy, store); | ||
updateStorage(strategy, store, options.persist); | ||
}); | ||
@@ -55,0 +56,0 @@ }, { detached: ((_f = options.persist) == null ? void 0 : _f.detached) ? true : false }); |
@@ -1,1 +0,1 @@ | ||
(function(s,n){typeof exports=="object"&&typeof module!="undefined"?module.exports=n():typeof define=="function"&&define.amd?define(n):(s=typeof globalThis!="undefined"?globalThis:s||self,s.piniaPersist=n())})(this,function(){"use strict";const s=typeof alert=="function",n=(e,t)=>{const i=e.storage,a=e.key||t.$id;if(e.paths){const r=e.paths.reduce((d,f)=>(d[f]=t.$state[f],d),{});s&&i?i.setItem(a,JSON.stringify(r)):uni.setStorage({key:a,data:JSON.stringify(r)})}else s&&i?i.setItem(a,JSON.stringify(t.$state)):uni.setStorage({key:a,data:JSON.stringify(t.$state)})};var l=({options:e,store:t})=>{var i,a,r,d,f,u;if((i=e.persist)!=null&&i.enabled){const h=[{key:t.$id,storage:((a=e.persist)==null?void 0:a.H5Storage)||(window==null?void 0:window.sessionStorage)}],g=(d=(r=e.persist)==null?void 0:r.strategies)!=null&&d.length?(f=e.persist)==null?void 0:f.strategies:h;g.forEach(o=>{var p;const y=o.storage||((p=e.persist)==null?void 0:p.H5Storage)||(window==null?void 0:window.sessionStorage),S=o.key||t.$id;let c;s?c=y.getItem(S):c=uni.getStorageSync(S),c&&(t.$patch(JSON.parse(c)),n(o,t))}),t.$subscribe(()=>{g.forEach(o=>{n(o,t)})},{detached:!!((u=e.persist)!=null&&u.detached)})}};return l}); | ||
(function(r,a){typeof exports=="object"&&typeof module!="undefined"?module.exports=a():typeof define=="function"&&define.amd?define(a):(r=typeof globalThis!="undefined"?globalThis:r||self,r.piniaPersist=a())})(this,function(){"use strict";const r=typeof alert=="function",a=(e,t,n)=>{const s=e.storage,i=e.key||t.$id,f=r||(n==null?void 0:n.enforceCustomStorage);if(e.paths){const d=e.paths.reduce((c,g)=>(c[g]=t.$state[g],c),{});f&&s?s.setItem(i,JSON.stringify(d)):uni.setStorage({key:i,data:JSON.stringify(d)})}else f&&s?s.setItem(i,JSON.stringify(t.$state)):uni.setStorage({key:i,data:JSON.stringify(t.$state)})};var m=({options:e,store:t})=>{var n,s,i,f,d,c;if((n=e.persist)!=null&&n.enabled){const g=[{key:t.$id,storage:((s=e.persist)==null?void 0:s.H5Storage)||(window==null?void 0:window.sessionStorage)}],S=(f=(i=e.persist)==null?void 0:i.strategies)!=null&&f.length?(d=e.persist)==null?void 0:d.strategies:g;S.forEach(u=>{var h,p;const y=u.storage||((h=e.persist)==null?void 0:h.H5Storage)||(window==null?void 0:window.sessionStorage),l=u.key||t.$id;let o;r||((p=e.persist)==null?void 0:p.enforceCustomStorage)?o=y.getItem(l):o=uni.getStorageSync(l),o&&(t.$patch(JSON.parse(o)),a(u,t,e.persist))}),t.$subscribe(()=>{S.forEach(u=>{a(u,t,e.persist)})},{detached:!!((c=e.persist)!=null&&c.detached)})}};return m}); |
{ | ||
"name": "pinia-plugin-persist-uni", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
10386
80