pinia-plugin-persist-uni.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import "./chunk-VX4GZB4L.js";
  2. // C:/Users/Administrator/Desktop/yudao的商城接口修改/商城接口修改/uni3/node_modules/pinia-plugin-persist-uni/dist/pinia-persist-uni.es.js
  3. var isH5 = typeof alert === "function";
  4. var updateStorage = (strategy, store, options) => {
  5. const storage = strategy.storage;
  6. const storeKey = strategy.key || store.$id;
  7. const isCustomStorage = isH5 || (options == null ? void 0 : options.enforceCustomStorage);
  8. if (strategy.paths) {
  9. const partialState = strategy.paths.reduce((finalObj, key) => {
  10. finalObj[key] = store.$state[key];
  11. return finalObj;
  12. }, {});
  13. if (isCustomStorage && storage) {
  14. storage.setItem(storeKey, JSON.stringify(partialState));
  15. } else {
  16. uni.setStorage({ key: storeKey, data: JSON.stringify(partialState) });
  17. }
  18. } else if (isCustomStorage && storage) {
  19. storage.setItem(storeKey, JSON.stringify(store.$state));
  20. } else {
  21. uni.setStorage({ key: storeKey, data: JSON.stringify(store.$state) });
  22. }
  23. };
  24. var index = ({ options, store }) => {
  25. var _a, _b, _c, _d, _e, _f;
  26. if ((_a = options.persist) == null ? void 0 : _a.enabled) {
  27. const defaultStrat = [
  28. {
  29. key: store.$id,
  30. storage: ((_b = options.persist) == null ? void 0 : _b.H5Storage) || (window == null ? void 0 : window.sessionStorage)
  31. }
  32. ];
  33. const strategies = ((_d = (_c = options.persist) == null ? void 0 : _c.strategies) == null ? void 0 : _d.length) ? (_e = options.persist) == null ? void 0 : _e.strategies : defaultStrat;
  34. strategies.forEach((strategy) => {
  35. var _a2, _b2;
  36. const storage = strategy.storage || ((_a2 = options.persist) == null ? void 0 : _a2.H5Storage) || (window == null ? void 0 : window.sessionStorage);
  37. const storeKey = strategy.key || store.$id;
  38. let storageResult;
  39. if (isH5 || ((_b2 = options.persist) == null ? void 0 : _b2.enforceCustomStorage)) {
  40. storageResult = storage.getItem(storeKey);
  41. } else {
  42. storageResult = uni.getStorageSync(storeKey);
  43. }
  44. if (storageResult) {
  45. store.$patch(JSON.parse(storageResult));
  46. updateStorage(strategy, store, options.persist);
  47. }
  48. });
  49. store.$subscribe(() => {
  50. strategies.forEach((strategy) => {
  51. updateStorage(strategy, store, options.persist);
  52. });
  53. }, { detached: ((_f = options.persist) == null ? void 0 : _f.detached) ? true : false });
  54. }
  55. };
  56. export {
  57. index as default
  58. };
  59. //# sourceMappingURL=pinia-plugin-persist-uni.js.map