pinia-plugin-subscription 0.2.0 → 1.0.0-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,179 +1,177 @@
1
- var g = Object.defineProperty;
2
- var E = (n, s, t) => s in n ? g(n, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[s] = t;
3
- var a = (n, s, t) => E(n, typeof s != "symbol" ? s + "" : s, t);
4
- import { h as O, g as f, D as m, i as y, P as C } from "./chunks/index-VL_PgzVK.js";
5
- import { C as T, p as W, a as L, S as I, d as q, b as G, c as U, e as z, f as B, s as J } from "./chunks/index-VL_PgzVK.js";
6
- import { toRaw as _ } from "vue";
7
- function u(n) {
1
+ var k = Object.defineProperty;
2
+ var E = (r, t, e) => t in r ? k(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
+ var c = (r, t, e) => E(r, typeof t != "symbol" ? t + "" : t, e);
4
+ import { h as O, g as b, i as _ } from "./chunks/constantes-BsD73jkk.js";
5
+ import { p as K, P as M, S as x, d as F, a as W, b as I, c as q, e as z, s as G } from "./chunks/constantes-BsD73jkk.js";
6
+ import { toRaw as y } from "vue";
7
+ function h(r) {
8
8
  if (typeof structuredClone == "function")
9
9
  try {
10
- return structuredClone(n);
10
+ return structuredClone(r);
11
11
  } catch {
12
12
  }
13
- if (n === null || typeof n != "object")
14
- return n;
15
- if (n instanceof Map)
13
+ if (r === null || typeof r != "object")
14
+ return r;
15
+ if (r instanceof Map)
16
16
  return new Map(
17
- Array.from(n.entries(), ([t, e]) => [u(t), u(e)])
17
+ Array.from(r.entries(), ([e, i]) => [h(e), h(i)])
18
18
  );
19
- if (n instanceof Set)
20
- return new Set(Array.from(n.values(), (t) => u(t)));
21
- if (Array.isArray(n))
22
- return n.map((t) => u(t));
23
- if (Object.getPrototypeOf(n) === Object.prototype || Object.getPrototypeOf(n) === null)
19
+ if (r instanceof Set)
20
+ return new Set(Array.from(r.values(), (e) => h(e)));
21
+ if (Array.isArray(r))
22
+ return r.map((e) => h(e));
23
+ if (Object.getPrototypeOf(r) === Object.prototype || Object.getPrototypeOf(r) === null)
24
24
  return Object.fromEntries(
25
- Object.entries(n).map(([t, e]) => [t, u(e)])
25
+ Object.entries(r).map(([e, i]) => [e, h(i)])
26
26
  );
27
- const s = Object.create(Object.getPrototypeOf(n));
28
- for (const [t, e] of Object.entries(n))
29
- s[t] = u(e);
30
- return s;
27
+ const t = Object.create(Object.getPrototypeOf(r));
28
+ for (const [e, i] of Object.entries(r))
29
+ t[e] = h(i);
30
+ return t;
31
31
  }
32
- function $(n, s = "all") {
33
- if (Array.isArray(s)) {
34
- const t = {};
35
- for (const e of s)
36
- e in n && (t[e] = u(_(n[e])));
37
- return t;
38
- }
39
- if (s !== "all")
40
- throw new Error(`Invalid keys parameter: ${s}`);
41
- return u(_(n));
32
+ function g(r, t = "all") {
33
+ if (Array.isArray(t)) {
34
+ const e = {};
35
+ for (const i of t)
36
+ i in r && (e[i] = h(y(r[i])));
37
+ return e;
38
+ }
39
+ if (t !== "all")
40
+ throw new Error(`Invalid keys parameter: ${t}`);
41
+ return h(y(r));
42
42
  }
43
- class v {
43
+ class $ {
44
44
  constructor() {
45
- a(this, "_stateSnapshots", /* @__PURE__ */ new Map());
46
- a(this, "_storeHasRollbackSnapshots", /* @__PURE__ */ new Set());
47
- a(this, "_storeRollbackSnapshotsParams", /* @__PURE__ */ new Map());
48
- }
49
- addRollbackAction(s) {
50
- typeof s.$rollbackAfterFailure != "function" && (s.$rollbackAfterFailure = async ({ action: t, stateKeys: e }, i = []) => {
51
- const r = s[t];
52
- if (typeof r != "function" || O(t))
45
+ c(this, "_stateSnapshots", /* @__PURE__ */ new Map());
46
+ c(this, "_storeHasRollbackSnapshots", /* @__PURE__ */ new Set());
47
+ c(this, "_storeRollbackSnapshotsParams", /* @__PURE__ */ new Map());
48
+ }
49
+ addRollbackAction(t) {
50
+ typeof t.$rollbackAfterFailure != "function" && (t.$rollbackAfterFailure = async ({ action: e, stateKeys: i }, s = []) => {
51
+ const n = t[e];
52
+ if (typeof n != "function" || O(e))
53
53
  return;
54
- const o = !e || e.length === 0 ? "all" : e, h = this.saveStateSnapshot(s, t, o);
54
+ const o = !i || i.length === 0 ? "all" : i, a = this.saveStateSnapshot(t, e, o);
55
55
  try {
56
- await r.apply(s, i);
57
- } catch (c) {
58
- throw this.patchStore(h, s), c;
56
+ await n.apply(t, s);
57
+ } catch (l) {
58
+ throw this.patchStore(a, t), l;
59
59
  } finally {
60
- this.removeStateSnapshot(h);
60
+ this.removeStateSnapshot(a);
61
61
  }
62
62
  });
63
63
  }
64
- createOnActionCallbacks(s, t) {
65
- const e = this._storeRollbackSnapshotsParams.get(
66
- f(s, t)
64
+ createOnActionCallbacks(t, e) {
65
+ const i = this._storeRollbackSnapshotsParams.get(
66
+ b(t, e)
67
67
  );
68
- if (!e || e.length === 0)
68
+ if (!i)
69
69
  return;
70
- const i = this.saveStateSnapshot(t, s, e);
70
+ const s = i.length === 0 ? "all" : i, n = this.saveStateSnapshot(e, t, s);
71
71
  return {
72
72
  after: () => {
73
- this.removeStateSnapshot(i);
73
+ this.removeStateSnapshot(n);
74
74
  },
75
75
  onError: () => {
76
- this.patchStore(i, t), this.removeStateSnapshot(i);
76
+ this.patchStore(n, e), this.removeStateSnapshot(n);
77
77
  }
78
78
  };
79
79
  }
80
- initFromPluginContext({ options: s, store: t }) {
81
- const e = s?.storeOptions?.rollbackAfterFailure;
82
- e && !this._storeHasRollbackSnapshots.has(t.$id) && (Object.keys(e).forEach((i) => {
80
+ initFromPluginContext({ options: t, store: e }) {
81
+ const i = t?.storeOptions?.rollbackAfterFailure;
82
+ i && !this._storeHasRollbackSnapshots.has(e.$id) && (Object.keys(i).forEach((s) => {
83
83
  this._storeRollbackSnapshotsParams.set(
84
- f(i, t),
85
- e[i]
84
+ b(s, e),
85
+ i[s]
86
86
  );
87
- }), this._storeHasRollbackSnapshots.add(t.$id)), this.addRollbackAction(t);
88
- }
89
- clearStoreTracking(s) {
90
- const t = `-${s.$id}`;
91
- for (const e of Array.from(this._storeRollbackSnapshotsParams.keys()))
92
- e.endsWith(t) && this._storeRollbackSnapshotsParams.delete(e);
93
- this._storeHasRollbackSnapshots.delete(s.$id);
94
- for (const e of Array.from(this._stateSnapshots.keys()))
95
- e.description?.startsWith(`${s.$id}:`) && this._stateSnapshots.delete(e);
96
- }
97
- patchStore(s, t) {
98
- const e = this._stateSnapshots.get(s);
99
- if (e)
87
+ }), this._storeHasRollbackSnapshots.add(e.$id)), this.addRollbackAction(e);
88
+ }
89
+ clearStoreTracking(t) {
90
+ const e = `-${t.$id}`;
91
+ for (const i of Array.from(this._storeRollbackSnapshotsParams.keys()))
92
+ i.endsWith(e) && this._storeRollbackSnapshotsParams.delete(i);
93
+ this._storeHasRollbackSnapshots.delete(t.$id);
94
+ for (const i of Array.from(this._stateSnapshots.keys()))
95
+ i.description?.startsWith(`${t.$id}:`) && this._stateSnapshots.delete(i);
96
+ }
97
+ patchStore(t, e) {
98
+ const i = this._stateSnapshots.get(t);
99
+ if (i)
100
100
  try {
101
- t.$patch(e);
102
- } catch (i) {
103
- console.error(`Failed to patch store with snapshot ${String(s)}`, i);
101
+ e.$patch(i);
102
+ } catch (s) {
103
+ console.error(`Failed to patch store with snapshot ${String(t)}`, s);
104
104
  }
105
105
  }
106
- removeStateSnapshot(s) {
107
- s && this._stateSnapshots.delete(s);
106
+ removeStateSnapshot(t) {
107
+ t && this._stateSnapshots.delete(t);
108
108
  }
109
- saveStateSnapshot(s, t, e) {
110
- const i = $(
111
- s.$state,
112
- e
113
- ), r = /* @__PURE__ */ Symbol(`${s.$id}:${t}`);
114
- return this._stateSnapshots.set(r, i), r;
109
+ saveStateSnapshot(t, e, i) {
110
+ const s = g(
111
+ t.$state,
112
+ i
113
+ ), n = /* @__PURE__ */ Symbol(`${t.$id}:${e}`);
114
+ return this._stateSnapshots.set(n, s), n;
115
115
  }
116
- storeHasRollbackSnapshots(s) {
117
- return this._storeHasRollbackSnapshots.has(s.$id);
116
+ storeHasRollbackSnapshots(t) {
117
+ return this._storeHasRollbackSnapshots.has(t.$id);
118
118
  }
119
119
  }
120
- const A = "PluginSubscription", P = {
120
+ const m = "PluginSubscription", C = {
121
121
  environment: "both",
122
122
  hydration: "immediate"
123
123
  };
124
- function R(n) {
124
+ function w(r) {
125
125
  if (typeof window < "u" && typeof window.requestAnimationFrame == "function") {
126
- window.requestAnimationFrame(() => globalThis.setTimeout(n, 0));
126
+ window.requestAnimationFrame(() => globalThis.setTimeout(r, 0));
127
127
  return;
128
128
  }
129
- globalThis.setTimeout(n, 0);
129
+ globalThis.setTimeout(r, 0);
130
130
  }
131
- function D(n) {
132
- return n !== void 0 && !Array.isArray(n);
133
- }
134
- class k extends m {
135
- constructor(t, e, i) {
136
- const r = Array.isArray(e) ? e : void 0, o = D(e) ? e : i;
137
- super(Array.isArray(r) && !!r?.includes(A), C);
138
- a(this, "_className", A);
139
- a(this, "_hydrationScheduler");
140
- a(this, "_onActionSubscriptions", []);
141
- a(this, "_onActionAfterSubscriptions", {});
142
- a(this, "_onActionOnErrorSubscriptions", {});
143
- a(this, "_options");
144
- a(this, "_pluginDebug");
145
- a(this, "_resetStoreCallback", []);
146
- a(this, "_storeRollbackSnapshotsHandler", new v());
147
- a(this, "_subscribers", []);
148
- a(this, "_subscribersDelivered", /* @__PURE__ */ new Set());
149
- a(this, "_subscribersScheduled", /* @__PURE__ */ new Set());
150
- a(this, "_subscriptions", []);
151
- a(this, "_subscriptionsDelivered", /* @__PURE__ */ new Set());
152
- a(this, "_subscriptionsScheduled", /* @__PURE__ */ new Set());
153
- this._hydrationScheduler = o?.hydrationScheduler ?? R, this._options = o, this._pluginDebug = Array.isArray(r) ? r : void 0, this._subscribers = t;
131
+ class A {
132
+ constructor(t, e) {
133
+ c(this, "_className", m);
134
+ c(this, "_hydrationScheduler");
135
+ c(this, "_onActionSubscriptions", /* @__PURE__ */ new Map());
136
+ c(this, "_onActionStores", /* @__PURE__ */ new Set());
137
+ c(this, "_onActionAfterSubscriptions", {});
138
+ c(this, "_onActionOnErrorSubscriptions", {});
139
+ c(this, "_options");
140
+ c(this, "_resetStoreCallback", /* @__PURE__ */ new Map());
141
+ c(this, "_disposedStores", /* @__PURE__ */ new WeakSet());
142
+ c(this, "_storeRollbackSnapshotsHandler", new $());
143
+ c(this, "_subscribers", []);
144
+ c(this, "_subscribersDelivered", /* @__PURE__ */ new Set());
145
+ c(this, "_subscribersScheduled", /* @__PURE__ */ new Set());
146
+ c(this, "_subscriptions", []);
147
+ c(this, "_subscriptionsDelivered", /* @__PURE__ */ new Set());
148
+ c(this, "_subscriptionsScheduled", /* @__PURE__ */ new Set());
149
+ c(this, "_tracer");
150
+ this._hydrationScheduler = e?.hydrationScheduler ?? w, this._options = e, this._subscribers = t, this.initializeTracer();
154
151
  }
155
152
  set subscribers(t) {
156
153
  this._subscribers = t;
157
154
  }
158
155
  get subscriptions() {
159
- if (!y(this._subscriptions))
156
+ if (!_(this._subscriptions))
160
157
  return this._subscriptions;
161
158
  }
162
159
  addOnActionAfterCallback(t, e, i) {
163
- const r = f(t, e);
164
- this._onActionAfterSubscriptions[r] || (this._onActionAfterSubscriptions[r] = []), this._onActionAfterSubscriptions[r]?.push(i);
160
+ const s = b(t, e);
161
+ this._onActionAfterSubscriptions[s] || (this._onActionAfterSubscriptions[s] = []), this._onActionAfterSubscriptions[s]?.push(i);
165
162
  }
166
163
  addOnActionOnErrorCallback(t, e, i) {
167
- const r = f(t, e);
168
- this._onActionOnErrorSubscriptions[r] || (this._onActionOnErrorSubscriptions[r] = []), this._onActionOnErrorSubscriptions[r]?.push(i);
164
+ const s = b(t, e);
165
+ this._onActionOnErrorSubscriptions[s] || (this._onActionOnErrorSubscriptions[s] = []), this._onActionOnErrorSubscriptions[s]?.push(i);
169
166
  }
170
- addResetStoreCallback(t) {
171
- this._resetStoreCallback.push(t);
167
+ addResetStoreCallback(t, e) {
168
+ this._resetStoreCallback.has(t) || this._resetStoreCallback.set(t, /* @__PURE__ */ new Set()), this._resetStoreCallback.get(t).add(e);
172
169
  }
173
170
  clearOnActionCallbacksSubscriptions(t) {
174
171
  delete this._onActionAfterSubscriptions[t], delete this._onActionOnErrorSubscriptions[t];
175
172
  }
176
173
  clearStoreTracking(t) {
174
+ this._onActionSubscriptions.delete(t), this._onActionStores.delete(t), this._resetStoreCallback.delete(t);
177
175
  const e = `-${t.$id}`;
178
176
  for (const i of Array.from(this._subscribersDelivered))
179
177
  i.endsWith(e) && this._subscribersDelivered.delete(i);
@@ -201,22 +199,19 @@ class k extends m {
201
199
  createRollbackSnapshot(t, e) {
202
200
  const {
203
201
  after: i,
204
- onError: r
202
+ onError: s
205
203
  } = this._storeRollbackSnapshotsHandler.createOnActionCallbacks(
206
204
  t,
207
205
  e
208
206
  ) ?? {};
209
- !i || !r || (this.addOnActionAfterCallback(t, e, i), this.addOnActionOnErrorCallback(t, e, r));
207
+ !i || !s || (this.addOnActionAfterCallback(t, e, i), this.addOnActionOnErrorCallback(t, e, s));
210
208
  }
211
209
  defineCurrentEnvironment() {
212
210
  return this._options?.runtimeEnvironment ?? (typeof window > "u" ? "server" : "client");
213
211
  }
214
- definePluginDebug(t) {
215
- return !!this._pluginDebug?.includes(t.name);
216
- }
217
212
  definePluginExecution(t) {
218
213
  return {
219
- ...P,
214
+ ...C,
220
215
  ...this._options?.execution ?? {},
221
216
  ...t.execution ?? {},
222
217
  ...this._options?.subscriberExecution?.[t.name] ?? {}
@@ -232,75 +227,80 @@ class k extends m {
232
227
  return `${t}-${e.$id}`;
233
228
  }
234
229
  executeResetStoreCallbacks(t) {
235
- this._resetStoreCallback.forEach((e) => e(t));
230
+ this._resetStoreCallback.get(t)?.forEach((e) => e(t));
236
231
  }
237
232
  executeStoreOnActionSubscription(t) {
238
- const e = this._onActionSubscriptions, i = this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t);
239
- !e?.length && !i || t.$onAction(({ after: r, args: o, name: h, onError: c }) => {
240
- const l = f(h, t);
241
- this.debugLog(`storeOnActionSubscription ${l}`, { args: o, name: h, store: t }), i && this.createRollbackSnapshot(h, t), e?.length && e.forEach((b) => b({
242
- after: this.createOnActionAfterCallbackCollector(h, t),
243
- args: o,
244
- name: h,
245
- onError: this.createOnActionOnErrorCallbackCollector(h, t)
233
+ if (this._onActionStores.has(t) || typeof t.$onAction != "function")
234
+ return;
235
+ const e = this._onActionSubscriptions.get(t), i = this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t);
236
+ !e?.size && !i || (this._onActionStores.add(t), t.$onAction(({ after: s, args: n, name: o, onError: a }) => {
237
+ const l = b(o, t);
238
+ this.trace("plugin:onAction", () => ({
239
+ args: n,
240
+ name: o,
241
+ store: t
242
+ }), t.$id), this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t) && this.createRollbackSnapshot(o, t);
243
+ const u = this._onActionSubscriptions.get(t);
244
+ u?.size && u.forEach((d) => d({
245
+ after: this.createOnActionAfterCallbackCollector(o, t),
246
+ args: n,
247
+ name: o,
248
+ onError: this.createOnActionOnErrorCallbackCollector(o, t)
246
249
  }));
247
- const d = this._onActionAfterSubscriptions[l], S = this._onActionOnErrorSubscriptions[l];
248
- this.clearOnActionCallbacksSubscriptions(l), d?.length && r((b) => {
249
- d.forEach((p) => p(b));
250
- }), S?.length && c((b) => {
251
- S.forEach((p) => p(b));
250
+ const f = this._onActionAfterSubscriptions[l], p = this._onActionOnErrorSubscriptions[l];
251
+ this.clearOnActionCallbacksSubscriptions(l), f?.length && s((d) => {
252
+ f.forEach((S) => S(d));
253
+ }), p?.length && a((d) => {
254
+ p.forEach((S) => S(d));
252
255
  });
253
- });
256
+ }));
254
257
  }
255
258
  executeSubscriber(t, e) {
256
- const i = this.defineSubscriberKey(e, t.store), r = this.definePluginDebug(e);
257
- if (this._subscribersDelivered.has(i) || !e.invoke(
258
- t,
259
- r
260
- ))
259
+ const i = this.defineSubscriberKey(e, t.store);
260
+ if (this._subscribersDelivered.has(i) || !e.invoke(t, this._options?.createTracer))
261
261
  return;
262
262
  this._subscribersDelivered.add(i);
263
- let o;
263
+ let s;
264
264
  try {
265
- o = e.hydrate?.(t, r);
266
- } catch (c) {
267
- this.logError(c, t.store, t.options);
265
+ s = e.hydrate?.(t);
266
+ } catch (o) {
267
+ this.traceError("subscriber:hydrate", o, () => ({ store: t.store, options: t.options }), t.store.$id);
268
268
  return;
269
269
  }
270
- const h = () => {
270
+ const n = () => {
271
271
  try {
272
- const c = e.afterHydration?.(t, r);
273
- c && typeof c.then == "function" && c.catch((l) => this.logError(l, t.store, t.options));
274
- } catch (c) {
275
- this.logError(c, t.store, t.options);
272
+ const o = e.afterHydration?.(t);
273
+ o && typeof o.then == "function" && o.catch((a) => this.traceError("subscriber:afterHydration", a, () => ({ store: t.store, options: t.options }), t.store.$id));
274
+ } catch (o) {
275
+ this.traceError("subscriber:afterHydration", o, () => ({ store: t.store, options: t.options }), t.store.$id);
276
276
  }
277
277
  };
278
- o && typeof o.then == "function" ? o.then(() => h()).catch((c) => this.logError(c, t.store, t.options)) : h(), e.subscriptions && this.subscriptionDelivery(t, e.subscriptions), e.storeMutationSubscription && this.storeMutationSubscription(e.storeMutationSubscription), this.storeOnActionSubscription(e), e.resetStoreCallback && this.addResetStoreCallback(e.resetStoreCallback);
278
+ s && typeof s.then == "function" ? s.then(() => n()).catch((o) => this.traceError("subscriber:hydrate", o, () => ({ store: t.store, options: t.options }), t.store.$id)) : n(), e.subscriptions && this.subscriptionDelivery(t, e.subscriptions), e.storeMutationSubscription && this.storeMutationSubscription(e.storeMutationSubscription), this.storeOnActionSubscription(e), e.resetStoreCallback && this.addResetStoreCallback(t.store, e.resetStoreCallback);
279
279
  }
280
280
  executeSubscriberSafely(t, e) {
281
281
  try {
282
282
  this.executeSubscriber(t, e);
283
283
  } catch (i) {
284
- this.logError(i, t.store, t.options);
284
+ this.traceError("subscriber:execute", i, () => ({ store: t.store, options: t.options }), t.store.$id);
285
285
  }
286
286
  }
287
+ initializeTracer() {
288
+ this._options?.createTracer && (this._tracer = this._options.createTracer("PluginSubscription"));
289
+ }
287
290
  plugin({ store: t, options: e }) {
288
291
  if (this._subscribers.length) {
289
- this.debugLog(`plugin() - ${t.$id}`, [
290
- "subscriber:",
291
- this._subscribers,
292
- "store:",
293
- t,
294
- "options:",
295
- e
296
- ]);
292
+ this.trace("plugin:invoke", () => ({
293
+ subscribers: this._subscribers,
294
+ store: t,
295
+ options: e
296
+ }), t.$id);
297
297
  try {
298
298
  this._subscribers.forEach(
299
299
  (i) => {
300
- const r = this.defineSubscriberKey(i, t), o = this.definePluginExecution(i);
301
- if (!(this._subscribersDelivered.has(r) || this._subscribersScheduled.has(r)) && this.shouldInvokePlugin(o)) {
302
- if (this.shouldScheduleAfterHydration(o)) {
303
- this.queueSubscriberExecution({ store: t, options: e }, i, r);
300
+ const s = this.defineSubscriberKey(i, t), n = this.definePluginExecution(i);
301
+ if (!(this._subscribersDelivered.has(s) || this._subscribersScheduled.has(s)) && this.shouldInvokePlugin(n)) {
302
+ if (this.shouldScheduleAfterHydration(n)) {
303
+ this.queueSubscriberExecution({ store: t, options: e }, i, s);
304
304
  return;
305
305
  }
306
306
  this.executeSubscriber({ store: t, options: e }, i);
@@ -308,26 +308,29 @@ class k extends m {
308
308
  }
309
309
  ), this.rewriteResetStore({ store: t }, t.$state), this._storeRollbackSnapshotsHandler.initFromPluginContext({ store: t, options: e }), this.executeStoreOnActionSubscription(t), this.registerStoreCleanup(t);
310
310
  } catch (i) {
311
- this.logError(i, t, e);
311
+ this.traceError("plugin:invoke", i, () => ({ store: t, options: e }), t.$id);
312
312
  }
313
313
  }
314
314
  }
315
315
  queueSubscriberExecution(t, e, i) {
316
- const r = this.defineHydrationScheduler(e);
317
- this._subscribersScheduled.add(i), r(() => {
318
- this._subscribersScheduled.delete(i), this.executeSubscriberSafely(t, e);
316
+ const s = this.defineHydrationScheduler(e);
317
+ this._subscribersScheduled.add(i), s(() => {
318
+ this._disposedStores.has(t.store) || (this._subscribersScheduled.delete(i), this.executeSubscriberSafely(t, e));
319
319
  });
320
320
  }
321
321
  registerStoreCleanup(t) {
322
- if (this._onActionSubscriptions = [], typeof t.$dispose != "function")
322
+ if (typeof t.$dispose != "function")
323
323
  return;
324
324
  const e = t.$dispose.bind(t), i = t;
325
- i.__piniaPluginSubscriptionDisposed || (i.__piniaPluginSubscriptionDisposed = !0, t.$dispose = () => (this.clearStoreTracking(t), this._storeRollbackSnapshotsHandler.clearStoreTracking(t), e()));
325
+ i.__piniaPluginSubscriptionDisposed || (i.__piniaPluginSubscriptionDisposed = !0, t.$dispose = () => (this._disposedStores.add(t), this.clearStoreTracking(t), this._storeRollbackSnapshotsHandler.clearStoreTracking(t), e()));
326
326
  }
327
327
  rewriteResetStore({ store: t }, e) {
328
- const i = u(e);
328
+ const i = h(e);
329
329
  t.$reset = () => {
330
- this.debugLog("rewriteResetStore()", { initState: i, store: t }), this.executeResetStoreCallbacks(t), t.$patch(u(i));
330
+ this.trace("plugin:reset", () => ({
331
+ initState: i,
332
+ store: t
333
+ }), t.$id), this.executeResetStoreCallbacks(t), t.$patch(h(i));
331
334
  };
332
335
  }
333
336
  shouldInvokePlugin(t) {
@@ -339,95 +342,91 @@ class k extends m {
339
342
  }
340
343
  storeOnActionSubscription(t) {
341
344
  const { store: e, callback: i } = t?.storeOnActionSubscription?.() ?? {};
342
- !e || !i || this._onActionSubscriptions.push(i);
345
+ !e || !i || (this._onActionSubscriptions.has(e) || this._onActionSubscriptions.set(e, /* @__PURE__ */ new Set()), this._onActionSubscriptions.get(e).add(i), this.executeStoreOnActionSubscription(e));
343
346
  }
344
347
  storeMutationSubscription(t) {
345
348
  const { store: e, callback: i } = t();
346
- e.$subscribe((r) => {
347
- this.debugLog(`$subscribe ${e.$id}`, { mutation: r, store: e }), i(r);
349
+ e.$subscribe((s) => {
350
+ this.trace("store:mutation", () => ({ mutation: s, store: e }), e.$id), i(s);
348
351
  });
349
352
  }
350
353
  subscriptionDelivery(t, e) {
351
- Object.entries(e).forEach(([i, r]) => {
352
- const o = this.defineSubscriptionKey(i, t.store), h = this.definePluginExecution(r.subscription);
353
- if (!(this._subscriptionsDelivered.has(o) || this._subscriptionsScheduled.has(o)) && this.shouldInvokePlugin(h)) {
354
- if (this.shouldScheduleAfterHydration(h)) {
355
- const c = this.defineHydrationScheduler(r.subscription);
356
- this._subscriptionsScheduled.add(o), c(() => {
357
- this._subscriptionsScheduled.delete(o), this.subscriptionDeliverySafely(t, i, r);
354
+ Object.entries(e).forEach(([i, s]) => {
355
+ const n = this.defineSubscriptionKey(i, t.store), o = this.definePluginExecution(s.subscription);
356
+ if (!(this._subscriptionsDelivered.has(n) || this._subscriptionsScheduled.has(n)) && this.shouldInvokePlugin(o)) {
357
+ if (this.shouldScheduleAfterHydration(o)) {
358
+ const a = this.defineHydrationScheduler(s.subscription);
359
+ this._subscriptionsScheduled.add(n), a(() => {
360
+ this._disposedStores.has(t.store) || (this._subscriptionsScheduled.delete(n), this.subscriptionDeliverySafely(t, i, s));
358
361
  });
359
362
  return;
360
363
  }
361
- this.subscriptionDeliverySafely(t, i, r);
364
+ this.subscriptionDeliverySafely(t, i, s);
362
365
  }
363
366
  });
364
367
  }
365
- subscriptionDeliverySafely({ store: t, options: e }, i, r) {
368
+ subscriptionDeliverySafely({ store: t, options: e }, i, s) {
366
369
  try {
367
370
  if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(i, t)))
368
371
  return;
369
- const { subscription: o, subscriptionOptions: h, stores: c } = r;
370
- this.debugLog(`subscriptionDelivery() - store: ${t.$id}`, [
371
- "pluginName:",
372
- i,
373
- "subscription:",
374
- o,
375
- "options:",
376
- r,
377
- "stores:",
378
- c
379
- ]);
372
+ const { subscription: n, subscriptionOptions: o, stores: a } = s;
373
+ this.trace("subscription:delivery", () => ({
374
+ pluginName: i,
375
+ subscription: n,
376
+ options: s,
377
+ stores: a
378
+ }), t.$id);
380
379
  const l = {
381
- storeOptions: { ...e?.storeOptions ?? {}, ...h }
380
+ storeOptions: { ...e?.storeOptions ?? {}, ...o }
382
381
  };
383
- o.invoke(
384
- { store: t, options: { ...e, ...l } },
385
- this.debug
386
- ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(i, t)), y(c) || c?.forEach(
387
- (d) => o.invoke(
388
- {
389
- store: d,
390
- options: {
391
- ...e,
392
- ...l,
393
- ...d?.storeOptions ?? {}
394
- }
395
- },
396
- this.debug
397
- )
382
+ n.invoke(
383
+ { store: t, options: { ...e, ...l } }
384
+ ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(i, t)), _(a) || a?.forEach(
385
+ (u) => n.invoke({
386
+ store: u,
387
+ options: {
388
+ ...e,
389
+ ...l,
390
+ ...u?.storeOptions ?? {}
391
+ }
392
+ })
398
393
  );
399
- } catch (o) {
400
- this.logError("subscriptionDelivery()", o, t, e);
394
+ } catch (n) {
395
+ this.traceError("subscription:delivery", n, () => ({ store: t, options: e }), t.$id);
401
396
  }
402
397
  }
398
+ trace(t, e, i) {
399
+ this._tracer && this._tracer.debug(t, e, i);
400
+ }
401
+ traceError(t, e, i, s) {
402
+ this._tracer ? this._tracer.error(t, e, i, s) : console.error(e);
403
+ }
403
404
  }
404
- function j(n, s) {
405
- const t = new k(n, s);
406
- return t.plugin.bind(t);
405
+ function H(r, t) {
406
+ const e = new A(r, t);
407
+ return e.plugin.bind(e);
407
408
  }
408
- function K(n, s) {
409
- const t = new k(n, s?.debug, {
410
- execution: s?.execution,
411
- hydrationScheduler: s?.hydrationScheduler,
412
- runtimeEnvironment: s?.runtimeEnvironment,
413
- subscriberExecution: s?.subscriberExecution
409
+ function D(r, t) {
410
+ const e = new A(r, {
411
+ execution: t?.execution,
412
+ hydrationScheduler: t?.hydrationScheduler,
413
+ runtimeEnvironment: t?.runtimeEnvironment,
414
+ subscriberExecution: t?.subscriberExecution
414
415
  });
415
- return t.plugin.bind(t);
416
+ return e.plugin.bind(e);
416
417
  }
417
418
  export {
418
- T as CustomConsole,
419
- W as PLUGIN_NAME,
420
- L as PluginSubscriber,
421
- k as PluginSubscription,
422
- I as Store,
423
- K as createHydrationPlugin,
424
- j as createPlugin,
425
- q as defineAStore,
426
- G as defineAStoreCtx,
427
- U as getDefineAStoreSetupContext,
428
- z as getEnhancedStore,
429
- B as getExtendingStore,
430
- y as isEmpty,
431
- J as setEnhancedStore
419
+ K as PLUGIN_NAME,
420
+ M as PluginSubscriber,
421
+ A as PluginSubscription,
422
+ x as Store,
423
+ D as createHydrationPlugin,
424
+ H as createPlugin,
425
+ F as defineAStore,
426
+ W as defineAStoreCtx,
427
+ I as getDefineAStoreSetupContext,
428
+ q as getEnhancedStore,
429
+ z as getExtendingStore,
430
+ G as setEnhancedStore
432
431
  };
433
432
  //# sourceMappingURL=pinia-plugin-subscription.js.map