pinia-plugin-subscription 1.0.0-beta.8 → 1.0.0-beta.9

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.
package/CHANGELOG.md CHANGED
@@ -7,7 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Changed
11
+ - `storeMutationSubscription` now supports several subscribers per store, like `storeOnActionSubscription`: a single `$subscribe` is registered per store and dispatches to every callback. A failing callback is reported through `traceError` without blocking the others, subscriber names are tracked, and the hook is unsubscribed when the store is disposed
12
+ - `storeOnActionSubscription` now receives the Pinia plugin context; action trace labels include the invoked action name
13
+
10
14
  ### Added
15
+ - Added `addScopesListener(scopes, context?)` to `createTracerRegistry()` for grouping events from selected store scopes; documented in `docs/tracing.md`
11
16
  - `traceGroups` glossary, exported from `pinia-plugin-subscription/helpers`, building the `groupId` emitted by `PluginSubscription` (`onActionAfter`, `onActionError`, `onMutation`, `reset`); documented in `docs/tracing.md`
12
17
  - `createPluginSubscriber(name, invoke, optionalProps?)` factory, exported from `pinia-plugin-subscription/helpers`, to build plain-object subscribers; documented in `docs/authoring-plugins.md` and used by the demo plugin
13
18
  - Added a dedicated tracing guide, `docs/tracing.md`; the README now only presents tracing and links to it
@@ -43,6 +48,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
43
48
  - The `pluginsubscription` namespace prefix is now a shared constant (`pluginSubscriptionNamespace`) instead of a hard-coded string
44
49
 
45
50
  ### Fixed
51
+ - Group listener filters receive only trace metadata, redaction safely copies own `__proto__` keys, and registry group payload factories are skipped when no listeners are registered
52
+ - Subscriber, subscription, action and rollback tracking keys now encode store IDs and names without delimiter collisions; disposing a store removes only its own tracking entries
46
53
  - Trace groups opened by `onAction`, `$subscribe` and `$reset` are now always closed (also when the action fails or a callback throws), so grouped events no longer stay buffered forever
47
54
  - Listener definitions passed to `addListener()` are no longer mutated when their `filter` is an array of criteria
48
55
  - `TracerHandler` and `TraceRegistry` types now match the implementation (`createTracer(source, options?)`, `void` group callbacks, `addListener()` with options, `tracerHandler`); `createTracerRegistry()` is explicitly typed as `TraceRegistry`
package/README.md CHANGED
@@ -63,7 +63,8 @@ app.mount('#app')
63
63
 
64
64
  ## Advanced Features
65
65
 
