pinia-plugin-persist.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import "./chunk-HM4MQYWN.js";
  2. // node_modules/pinia-plugin-persist/dist/pinia-persist.es.js
  3. var updateStorage = (strategy, store) => {
  4. const storage = strategy.storage || sessionStorage;
  5. const storeKey = strategy.key || store.$id;
  6. if (strategy.paths) {
  7. const partialState = strategy.paths.reduce((finalObj, key) => {
  8. finalObj[key] = store.$state[key];
  9. return finalObj;
  10. }, {});
  11. storage.setItem(storeKey, JSON.stringify(partialState));
  12. } else {
  13. storage.setItem(storeKey, JSON.stringify(store.$state));
  14. }
  15. };
  16. var index = ({ options, store }) => {
  17. var _a, _b, _c, _d;
  18. if ((_a = options.persist) == null ? void 0 : _a.enabled) {
  19. const defaultStrat = [{
  20. key: store.$id,
  21. storage: sessionStorage
  22. }];
  23. const strategies = ((_c = (_b = options.persist) == null ? void 0 : _b.strategies) == null ? void 0 : _c.length) ? (_d = options.persist) == null ? void 0 : _d.strategies : defaultStrat;
  24. strategies.forEach((strategy) => {
  25. const storage = strategy.storage || sessionStorage;
  26. const storeKey = strategy.key || store.$id;
  27. const storageResult = storage.getItem(storeKey);
  28. if (storageResult) {
  29. store.$patch(JSON.parse(storageResult));
  30. updateStorage(strategy, store);
  31. }
  32. });
  33. store.$subscribe(() => {
  34. strategies.forEach((strategy) => {
  35. updateStorage(strategy, store);
  36. });
  37. });
  38. }
  39. };
  40. export {
  41. index as default,
  42. updateStorage
  43. };
  44. //# sourceMappingURL=pinia-plugin-persist.js.map