pinia-plugin-persist-state 0.1.2 → 0.1.3-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- import { Store as f, isEmpty as u, CustomConsole as _, PluginSubscriber as w } from "pinia-plugin-subscription";
2
- import { toRaw as P, nextTick as v } from "vue";
3
- class E {
1
+ import { Store as f, traceGroups as p, isEmpty as y, PluginSubscriber as _ } from "pinia-plugin-subscription/helpers";
2
+ import { toRaw as b, nextTick as w } from "vue";
3
+ class P {
4
4
  _storage;
5
5
  constructor(t) {
6
6
  this._storage = t === "localStorage" ? localStorage : sessionStorage;
@@ -27,7 +27,7 @@ class E {
27
27
  this._storage.setItem(e, JSON.stringify(t));
28
28
  }
29
29
  }
30
- let l = class {
30
+ let g = class {
31
31
  constructor(t, e, s = {}) {
32
32
  this._databaseName = t, this._objectStoreName = e, this._objectStoreOptions = s;
33
33
  }
@@ -91,7 +91,7 @@ let l = class {
91
91
  });
92
92
  }
93
93
  };
94
- class m {
94
+ class l {
95
95
  _db;
96
96
  _db_options;
97
97
  get dbName() {
@@ -104,7 +104,7 @@ class m {
104
104
  }
105
105
  defineDb() {
106
106
  const { databaseName: t, keyPath: e, name: s, objectStoreName: r, storage: a } = this._db_options, o = a ?? (s === "localStorage" || s === "sessionStorage" ? s : "indexedDB");
107
- return o === "localStorage" || o === "sessionStorage" ? new E(o) : new l(t ?? s, r ?? "persistedStore", { keyPath: e });
107
+ return o === "localStorage" || o === "sessionStorage" ? new P(o) : new g(t ?? s, r ?? "persistedStore", { keyPath: e });
108
108
  }
109
109
  async getItem(t) {
110
110
  return this._db.getItem(t);
@@ -113,10 +113,10 @@ class m {
113
113
  await this._db.removeItem(t);
114
114
  }
115
115
  async setItem(t, e) {
116
- this._db instanceof l ? await this._db.setItem({ storeName: t, ...e }) : await this._db.setItem(e, t);
116
+ this._db instanceof g ? await this._db.setItem({ storeName: t, ...e }) : await this._db.setItem(e, t);
117
117
  }
118
118
  }
119
- class T {
119
+ class v {
120
120
  _store = /* @__PURE__ */ new Map();
121
121
  async clear() {
122
122
  this._store.clear();
@@ -141,25 +141,25 @@ class T {
141
141
  this._store.set(e, t);
142
142
  }
143
143
  }
144
- let p = {};
145
- const N = {
144
+ let u = {};
145
+ const E = {
146
146
  clear: async () => {
147
- p = {};
147
+ u = {};
148
148
  },
149
- getItem: async (i) => new Promise((t) => t(p[i])),
149
+ getItem: async (i) => new Promise((t) => t(u[i])),
150
150
  removeItem: async (i) => {
151
- delete p[i];
151
+ delete u[i];
152
152
  },
153
153
  removeItems: async (i) => {
154
- i ? i.forEach((t) => delete p[t]) : p = {};
154
+ i ? i.forEach((t) => delete u[t]) : u = {};
155
155
  },
156
156
  setItem: async (i, t) => {
157
- p[t] = i;
157
+ u[t] = i;
158
158
  }
159
159
  };
160
- class I extends m {
160
+ class T extends l {
161
161
  defineDb() {
162
- return this.dbName === "localStorage" || this.dbName === "sessionStorage" ? N : new T();
162
+ return this.dbName === "localStorage" || this.dbName === "sessionStorage" ? E : new v();
163
163
  }
164
164
  }
165
165
  class h {
@@ -245,7 +245,7 @@ class h {
245
245
  return t.buffer.slice(t.byteOffset, t.byteOffset + t.byteLength);
246
246
  }
247
247
  }
248
- const O = [
248
+ const N = [
249
249
  "@context",
250
250
  "activeLink",
251
251
  "computed",
@@ -256,8 +256,8 @@ const O = [
256
256
  "isLoading",
257
257
  "subs",
258
258
  "version"
259
- ], M = 200;
260
- class g extends f {
259
+ ], I = 200;
260
+ class S extends f {
261
261
  _className = "StorePersister";
262
262
  _crypt;
263
263
  _excludedKeys;
@@ -265,46 +265,48 @@ class g extends f {
265
265
  _propertiesToEncrypt;
266
266
  _persistenceVersion = 0;
267
267
  _persistTimer;
268
+ _resolvePendingPersist;
268
269
  _writeQueue = Promise.resolve();
269
270
  _watchedStore;
270
- constructor(t, e, s, r) {
271
- super(t, e, s, r);
272
- const { crypt: a, persister: o, watchedStore: n } = e;
273
- this._excludedKeys = this.initExcludedKeys(), this.definePersister(o), this._propertiesToEncrypt = new Set(this.getPropertiesToEncrypt()), this._watchedStore = n, a && (this._crypt = a, this.options && (this.options.isEncrypted = !1)), this.augmentStore(), this.debugLog("StorePersister constructor", {
271
+ constructor(t, e) {
272
+ super(t, e);
273
+ const { crypt: s, persister: r, watchedStore: a } = e;
274
+ this._excludedKeys = this.initExcludedKeys(), this.definePersister(r), this._propertiesToEncrypt = new Set(this.getPropertiesToEncrypt()), this._watchedStore = a, s && (this._crypt = s, this.options && (this.options.isEncrypted = !1)), this.augmentStore(), this.trace("constructor", t.$id, () => ({
274
275
  toBeWatched: this.toBeWatched(),
275
- watchedStore: n,
276
- persister: o,
277
- store: t,
278
- options: e
279
- }), this.toBeWatched() && (this._watchedStore.add(this.store.$id), this.storeSubscribe = this.storeSubscription.bind(this));
276
+ watchedStore: a
277
+ //persister
278
+ })), this.toBeWatched() && (this._watchedStore.add(this.store.$id), this.storeSubscribe = this.storeSubscription.bind(this));
280
279
  }
281
280
  augmentStore() {
282
281
  const { isEncrypted: t, persistedPropertiesToEncrypt: e, watchMutation: s } = this.options;
283
282
  t === void 0 && (this.options.isEncrypted = !1), e === void 0 && (this.options.persistedPropertiesToEncrypt = []), s === void 0 && (this.options.watchMutation = !1), this.stateHas("isLoading") || this.addToState("isLoading", !1), this.store.persistState = async () => await this.persist(), this.store.flushPersistedState = async () => await this.flushPersistedState(), this.store.remember = async () => await this.remember(), this.store.removePersistedState = this.removePersistedState.bind(this), this.store.watch = this.watch.bind(this), this.store.stopWatch = this.stopWatch.bind(this), this.store.hydrate = this.hydrate.bind(this);
284
283
  }
285
284
  cancelScheduledPersist() {
286
- this._persistTimer && (clearTimeout(this._persistTimer), this._persistTimer = void 0);
285
+ this._persistTimer && (clearTimeout(this._persistTimer), this._persistTimer = void 0), this._resolvePendingPersist?.(), this._resolvePendingPersist = void 0;
287
286
  }
288
287
  async cryptState(t, e = !1) {
289
288
  return await new Promise(async (s) => {
290
289
  const r = this._crypt, a = this.getPropertiesToEncrypt(), o = this.isEncrypted();
291
- if (this.debugLog(`cryptState - ${this.store.$id} ${e ? "decrypt" : "crypt"}`, {
290
+ if (this.trace(`cryptState:${e ? "decrypt" : "crypt"}`, this.store.$id, () => ({
292
291
  can: this._propertiesToEncrypt.size > 0 && o === e && !!r,
293
- Crypt: r,
294
- state: t
295
- }), this._propertiesToEncrypt.size > 0 && r) {
292
+ //Crypt,
293
+ groupId: p.onMutation(this.store.$id)
294
+ })), this._propertiesToEncrypt.size > 0 && r) {
296
295
  const n = {};
297
296
  for (const c of a) {
298
297
  const d = this.getValue(t[c]);
299
298
  d && (n[c] = e ? await this.decryptProperty(r, d) : await this.encryptProperty(r, d));
300
299
  }
301
- this.debugLog(`cryptState - ${this.store.$id}`, { encryptedState: n }), u(n) || (t = { ...t, ...n }, this.options && (this.options.isEncrypted = !e));
300
+ this.trace("cryptState:result", this.store.$id, () => ({
301
+ groupId: p.onMutation(this.store.$id)
302
+ //encryptedState
303
+ })), y(n) || (t = { ...t, ...n }, this.options && (this.options.isEncrypted = !e));
302
304
  }
303
305
  s(t);
304
306
  });
305
307
  }
306
- static customizeStore(t, e, s, r) {
307
- return !e.storeOptions?.persist && e.storeOptions?.watchMutation && (e.storeOptions.persist = !0), super.customizeStore(t, e, s, r);
308
+ static customizeStore(t, e) {
309
+ return !e.storeOptions?.persist && e.storeOptions?.watchMutation && (e.storeOptions.persist = !0), super.customizeStore(t, e);
308
310
  }
309
311
  async decryptProperty(t, e) {
310
312
  return t.decrypt(e);
@@ -316,7 +318,7 @@ class g extends f {
316
318
  return;
317
319
  }
318
320
  const s = e.storageOptions;
319
- this._persister = new m({
321
+ this._persister = new l({
320
322
  name: s?.databaseName ?? e.storage,
321
323
  storage: e.storage,
322
324
  databaseName: s?.databaseName,
@@ -355,9 +357,16 @@ class g extends f {
355
357
  return;
356
358
  s = r.state;
357
359
  }
358
- return t && this.toBeCrypted() && s && (await this._crypt?.init(), s = await this.cryptState(s, !0)), this.debugLog(`getPersistedState ${e}`, { persistedState: s, state: this.state }), s;
360
+ return t && this.toBeCrypted() && s && (await this._crypt?.init(), s = await this.cryptState(s, !0)), this.trace("getPersistedState", this.store.$id, () => ({
361
+ groupId: p.onMutation(this.store.$id)
362
+ //persistenceKey,
363
+ //persistedState
364
+ })), s;
359
365
  } catch (s) {
360
- this.logError("getPersistedState()", { persistenceKey: e, e: s });
366
+ this.traceError("getPersistedState", s, this.store.$id, () => ({
367
+ groupId: p.onMutation(this.store.$id),
368
+ persistenceKey: e
369
+ }));
361
370
  }
362
371
  }
363
372
  async getStateToPersist() {
@@ -367,7 +376,7 @@ class g extends f {
367
376
  for (const o of Object.keys(e))
368
377
  if (!this.hasDeniedFirstChar(o[0]) && !t.has(o)) {
369
378
  const n = e[o];
370
- u(n) || (s && this._propertiesToEncrypt.has(o) ? a[o] = await this.encryptProperty(r, n) : a[o] = P(n));
379
+ y(n) || (s && this._propertiesToEncrypt.has(o) ? a[o] = await this.encryptProperty(r, n) : a[o] = b(n));
371
380
  }
372
381
  return this.transformState(a);
373
382
  }
@@ -375,7 +384,7 @@ class g extends f {
375
384
  return this.options.watchMutation;
376
385
  }
377
386
  getDebounceMs() {
378
- return Math.max(0, this.options.debounceMs ?? M);
387
+ return Math.max(0, this.options.debounceMs ?? I);
379
388
  }
380
389
  static hasRequiredKeys(t) {
381
390
  return !!t?.persist || !!t?.watchMutation;
@@ -385,7 +394,7 @@ class g extends f {
385
394
  }
386
395
  initExcludedKeys() {
387
396
  return /* @__PURE__ */ new Set([
388
- ...O,
397
+ ...N,
389
398
  ...this.options.excludedKeys ?? []
390
399
  ]);
391
400
  }
@@ -394,7 +403,9 @@ class g extends f {
394
403
  }
395
404
  async persist() {
396
405
  const t = ++this._persistenceVersion, e = await this.getStateToPersist();
397
- if (this.debugLog(`persist() ${this.store.$id} - stateToPersist:`, { state: e }), !u(e)) {
406
+ if (this.trace("persist", this.store.$id, () => ({
407
+ groupId: p.onMutation(this.store.$id)
408
+ })), !y(e)) {
398
409
  const s = this.getCacheOptions(), r = s ? { state: e, metadata: { cachedAt: Date.now(), version: s.version } } : e, a = this._writeQueue.then(async () => {
399
410
  t === this._persistenceVersion && await this._persister.setItem(this.getPersistenceKey(), r);
400
411
  });
@@ -406,9 +417,9 @@ class g extends f {
406
417
  this.state.isLoading = !0;
407
418
  try {
408
419
  let t = await this.getPersistedState();
409
- t && !u(t) && this.store.$patch(t);
420
+ t && !y(t) && this.store.$patch(t);
410
421
  } catch (t) {
411
- this.logError("remember()", { error: t, storeName: this.store.$id });
422
+ this.traceError("remember", t, this.store.$id);
412
423
  } finally {
413
424
  this.state.isLoading = !1;
414
425
  }
@@ -422,9 +433,20 @@ class g extends f {
422
433
  }), await t;
423
434
  }
424
435
  schedulePersist(t) {
425
- this.cancelScheduledPersist(), this._persistTimer = setTimeout(() => {
426
- this._persistTimer = void 0, this.persist().then(() => this.store.mutationCallback?.(this.state, t)).catch((e) => this.logError("persist()", { error: e, storeName: this.store.$id }));
427
- }, this.getDebounceMs());
436
+ return this.cancelScheduledPersist(), new Promise((e) => {
437
+ this._resolvePendingPersist = e, this._persistTimer = setTimeout(async () => {
438
+ this._persistTimer = void 0, this._resolvePendingPersist = void 0;
439
+ try {
440
+ await this.persist(), this.store.mutationCallback?.(this.state, t);
441
+ } catch (s) {
442
+ this.traceError("persist", s, this.store.$id, () => ({
443
+ groupId: p.onMutation(this.store.$id)
444
+ }));
445
+ } finally {
446
+ e();
447
+ }
448
+ }, this.getDebounceMs());
449
+ });
428
450
  }
429
451
  async shouldIgnoreCachedState(t, e) {
430
452
  const s = this.getCacheOptions(), r = s.maxAge !== void 0 && t.metadata.cachedAt + s.maxAge < Date.now(), a = s.version !== void 0 && s.version !== t.metadata.version, o = r ? s.onExpired : a ? s.onVersionMismatch : void 0;
@@ -436,19 +458,12 @@ class g extends f {
436
458
  stopWatch() {
437
459
  this.options?.watchMutation && (this.options && (this.options.watchMutation = !1), this._watchedStore.delete(this.store.$id));
438
460
  }
439
- storeSubscription(t) {
440
- this.debugLog(`store.storeSubscription() ${this.store.$id}`, [
441
- "mutation type !== patch object: ",
442
- t.type !== "patch object",
443
- "watchMutation: ",
444
- this.getWatchMutation(),
445
- "mutation:",
446
- t,
447
- "state:",
448
- this.state,
449
- "store:",
450
- this.store
451
- ]), t.type !== "patch object" && this.getWatchMutation() && this.schedulePersist(t);
461
+ async storeSubscription(t) {
462
+ this.trace("storeSubscription", this.store.$id, () => ({
463
+ groupId: p.onMutation(this.store.$id),
464
+ isNotPatchObject: t.type !== "patch object",
465
+ watchMutation: this.getWatchMutation()
466
+ })), t.type !== "patch object" && this.getWatchMutation() && await this.schedulePersist(t);
452
467
  }
453
468
  toBeCrypted() {
454
469
  return !!(this._crypt && this.getPropertiesToEncrypt());
@@ -463,39 +478,34 @@ class g extends f {
463
478
  this.toBeWatched() && (this.options && (this.options.watchMutation = !0), this.storeSubscribe = this.storeSubscription.bind(this));
464
479
  }
465
480
  }
466
- const b = "pinia-plugin-persist-state";
467
- class C extends _ {
468
- _pluginName = b;
469
- }
470
- const S = new C();
471
- class x extends w {
481
+ const O = "pinia-plugin-persist-state";
482
+ class M extends _ {
472
483
  _resetStoreCallback = (t) => {
473
484
  const { removePersistedState: e } = t;
474
485
  typeof e == "function" && e();
475
486
  };
476
487
  constructor() {
477
488
  super(
478
- b,
479
- g.customizeStore.bind(g),
480
- S
489
+ O,
490
+ S.customizeStore.bind(S)
481
491
  ), this.execution = {
482
492
  environment: "client",
483
493
  hydration: "immediate"
484
494
  }, this.hydrationScheduler = (t) => {
485
- v(() => t());
495
+ w(() => t());
486
496
  };
487
497
  }
488
498
  hydrate() {
489
499
  typeof window > "u" || this.hydrationScheduler?.(() => {
490
500
  const t = this.storeInstance?.hydrate?.();
491
- t && typeof t.catch == "function" && t.catch((e) => S.error("PersistPiniaState hydration error", e));
501
+ t && typeof t.catch == "function" && t.catch((e) => console.error("PersistPiniaState hydration error", e));
492
502
  });
493
503
  }
494
504
  }
495
- const y = new x(), A = ["localStorage", "sessionStorage"];
496
- function L(i, t) {
505
+ const m = new M(), A = ["localStorage", "sessionStorage"];
506
+ function j(i, t) {
497
507
  const e = K(i, t);
498
- return y.pluginOptions = D(e), y;
508
+ return m.pluginOptions = x(e), m;
499
509
  }
500
510
  function K(i, t) {
501
511
  return typeof i != "string" ? i : A.includes(i) ? { storage: i, cryptKey: t } : {
@@ -505,12 +515,12 @@ function K(i, t) {
505
515
  cryptKey: t
506
516
  };
507
517
  }
508
- function D(i) {
518
+ function x(i) {
509
519
  let t, e;
510
520
  const s = /* @__PURE__ */ new Set();
511
521
  try {
512
522
  if (typeof window < "u")
513
- return i && !u(i.storage) && (t = new m({
523
+ return i && !y(i.storage) && (t = new l({
514
524
  name: i.databaseName ?? i.storage,
515
525
  storage: i.storage,
516
526
  databaseName: i.databaseName,
@@ -523,15 +533,15 @@ function D(i) {
523
533
  }
524
534
  function $(i, t) {
525
535
  let e;
526
- return t && (e = new h(t)), y.pluginOptions = {
527
- persister: new I({ name: i }),
536
+ return t && (e = new h(t)), m.pluginOptions = {
537
+ persister: new T({ name: i }),
528
538
  crypt: e,
529
539
  watchedStore: /* @__PURE__ */ new Set()
530
- }, y;
540
+ }, m;
531
541
  }
532
542
  export {
533
- b as PLUGIN_NAME,
534
- I as PersisterMock,
535
- L as createPersistStatePlugin,
543
+ O as PLUGIN_NAME,
544
+ T as PersisterMock,
545
+ j as createPersistStatePlugin,
536
546
  $ as createPluginMock
537
547
  };
@@ -1,4 +1,4 @@
1
- import type { PluginSubscriberInterface } from "pinia-plugin-subscription";
1
+ import type { PluginSubscriberInterface } from "pinia-plugin-subscription/types";
2
2
  import type { PersistStatePluginOptions } from "./store";
3
3
  export type { PersistedCacheOptions, PersistedStore, PersistedStoreOptions, PersistedStateTransformer, PersistStatePluginOptions, PersistStorage, PluginPersistedStoreOptions, } from "./store";
4
4
  export declare const PLUGIN_NAME: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pinia-plugin-persist-state",
3
3
  "description": "Pinia plugin for persisting store state",
4
- "version": "0.1.2",
4
+ "version": "0.1.3-beta.0",
5
5
  "type": "module",
6
6
  "author": "Alexandre Bidaud <alexbidaud85@gmail.com> (https://github.com/AlxBDo)",
7
7
  "license": "MIT",
@@ -42,7 +42,7 @@
42
42
  "coverage": "vitest run --coverage"
43
43
  },
44
44
  "dependencies": {
45
- "pinia-plugin-subscription": "^0.1.7"
45
+ "pinia-plugin-subscription": "^1.0.0-beta.9"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "pinia": "^2.0.0 || ^3.0.0",
@@ -56,6 +56,7 @@
56
56
  "@vue/tsconfig": "^0.8.1",
57
57
  "fake-indexeddb": "^6.2.5",
58
58
  "pinia": "^3.0.2",
59
+ "pinia-plugin-extending-store": "^0.1.6-beta.4",
59
60
  "typescript": "~5.9.3",
60
61
  "vite": "^7.2.4",
61
62
  "vitest": "^4.0.16",