版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // src/normalize.ts
  2. function isObject(v) {
  3. return typeof v === "object" && v !== null;
  4. }
  5. function normalizeOptions(options, factoryOptions) {
  6. options = isObject(options) ? options : /* @__PURE__ */ Object.create(null);
  7. return new Proxy(options, {
  8. get(target, key, receiver) {
  9. if (key === "key")
  10. return Reflect.get(target, key, receiver);
  11. return Reflect.get(target, key, receiver) || Reflect.get(factoryOptions, key, receiver);
  12. }
  13. });
  14. }
  15. // src/pick.ts
  16. function get(state, path) {
  17. return path.reduce((obj, p) => {
  18. return obj == null ? void 0 : obj[p];
  19. }, state);
  20. }
  21. function set(state, path, val) {
  22. return path.slice(0, -1).reduce((obj, p) => {
  23. if (/^(__proto__)$/.test(p))
  24. return {};
  25. else
  26. return obj[p] = obj[p] || {};
  27. }, state)[path[path.length - 1]] = val, state;
  28. }
  29. function pick(baseState, paths) {
  30. return paths.reduce((substate, path) => {
  31. const pathArray = path.split(".");
  32. return set(substate, pathArray, get(baseState, pathArray));
  33. }, {});
  34. }
  35. // src/plugin.ts
  36. function hydrateStore(store, { storage, serializer, key, debug }) {
  37. try {
  38. const fromStorage = storage == null ? void 0 : storage.getItem(key);
  39. if (fromStorage)
  40. store.$patch(serializer == null ? void 0 : serializer.deserialize(fromStorage));
  41. } catch (error) {
  42. if (debug)
  43. console.error(error);
  44. }
  45. }
  46. function persistState(state, { storage, serializer, key, paths, debug }) {
  47. try {
  48. const toStore = Array.isArray(paths) ? pick(state, paths) : state;
  49. storage.setItem(key, serializer.serialize(toStore));
  50. } catch (error) {
  51. if (debug)
  52. console.error(error);
  53. }
  54. }
  55. function createPersistedState(factoryOptions = {}) {
  56. return (context) => {
  57. const { auto = false } = factoryOptions;
  58. const {
  59. options: { persist = auto },
  60. store
  61. } = context;
  62. if (!persist)
  63. return;
  64. const persistences = (Array.isArray(persist) ? persist.map((p) => normalizeOptions(p, factoryOptions)) : [normalizeOptions(persist, factoryOptions)]).map(
  65. ({
  66. storage = localStorage,
  67. beforeRestore = null,
  68. afterRestore = null,
  69. serializer = {
  70. serialize: JSON.stringify,
  71. deserialize: JSON.parse
  72. },
  73. key = store.$id,
  74. paths = null,
  75. debug = false
  76. }) => {
  77. var _a;
  78. return {
  79. storage,
  80. beforeRestore,
  81. afterRestore,
  82. serializer,
  83. key: ((_a = factoryOptions.key) != null ? _a : (k) => k)(key),
  84. paths,
  85. debug
  86. };
  87. }
  88. );
  89. store.$persist = () => {
  90. persistences.forEach((persistence) => {
  91. persistState(store.$state, persistence);
  92. });
  93. };
  94. store.$hydrate = ({ runHooks = true } = {}) => {
  95. persistences.forEach((persistence) => {
  96. const { beforeRestore, afterRestore } = persistence;
  97. if (runHooks)
  98. beforeRestore == null ? void 0 : beforeRestore(context);
  99. hydrateStore(store, persistence);
  100. if (runHooks)
  101. afterRestore == null ? void 0 : afterRestore(context);
  102. });
  103. };
  104. persistences.forEach((persistence) => {
  105. const { beforeRestore, afterRestore } = persistence;
  106. beforeRestore == null ? void 0 : beforeRestore(context);
  107. hydrateStore(store, persistence);
  108. afterRestore == null ? void 0 : afterRestore(context);
  109. store.$subscribe(
  110. (_mutation, state) => {
  111. persistState(state, persistence);
  112. },
  113. {
  114. detached: true
  115. }
  116. );
  117. });
  118. };
  119. }
  120. // src/index.ts
  121. var src_default = createPersistedState();
  122. export {
  123. createPersistedState,
  124. src_default as default
  125. };