66
- - **Structured tracing:** Subscribe to typed trace events and filter them at runtime, per store or per namespace — see [Tracing](#tracing).
66
+ - **Structured tracing:** Subscribe to typed trace events and filter them at runtime, per store or per namespace. Group filters receive metadata only, and the built-in registry avoids preparing group payloads when it has no listeners — see [Tracing](#tracing).
67
+ - **Scope-based trace listeners:** Use `addScopesListener()` to group events from selected stores — see [Tracing](docs/tracing.md#filtering-events).
67
68
  - **Reset callbacks:** Define `resetStoreCallback` to run custom logic when a store is reset.
68
69
  - **SSR and hydration control:** Configure whether each subscribed plugin runs on the server, the client, or only after client hydration.
69
70
  - **Lifecycle safety:** Internal hydration ordering is now guarded so `hydrate()` completes before `afterHydration()` and disposed stores do not retain stale subscriber metadata.
@@ -1,9 +1,9 @@
1
- var w = Object.defineProperty;
2
- var H = (i, s, t) => s in i ? w(i, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[s] = t;
1
+ var M = Object.defineProperty;
2
+ var H = (i, s, t) => s in i ? M(i, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[s] = t;
3
3
  var a = (i, s, t) => H(i, typeof s != "symbol" ? s + "" : s, t);
4
- import { toRaw as k } from "vue";
5
- import { defineStore as E } from "pinia";
6
- function u(i) {
4
+ import { toRaw as $ } from "vue";
5
+ import { defineStore as O } from "pinia";
6
+ function d(i) {
7
7
  if (typeof structuredClone == "function")
8
8
  try {
9
9
  return structuredClone(i);
@@ -13,64 +13,64 @@ function u(i) {
13
13
  return i;
14
14
  if (i instanceof Map)
15
15
  return new Map(
16
- Array.from(i.entries(), ([t, e]) => [u(t), u(e)])
16
+ Array.from(i.entries(), ([t, e]) => [d(t), d(e)])
17
17
  );
18
18
  if (i instanceof Set)
19
- return new Set(Array.from(i.values(), (t) => u(t)));
19
+ return new Set(Array.from(i.values(), (t) => d(t)));
20
20
  if (Array.isArray(i))
21
- return i.map((t) => u(t));
21
+ return i.map((t) => d(t));
22
22
  if (Object.getPrototypeOf(i) === Object.prototype || Object.getPrototypeOf(i) === null)
23
23
  return Object.fromEntries(
24
- Object.entries(i).map(([t, e]) => [t, u(e)])
24
+ Object.entries(i).map(([t, e]) => [t, d(e)])
25
25
  );
26
26
  const s = Object.create(Object.getPrototypeOf(i));
27
27
  for (const [t, e] of Object.entries(i))
28
- s[t] = u(e);
28
+ s[t] = d(e);
29
29
  return s;
30
30
  }
31
31
  function R(i, s = "all") {
32
32
  if (Array.isArray(s)) {
33
33
  const t = {};
34
34
  for (const e of s)
35
- e in i && (t[e] = u(k(i[e])));
35
+ e in i && (t[e] = d($(i[e])));
36
36
  return t;
37
37
  }
38
38
  if (s !== "all")
39
39
  throw new Error(`Invalid keys parameter: ${s}`);
40
- return u(k(i));
40
+ return d($(i));
41
41
  }
42
- const A = /* @__PURE__ */ new WeakMap(), b = /* @__PURE__ */ new Map(), $ = /* @__PURE__ */ new WeakSet();
43
- function x(i, s) {
44
- if ($.has(i) || typeof i.$dispose != "function")
42
+ const A = /* @__PURE__ */ new WeakMap(), b = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new WeakSet();
43
+ function T(i, s) {
44
+ if (g.has(i) || typeof i.$dispose != "function")
45
45
  return;
46
46
  const t = i.$dispose;
47
- i.$dispose = (...e) => (A.delete(i), b.delete(s), t.apply(i, e)), $.add(i);
47
+ i.$dispose = (...e) => (A.delete(i), b.delete(s), t.apply(i, e)), g.add(i);
48
48
  }
49
- function N(i, s, t) {
49
+ function q(i, s, t) {
50
50
  const e = t ? { storeOptions: t } : {};
51
- return typeof s == "function" ? g(
51
+ return typeof s == "function" ? C(
52
52
  i,
53
53
  s,
54
54
  e
55
- ) : v(
55
+ ) : x(
56
56
  i,
57
57
  s,
58
58
  e
59
59
  );
60
60
  }
61
- function q(i, s, t) {
61
+ function z(i, s, t) {
62
62
  const e = t ? { storeOptions: { ...t, enhancedStore: !0 } } : { storeOptions: { enhancedStore: !0 } };
63
- return g(
63
+ return C(
64
64
  i,
65
65
  s,
66
66
  e
67
67
  );
68
68
  }
69
- function g(i, s, t) {
69
+ function C(i, s, t) {
70
70
  const e = {
71
71
  id: i,
72
72
  extensions: {}
73
- }, r = t?.storeOptions?.enhancedStore === !0, n = E(
73
+ }, r = t?.storeOptions?.enhancedStore === !0, n = O(
74
74
  i,
75
75
  () => s(e),
76
76
  t
@@ -78,17 +78,17 @@ function g(i, s, t) {
78
78
  return Object.assign(((...c) => {
79
79
  r && b.set(i, e);
80
80
  const o = n(...c);
81
- return r && (A.set(o, e), b.delete(i), x(o, i)), o;
81
+ return r && (A.set(o, e), b.delete(i), T(o, i)), o;
82
82
  }), n);
83
83
  }
84
- function v(i, s, t) {
85
- return t && (s = { ...s, ...t ?? {} }), E(i, s);
84
+ function x(i, s, t) {
85
+ return t && (s = { ...s, ...t ?? {} }), O(i, s);
86
86
  }
87
87
  const P = /* @__PURE__ */ new Set(["_", "$"]);
88
- function T(i) {
88
+ function v(i) {
89
89
  return P.has(i[0]);
90
90
  }
91
- function z(i) {
91
+ function U(i) {
92
92
  const s = i;
93
93
  return A.get(i) ?? (typeof s?.$id == "string" ? b.get(s.$id) : void 0);
94
94
  }
@@ -101,8 +101,15 @@ function B(i) {
101
101
  function J(i, s) {
102
102
  i.extensions.enhancedStore = s, i.extensions.extending = s;
103
103
  }
104
- function S(i, s) {
105
- return `${i}-${s.$id}`;
104
+ function f(i, s) {
105
+ return _(i, s);
106
+ }
107
+ function y(i) {
108
+ const s = String(i.$id);
109
+ return `${s.length}:${s}:`;
110
+ }
111
+ function _(i, s) {
112
+ return `${y(s)}${i.length}:${i}`;
106
113
  }
107
114
  class D {
108
115
  constructor() {
@@ -113,13 +120,13 @@ class D {
113
120
  addRollbackAction(s) {
114
121
  typeof s.$rollbackAfterFailure != "function" && (s.$rollbackAfterFailure = async ({ action: t, stateKeys: e }, r = []) => {
115
122
  const n = s[t];
116
- if (typeof n != "function" || T(t))
123
+ if (typeof n != "function" || v(t))
117
124
  return;
118
125
  const c = !e || e.length === 0 ? "all" : e, o = this.saveStateSnapshot(s, t, c);
119
126
  try {
120
127
  await n.apply(s, r);
121
- } catch (h) {
122
- throw this.patchStore(o, s), h;
128
+ } catch (u) {
129
+ throw this.patchStore(o, s), u;
123
130
  } finally {
124
131
  this.removeStateSnapshot(o);
125
132
  }
@@ -127,7 +134,7 @@ class D {
127
134
  }
128
135
  createOnActionCallbacks(s, t) {
129
136
  const e = this._storeRollbackSnapshotsParams.get(
130
- S(s, t)
137
+ f(s, t)
131
138
  );
132
139
  if (!e)
133
140
  return;
@@ -146,18 +153,18 @@ class D {
146
153
  e && !this._storeHasRollbackSnapshots.has(t.$id) && (Object.keys(e).forEach((r) => {
147
154
  const n = e[r];
148
155
  n && this._storeRollbackSnapshotsParams.set(
149
- S(r, t),
156
+ f(r, t),
150
157
  n
151
158
  );
152
159
  }), this._storeHasRollbackSnapshots.add(t.$id)), this.addRollbackAction(t);
153
160
  }
154
161
  clearStoreTracking(s) {
155
- const t = `-${s.$id}`;
162
+ const t = y(s);
156
163
  for (const e of Array.from(this._storeRollbackSnapshotsParams.keys()))
157
- e.endsWith(t) && this._storeRollbackSnapshotsParams.delete(e);
164
+ e.startsWith(t) && this._storeRollbackSnapshotsParams.delete(e);
158
165
  this._storeHasRollbackSnapshots.delete(s.$id);
159
166
  for (const e of Array.from(this._stateSnapshots.keys()))
160
- e.description?.startsWith(`${s.$id}:`) && this._stateSnapshots.delete(e);
167
+ e.description?.startsWith(t) && this._stateSnapshots.delete(e);
161
168
  }
162
169
  patchStore(s, t) {
163
170
  const e = this._stateSnapshots.get(s);
@@ -175,7 +182,7 @@ class D {
175
182
  const r = R(
176
183
  s.$state,
177
184
  e
178
- ), n = /* @__PURE__ */ Symbol(`${s.$id}:${t}`);
185
+ ), n = Symbol(f(t, s));
179
186
  return this._stateSnapshots.set(n, r), n;
180
187
  }
181
188
  storeHasRollbackSnapshots(s) {
@@ -210,34 +217,38 @@ class G {
210
217
  return this.classNameAddedToTraceNamespace ? `${this._className}:${s}` : s;
211
218
  }
212
219
  }
213
- function O(i) {
220
+ function E(i) {
214
221
  return i ? Array.isArray(i) ? i.length === 0 : typeof i == "object" ? Object.keys(i).length === 0 : typeof i == "string" ? i.trim() === "" : !1 : !(typeof i == "boolean" || typeof i == "number");
215
222
  }
216
- const j = "pinia-plugin-subscription", C = "PluginSubscription", Q = C.toLowerCase(), d = {
223
+ const j = "pinia-plugin-subscription", m = "PluginSubscription", Q = m.toLowerCase(), l = {
217
224
  onActionAfter: (i) => `store:onAction:after:${i}`,
218
225
  onActionError: (i) => `store:onAction:onError:${i}`,
219
226
  onMutation: (i) => `store:onMutation:${i}`,
220
227
  reset: (i) => `store:reset:${i}`
221
- }, M = C, F = {
228
+ }, I = m, F = {
222
229
  environment: "both",
223
230
  hydration: "immediate"
224
231
  };
225
- function I(i) {
232
+ function K(i) {
226
233
  if (typeof window < "u" && typeof window.requestAnimationFrame == "function") {
227
234
  window.requestAnimationFrame(() => globalThis.setTimeout(i, 0));
228
235
  return;
229
236
  }
230
237
  globalThis.setTimeout(i, 0);
231
238
  }
232
- class U extends G {
239
+ class V extends G {
233
240
  constructor(t, e) {
234
241
  super();
235
- a(this, "_className", M);
242
+ a(this, "_className", I);
236
243
  a(this, "_hydrationScheduler");
237
244
  a(this, "_onActionSubscriptions", /* @__PURE__ */ new Map());
245
+ a(this, "_onActionSubscriptionsTraces", /* @__PURE__ */ new Map());
238
246
  a(this, "_onActionStores", /* @__PURE__ */ new Set());
239
247
  a(this, "_onActionAfterSubscriptions", {});
240
248
  a(this, "_onActionOnErrorSubscriptions", {});
249
+ a(this, "_onMutationSubscriptions", /* @__PURE__ */ new Map());
250
+ a(this, "_onMutationSubscriptionsTraces", /* @__PURE__ */ new Map());
251
+ a(this, "_onMutationUnsubscribes", /* @__PURE__ */ new Map());
241
252
  a(this, "_options");
242
253
  a(this, "_resetStoreCallback", /* @__PURE__ */ new Map());
243
254
  a(this, "_disposedStores", /* @__PURE__ */ new WeakSet());
@@ -248,21 +259,21 @@ class U extends G {
248
259
  a(this, "_subscriptions", []);
249
260
  a(this, "_subscriptionsDelivered", /* @__PURE__ */ new Set());
250
261
  a(this, "_subscriptionsScheduled", /* @__PURE__ */ new Set());
251
- this._hydrationScheduler = e?.hydrationScheduler ?? I, this._options = e, this._subscribers = t, this.initializeTracer();
262
+ this._hydrationScheduler = e?.hydrationScheduler ?? K, this._options = e, this._subscribers = t, this.initializeTracer();
252
263
  }
253
264
  set subscribers(t) {
254
265
  this._subscribers = t;
255
266
  }
256
267
  get subscriptions() {
257
- if (!O(this._subscriptions))
268
+ if (!E(this._subscriptions))
258
269
  return this._subscriptions;
259
270
  }
260
271
  addOnActionAfterCallback(t, e, r) {
261
- const n = S(t, e);
272
+ const n = f(t, e);
262
273
  this._onActionAfterSubscriptions[n] || (this._onActionAfterSubscriptions[n] = []), this._onActionAfterSubscriptions[n]?.push(r);
263
274
  }
264
275
  addOnActionOnErrorCallback(t, e, r) {
265
- const n = S(t, e);
276
+ const n = f(t, e);
266
277
  this._onActionOnErrorSubscriptions[n] || (this._onActionOnErrorSubscriptions[n] = []), this._onActionOnErrorSubscriptions[n]?.push(r);
267
278
  }
268
279
  addResetStoreCallback(t, e) {
@@ -272,20 +283,20 @@ class U extends G {
272
283
  delete this._onActionAfterSubscriptions[t], delete this._onActionOnErrorSubscriptions[t];
273
284
  }
274
285
  clearStoreTracking(t) {
275
- this._onActionSubscriptions.delete(t), this._onActionStores.delete(t), this._resetStoreCallback.delete(t);
276
- const e = `-${t.$id}`;
286
+ this._onActionSubscriptions.delete(t), this._onActionStores.delete(t), this._onMutationUnsubscribes.get(t)?.(), this._onMutationUnsubscribes.delete(t), this._onMutationSubscriptions.delete(t), this._resetStoreCallback.delete(t), this._onActionSubscriptionsTraces.delete(t.$id), this._onMutationSubscriptionsTraces.delete(t.$id);
287
+ const e = y(t);
277
288
  for (const r of Array.from(this._subscribersDelivered))
278
- r.endsWith(e) && this._subscribersDelivered.delete(r);
289
+ r.startsWith(e) && this._subscribersDelivered.delete(r);
279
290
  for (const r of Array.from(this._subscribersScheduled))
280
- r.endsWith(e) && this._subscribersScheduled.delete(r);
291
+ r.startsWith(e) && this._subscribersScheduled.delete(r);
281
292
  for (const r of Array.from(this._subscriptionsDelivered))
282
- r.endsWith(e) && this._subscriptionsDelivered.delete(r);
293
+ r.startsWith(e) && this._subscriptionsDelivered.delete(r);
283
294
  for (const r of Array.from(this._subscriptionsScheduled))
284
- r.endsWith(e) && this._subscriptionsScheduled.delete(r);
295
+ r.startsWith(e) && this._subscriptionsScheduled.delete(r);
285
296
  for (const r of Object.keys(this._onActionAfterSubscriptions))
286
- r.endsWith(e) && delete this._onActionAfterSubscriptions[r];
297
+ r.startsWith(e) && delete this._onActionAfterSubscriptions[r];
287
298
  for (const r of Object.keys(this._onActionOnErrorSubscriptions))
288
- r.endsWith(e) && delete this._onActionOnErrorSubscriptions[r];
299
+ r.startsWith(e) && delete this._onActionOnErrorSubscriptions[r];
289
300
  }
290
301
  createOnActionAfterCallbackCollector(t, e) {
291
302
  return (r) => {
@@ -322,10 +333,10 @@ class U extends G {
322
333
  return t.hydrationScheduler ?? this._hydrationScheduler;
323
334
  }
324
335
  defineSubscriberKey(t, e) {
325
- return `${t.name}-${e.$id}`;
336
+ return _(t.name, e);
326
337
  }
327
338
  defineSubscriptionKey(t, e) {
328
- return `${t}-${e.$id}`;
339
+ return _(t, e);
329
340
  }
330
341
  executeResetStoreCallbacks(t) {
331
342
  this._resetStoreCallback.get(t)?.forEach((e) => e(t));
@@ -334,66 +345,65 @@ class U extends G {
334
345
  if (this._onActionStores.has(t) || typeof t.$onAction != "function")
335
346
  return;
336
347
  const e = this._onActionSubscriptions.get(t), r = this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t);
337
- !e?.size && !r || (this._onActionStores.add(t), t.$onAction(({ after: n, args: c, name: o, onError: h }) => {
338
- const l = this._onActionSubscriptions.get(t), p = l?.size;
339
- if (!p)
348
+ !e?.size && !r || (this._onActionStores.add(t), t.$onAction(({ after: n, args: c, name: o, onError: u }) => {
349
+ const h = this._onActionSubscriptions.get(t);
350
+ if (!h?.size)
340
351
  return;
341
- this.traceGroupStart(() => ({ args: c, callbacks: p, name: o }), t.$id, "onAction");
342
- let _ = !1;
343
- const y = () => {
344
- _ || (_ = !0, this.traceGroupEnd({}, t.$id));
352
+ this.traceGroupStart(() => ({ args: c }), t.$id, `store:onAction:${o}`);
353
+ let p = !1;
354
+ const k = () => {
355
+ p || (p = !0, this.traceGroupEnd({}, t.$id));
345
356
  };
346
357
  try {
347
- const f = S(o, t);
348
- this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t) && this.createRollbackSnapshot(o, t), l.forEach((m) => m({
358
+ const S = f(o, t);
359
+ this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t) && this.createRollbackSnapshot(o, t), h.forEach((w) => w({
349
360
  after: this.createOnActionAfterCallbackCollector(o, t),
350
361
  args: c,
351
362
  name: o,
352
363
  onError: this.createOnActionOnErrorCallbackCollector(o, t)
353
- })), y(), this.executeStoreOnActionAfterCallbacks(n, f, t), this.executeStoreOnActionOnErrorCallbacks(h, f, t), this.clearOnActionCallbacksSubscriptions(f);
354
- } catch (f) {
355
- throw y(), f;
364
+ })), k(), this.executeStoreOnActionAfterCallbacks(n, S, t, o), this.executeStoreOnActionOnErrorCallbacks(u, S, t, o), this.clearOnActionCallbacksSubscriptions(S);
365
+ } catch (S) {
366
+ throw k(), S;
356
367
  }
357
368
  }));
358
369
  }
359
- executeStoreOnActionAfterCallbacks(t, e, r) {
360
- const n = this._onActionAfterSubscriptions[e], c = n?.length;
361
- c && t((o) => {
370
+ executeStoreOnActionAfterCallbacks(t, e, r, n) {
371
+ const c = this._onActionAfterSubscriptions[e];
372
+ c?.length && t((o) => {
362
373
  this.traceGroupStart(
363
374
  () => ({
364
- callbacks: c,
365
- groupId: d.onActionAfter(r.$id),
375
+ groupId: l.onActionAfter(r.$id),
366
376
  result: o
367
377
  }),
368
378
  r.$id,
369
- "store:onAction:after"
370
- ), n.forEach((h) => {
379
+ `store:onAction:after:${n}`
380
+ ), c.forEach((u) => {
371
381
  try {
372
- h(o);
373
- } catch (l) {
374
- this.traceError("storeOnActionAfterCallbacks", l, r.$id);
382
+ u(o);
383
+ } catch (h) {
384
+ this.traceError("storeOnActionAfterCallbacks", h, r.$id, () => ({ "action name": n }));
375
385
  }
376
- }), this.traceGroupEnd(() => ({ groupId: d.onActionAfter(r.$id) }), r.$id);
386
+ }), this.traceGroupEnd(() => ({ groupId: l.onActionAfter(r.$id) }), r.$id);
377
387
  });
378
388
  }
379
- executeStoreOnActionOnErrorCallbacks(t, e, r) {
380
- const n = this._onActionOnErrorSubscriptions[e];
381
- n?.length && t((c) => {
389
+ executeStoreOnActionOnErrorCallbacks(t, e, r, n) {
390
+ const c = this._onActionOnErrorSubscriptions[e];
391
+ c?.length && t((o) => {
382
392
  this.traceGroupStart(
383
393
  () => ({
384
- callbacks: n.length,
385
- error: c,
386
- groupId: d.onActionError(r.$id)
394
+ "action name": n,
395
+ error: o,
396
+ groupId: l.onActionError(r.$id)
387
397
  }),
388
398
  r.$id,
389
- "store:onAction:onError"
390
- ), n.forEach((o) => {
399
+ `store:onAction:onError:${n}`
400
+ ), c.forEach((u) => {
391
401
  try {
392
- o(c);
402
+ u(o);
393
403
  } catch (h) {
394
- this.traceError("store:onAction:onError:error", h, r.$id);
404
+ this.traceError("store:onAction:onError:error", h, r.$id, () => ({ groupId: l.onActionError(r.$id) }));
395
405
  }
396
- }), this.traceGroupEnd(() => ({ groupId: d.onActionError(r.$id) }), r.$id);
406
+ }), this.traceGroupEnd(() => ({ groupId: l.onActionError(r.$id) }), r.$id);
397
407
  });
398
408
  }
399
409
  executeSubscriber(t, e) {
@@ -412,13 +422,13 @@ class U extends G {
412
422
  try {
413
423
  const o = e.afterHydration?.(t);
414
424
  o && typeof o.then == "function" && o.catch(
415
- (h) => this.traceError("subscriber:afterHydration", h, t.store.$id)
425
+ (u) => this.traceError("subscriber:afterHydration", u, t.store.$id)
416
426
  );
417
427
  } catch (o) {
418
428
  this.traceError("subscriber:afterHydration", o, t.store.$id);
419
429
  }
420
430
  };
421
- n && typeof n.then == "function" ? n.then(() => c()).catch((o) => this.traceError("subscriber:hydrate", o, t.store.$id)) : c(), e.subscriptions && this.subscriptionDelivery(t, e.subscriptions), e.storeMutationSubscription && this.storeMutationSubscription(e.storeMutationSubscription), this.storeOnActionSubscription(e), e.resetStoreCallback && this.addResetStoreCallback(t.store, e.resetStoreCallback);
431
+ n && typeof n.then == "function" ? n.then(() => c()).catch((o) => this.traceError("subscriber:hydrate", o, t.store.$id)) : c(), e.subscriptions && this.subscriptionDelivery(t, e.subscriptions), e.storeMutationSubscription && this.storeMutationSubscription(e, e.storeMutationSubscription), this.storeOnActionSubscription(e, t), e.resetStoreCallback && this.addResetStoreCallback(t.store, e.resetStoreCallback);
422
432
  }
423
433
  executeSubscriberSafely(t, e) {
424
434
  try {
@@ -431,31 +441,39 @@ class U extends G {
431
441
  this._options?.tracerHandler && (this._tracer = this._options.tracerHandler.createTracer(j));
432
442
  }
433
443
  plugin({ store: t, options: e }) {
434
- if (this._subscribers.length) {
435
- this.traceGroupStart(() => ({
436
- environment: this.defineCurrentEnvironment(),
437
- subscribers: this._subscribers,
438
- store: t,
439
- options: e
440
- }), t.$id);
441
- try {
442
- this._subscribers.forEach(
443
- (r) => {
444
- const n = this.defineSubscriberKey(r, t), c = this.definePluginExecution(r);
445
- if (!(this._subscribersDelivered.has(n) || this._subscribersScheduled.has(n)) && this.shouldInvokePlugin(c)) {
446
- if (this.shouldScheduleAfterHydration(c)) {
447
- this.queueSubscriberExecution({ store: t, options: e }, r, n);
448
- return;
449
- }
450
- this.executeSubscriber({ store: t, options: e }, r);
444
+ if (!this._subscribers.length)
445
+ return;
446
+ const r = {
447
+ $onAction: [],
448
+ $subscribe: []
449
+ };
450
+ this.traceGroupStart(() => ({
451
+ environment: {
452
+ current: this.defineCurrentEnvironment(),
453
+ ...this._options?.execution ?? {}
454
+ },
455
+ subscribers: this._subscribers,
456
+ "hooks subscriptions": r,
457
+ store: t,
458
+ options: e
459
+ }), t.$id), this._onActionSubscriptionsTraces.set(t.$id, r.$onAction), this._onMutationSubscriptionsTraces.set(t.$id, r.$subscribe);
460
+ try {
461
+ this._subscribers.forEach(
462
+ (n) => {
463
+ const c = this.defineSubscriberKey(n, t), o = this.definePluginExecution(n);
464
+ if (!(this._subscribersDelivered.has(c) || this._subscribersScheduled.has(c)) && this.shouldInvokePlugin(o)) {
465
+ if (this.shouldScheduleAfterHydration(o)) {
466
+ this.queueSubscriberExecution({ store: t, options: e }, n, c);
467
+ return;
451
468
  }
469
+ this.executeSubscriber({ store: t, options: e }, n);
452
470
  }
453
- ), this.rewriteResetStore({ store: t }, t.$state), this._storeRollbackSnapshotsHandler.initFromPluginContext({ store: t, options: e }), this.executeStoreOnActionSubscription(t), this.registerStoreCleanup(t);
454
- } catch (r) {
455
- this.traceError("plugin:invoke", r, t.$id);
456
- }
457
- this.traceGroupEnd(() => ({}), t.$id);
471
+ }
472
+ ), this.rewriteResetStore({ store: t }, t.$state), this._storeRollbackSnapshotsHandler.initFromPluginContext({ store: t, options: e }), this.executeStoreOnActionSubscription(t), this.registerStoreCleanup(t);
473
+ } catch (n) {
474
+ this.traceError("plugin:invoke", n, t.$id);
458
475
  }
476
+ this.traceGroupEnd(() => ({}), t.$id);
459
477
  }
460
478
  queueSubscriberExecution(t, e, r) {
461
479
  const n = this.defineHydrationScheduler(e);
@@ -470,13 +488,13 @@ class U extends G {
470
488
  r.__piniaPluginSubscriptionDisposed || (r.__piniaPluginSubscriptionDisposed = !0, t.$dispose = () => (this._disposedStores.add(t), this.clearStoreTracking(t), this._storeRollbackSnapshotsHandler.clearStoreTracking(t), e()));
471
489
  }
472
490
  rewriteResetStore({ store: t }, e) {
473
- const r = u(e);
491
+ const r = d(e);
474
492
  t.$reset = () => {
475
- this.traceGroupStart(() => ({ initialState: r, groupId: d.reset(t.$id) }), t.$id, "store:reset");
493
+ this.traceGroupStart(() => ({ initialState: r, groupId: l.reset(t.$id) }), t.$id, "store:reset");
476
494
  try {
477
- this.executeResetStoreCallbacks(t), t.$patch(u(r));
495
+ this.executeResetStoreCallbacks(t), t.$patch(d(r));
478
496
  } finally {
479
- this.traceGroupEnd(() => ({ initialState: r, groupId: d.reset(t.$id) }), t.$id);
497
+ this.traceGroupEnd(() => ({ initialState: r, groupId: l.reset(t.$id) }), t.$id);
480
498
  }
481
499
  };
482
500
  }
@@ -487,28 +505,40 @@ class U extends G {
487
505
  shouldScheduleAfterHydration(t) {
488
506
  return this.defineCurrentEnvironment() === "client" && t.hydration === "defer";
489
507
  }
490
- storeOnActionSubscription(t) {
491
- const { store: e, callback: r } = t?.storeOnActionSubscription?.() ?? {};
492
- !e || !r || (this._onActionSubscriptions.has(e) || this._onActionSubscriptions.set(e, /* @__PURE__ */ new Set()), this._onActionSubscriptions.get(e).add(r), this.executeStoreOnActionSubscription(e));
508
+ storeOnActionSubscription(t, e) {
509
+ const { store: r, callback: n } = t?.storeOnActionSubscription?.(e) ?? {};
510
+ !r || !n || (this._onActionSubscriptions.has(r) || this._onActionSubscriptions.set(r, /* @__PURE__ */ new Set()), this._onActionSubscriptions.get(r).add(n), this._onActionSubscriptionsTraces.get(r.$id)?.push(t.name));
493
511
  }
494
- storeMutationSubscription(t) {
495
- const { store: e, callback: r } = t();
496
- e.$subscribe((n) => {
497
- this.traceGroupStart(() => ({ mutation: n, groupId: d.onMutation(e.$id) }), e.$id, "store:onMutation");
498
- try {
499
- r(n);
500
- } finally {
501
- this.traceGroupEnd(() => ({ mutation: n, groupId: d.onMutation(e.$id) }), e.$id);
512
+ storeMutationSubscription(t, e) {
513
+ const { store: r, callback: n } = e();
514
+ if (!r || !n || (this._onMutationSubscriptions.has(r) || this._onMutationSubscriptions.set(r, /* @__PURE__ */ new Set()), this._onMutationSubscriptions.get(r).add(n), this._onMutationSubscriptionsTraces.get(r.$id)?.push(t.name), this._onMutationUnsubscribes.has(r)))
515
+ return;
516
+ const c = r.$subscribe((o) => {
517
+ const u = this._onMutationSubscriptions.get(r);
518
+ if (u?.size) {
519
+ this.traceGroupStart(() => ({ mutation: o, groupId: l.onMutation(r.$id) }), r.$id, "store:onMutation");
520
+ try {
521
+ u.forEach((h) => {
522
+ try {
523
+ h(o);
524
+ } catch (p) {
525
+ this.traceError("store:onMutation", p, r.$id);
526
+ }
527
+ });
528
+ } finally {
529
+ this.traceGroupEnd(() => ({ mutation: o, groupId: l.onMutation(r.$id) }), r.$id);
530
+ }
502
531
  }
503
532
  });
533
+ this._onMutationUnsubscribes.set(r, c);
504
534
  }
505
535
  subscriptionDelivery(t, e) {
506
536
  Object.entries(e).forEach(([r, n]) => {
507
537
  const c = this.defineSubscriptionKey(r, t.store), o = this.definePluginExecution(n.subscription);
508
538
  if (!(this._subscriptionsDelivered.has(c) || this._subscriptionsScheduled.has(c)) && this.shouldInvokePlugin(o)) {
509
539
  if (this.shouldScheduleAfterHydration(o)) {
510
- const h = this.defineHydrationScheduler(n.subscription);
511
- this._subscriptionsScheduled.add(c), h(() => {
540
+ const u = this.defineHydrationScheduler(n.subscription);
541
+ this._subscriptionsScheduled.add(c), u(() => {
512
542
  this._disposedStores.has(t.store) || (this._subscriptionsScheduled.delete(c), this.subscriptionDeliverySafely(t, r, n));
513
543
  });
514
544
  return;
@@ -521,23 +551,23 @@ class U extends G {
521
551
  try {
522
552
  if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(r, t)))
523
553
  return;
524
- const { subscription: c, subscriptionOptions: o, stores: h } = n;
554
+ const { subscription: c, subscriptionOptions: o, stores: u } = n;
525
555
  this.trace("Subscription:delivery", t.$id, () => ({
526
556
  pluginName: r,
527
557
  subscription: c,
528
558
  subscriptionOptions: o
529
559
  }));
530
- const l = {
560
+ const h = {
531
561
  storeOptions: { ...e?.storeOptions ?? {}, ...o }
532
562
  };
533
563
  c.invoke(
534
- { store: t, options: { ...e, ...l } }
535
- ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(r, t)), O(h) || h?.forEach(
564
+ { store: t, options: { ...e, ...h } }
565
+ ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(r, t)), E(u) || u?.forEach(
536
566
  (p) => c.invoke({
537
567
  store: p,
538
568
  options: {
539
569
  ...e,
540
- ...l,
570
+ ...h,
541
571
  ...p?.storeOptions ?? {}
542
572
  }
543
573
  })
@@ -547,30 +577,32 @@ class U extends G {
547
577
  }
548
578
  }
549
579
  traceGroupEnd(t, e) {
550
- this._options?.tracerHandler && this._options.tracerHandler.closeGroup({
580
+ const r = this._options?.tracerHandler;
581
+ r && (r.hasTraceListeners?.() ?? !0) && r.closeGroup({
551
582
  ...typeof t == "function" ? t() : t ?? {},
552
583
  scope: e
553
584
  });
554
585
  }
555
586
  traceGroupStart(t, e, r) {
556
- this._options?.tracerHandler && (t = typeof t == "function" ? t() : t ?? {}, this._options.tracerHandler.startGroup({
587
+ const n = this._options?.tracerHandler;
588
+ n && (n.hasTraceListeners?.() ?? !0) && (t = typeof t == "function" ? t() : t ?? {}, n.startGroup({
557
589
  ...t,
558
590
  scope: e
559
591
  }), this.trace(r ?? "context", e, t));
560
592
  }
561
593
  }
562
594
  export {
563
- U as P,
595
+ V as P,
564
596
  G as T,
565
597
  Q as a,
566
- q as b,
598
+ z as b,
567
599
  B as c,
568
- N as d,
569
- z as g,
570
- T as h,
571
- O as i,
600
+ q as d,
601
+ U as g,
602
+ v as h,
603
+ E as i,
572
604
  j as p,
573
605
  J as s,
574
- d as t
606
+ l as t
575
607
  };
576
- //# sourceMappingURL=PluginSubscription-C_a6v8oi.js.map
608
+ //# sourceMappingURL=PluginSubscription-ZO5mkdvZ.js.map