pinia-plugin-subscription 1.0.0-beta.7 → 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,13 @@ 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`
16
+ - `traceGroups` glossary, exported from `pinia-plugin-subscription/helpers`, building the `groupId` emitted by `PluginSubscription` (`onActionAfter`, `onActionError`, `onMutation`, `reset`); documented in `docs/tracing.md`
11
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
12
18
  - Added a dedicated tracing guide, `docs/tracing.md`; the README now only presents tracing and links to it
13
19
  - Moved the API reference, import subpaths, development, `Store` summary and testing sections from `README.md` to `docs/authoring-plugins.md`
@@ -42,6 +48,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
42
48
  - The `pluginsubscription` namespace prefix is now a shared constant (`pluginSubscriptionNamespace`) instead of a hard-coded string
43
49
 
44
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
45
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
46
54
  - Listener definitions passed to `addListener()` are no longer mutated when their `filter` is an array of criteria
47
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 g = Object.defineProperty;
2
- var w = (i, s, t) => s in i ? g(i, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[s] = t;
3
- var a = (i, s, t) => w(i, typeof s != "symbol" ? s + "" : s, t);
4
- import { toRaw as A } from "vue";
5
- import { defineStore as $ } from "pinia";
6
- function u(i) {
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
+ var a = (i, s, t) => H(i, typeof s != "symbol" ? s + "" : s, t);
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,52 +13,52 @@ 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
- function x(i, s = "all") {
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(A(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(A(i));
40
+ return d($(i));
41
41
  }
42
- const b = /* @__PURE__ */ new WeakMap(), S = /* @__PURE__ */ new Map(), k = /* @__PURE__ */ new WeakSet();
43
- function H(i, s) {
44
- if (k.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) => (b.delete(i), S.delete(s), t.apply(i, e)), k.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
51
  return typeof s == "function" ? C(
52
52
  i,
53
53
  s,
54
54
  e
55
- ) : R(
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
63
  return C(
64
64
  i,
@@ -70,41 +70,48 @@ function C(i, s, t) {
70
70
  const e = {
71
71
  id: i,
72
72
  extensions: {}
73
- }, r = t?.storeOptions?.enhancedStore === !0, n = $(
73
+ }, r = t?.storeOptions?.enhancedStore === !0, n = O(
74
74
  i,
75
75
  () => s(e),
76
76
  t
77
77
  );
78
78
  return Object.assign(((...c) => {
79
- r && S.set(i, e);
79
+ r && b.set(i, e);
80
80
  const o = n(...c);
81
- return r && (b.set(o, e), S.delete(i), H(o, i)), o;
81
+ return r && (A.set(o, e), b.delete(i), T(o, i)), o;
82
82
  }), n);
83
83
  }
84
- function R(i, s, t) {
85
- return t && (s = { ...s, ...t ?? {} }), $(i, s);
84
+ function x(i, s, t) {
85
+ return t && (s = { ...s, ...t ?? {} }), O(i, s);
86
86
  }
87
- const v = /* @__PURE__ */ new Set(["_", "$"]);
88
- function P(i) {
89
- return v.has(i[0]);
87
+ const P = /* @__PURE__ */ new Set(["_", "$"]);
88
+ function v(i) {
89
+ return P.has(i[0]);
90
90
  }
91
- function I(i) {
91
+ function U(i) {
92
92
  const s = i;
93
- return b.get(i) ?? (typeof s?.$id == "string" ? S.get(s.$id) : void 0);
93
+ return A.get(i) ?? (typeof s?.$id == "string" ? b.get(s.$id) : void 0);
94
94
  }
95
- function z(i) {
95
+ function B(i) {
96
96
  const s = i.extensions.enhancedStore ?? i.extensions.extending;
97
97
  if (!s)
98
98
  throw new Error(`${i.id}Store - getEnhancedStore - Error: enhanced store is required`);
99
99
  return s;
100
100
  }
101
- function B(i, s) {
101
+ function J(i, s) {
102
102
  i.extensions.enhancedStore = s, i.extensions.extending = s;
103
103
  }
104
104
  function f(i, s) {
105
- return `${i}-${s.$id}`;
105
+ return _(i, s);
106
+ }
107
+ function y(i) {
108
+ const s = String(i.$id);
109
+ return `${s.length}:${s}:`;
106
110
  }
107
- class T {
111
+ function _(i, s) {
112
+ return `${y(s)}${i.length}:${i}`;
113
+ }
114
+ class D {
108
115
  constructor() {
109
116
  a(this, "_stateSnapshots", /* @__PURE__ */ new Map());
110
117
  a(this, "_storeHasRollbackSnapshots", /* @__PURE__ */ new Set());
@@ -113,13 +120,13 @@ class T {
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" || P(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
  }
@@ -152,12 +159,12 @@ class T {
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);
@@ -172,17 +179,17 @@ class T {
172
179
  s && this._stateSnapshots.delete(s);
173
180
  }
174
181
  saveStateSnapshot(s, t, e) {
175
- const r = x(
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) {
182
189
  return this._storeHasRollbackSnapshots.has(s.$id);
183
190
  }
184
191
  }
185
- class D {
192
+ class G {
186
193
  constructor() {
187
194
  a(this, "_tracer");
188
195
  a(this, "_traceOptions", {
@@ -210,10 +217,15 @@ class D {
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", E = "PluginSubscription", J = E.toLowerCase(), G = E, F = {
223
+ const j = "pinia-plugin-subscription", m = "PluginSubscription", Q = m.toLowerCase(), l = {
224
+ onActionAfter: (i) => `store:onAction:after:${i}`,
225
+ onActionError: (i) => `store:onAction:onError:${i}`,
226
+ onMutation: (i) => `store:onMutation:${i}`,
227
+ reset: (i) => `store:reset:${i}`
228
+ }, I = m, F = {
217
229
  environment: "both",
218
230
  hydration: "immediate"
219
231
  };
@@ -224,19 +236,23 @@ function K(i) {
224
236
  }
225
237
  globalThis.setTimeout(i, 0);
226
238
  }
227
- class Q extends D {
239
+ class V extends G {
228
240
  constructor(t, e) {
229
241
  super();
230
- a(this, "_className", G);
242
+ a(this, "_className", I);
231
243
  a(this, "_hydrationScheduler");
232
244
  a(this, "_onActionSubscriptions", /* @__PURE__ */ new Map());
245
+ a(this, "_onActionSubscriptionsTraces", /* @__PURE__ */ new Map());
233
246
  a(this, "_onActionStores", /* @__PURE__ */ new Set());
234
247
  a(this, "_onActionAfterSubscriptions", {});
235
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());
236
252
  a(this, "_options");
237
253
  a(this, "_resetStoreCallback", /* @__PURE__ */ new Map());
238
254
  a(this, "_disposedStores", /* @__PURE__ */ new WeakSet());
239
- a(this, "_storeRollbackSnapshotsHandler", new T());
255
+ a(this, "_storeRollbackSnapshotsHandler", new D());
240
256
  a(this, "_subscribers", []);
241
257
  a(this, "_subscribersDelivered", /* @__PURE__ */ new Set());
242
258
  a(this, "_subscribersScheduled", /* @__PURE__ */ new Set());
@@ -249,7 +265,7 @@ class Q extends D {
249
265
  this._subscribers = t;
250
266
  }
251
267
  get subscriptions() {
252
- if (!O(this._subscriptions))
268
+ if (!E(this._subscriptions))
253
269
  return this._subscriptions;
254
270
  }
255
271
  addOnActionAfterCallback(t, e, r) {
@@ -267,20 +283,20 @@ class Q extends D {
267
283
  delete this._onActionAfterSubscriptions[t], delete this._onActionOnErrorSubscriptions[t];
268
284
  }
269
285
  clearStoreTracking(t) {
270
- this._onActionSubscriptions.delete(t), this._onActionStores.delete(t), this._resetStoreCallback.delete(t);
271
- 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);
272
288
  for (const r of Array.from(this._subscribersDelivered))
273
- r.endsWith(e) && this._subscribersDelivered.delete(r);
289
+ r.startsWith(e) && this._subscribersDelivered.delete(r);
274
290
  for (const r of Array.from(this._subscribersScheduled))
275
- r.endsWith(e) && this._subscribersScheduled.delete(r);
291
+ r.startsWith(e) && this._subscribersScheduled.delete(r);
276
292
  for (const r of Array.from(this._subscriptionsDelivered))
277
- r.endsWith(e) && this._subscriptionsDelivered.delete(r);
293
+ r.startsWith(e) && this._subscriptionsDelivered.delete(r);
278
294
  for (const r of Array.from(this._subscriptionsScheduled))
279
- r.endsWith(e) && this._subscriptionsScheduled.delete(r);
295
+ r.startsWith(e) && this._subscriptionsScheduled.delete(r);
280
296
  for (const r of Object.keys(this._onActionAfterSubscriptions))
281
- r.endsWith(e) && delete this._onActionAfterSubscriptions[r];
297
+ r.startsWith(e) && delete this._onActionAfterSubscriptions[r];
282
298
  for (const r of Object.keys(this._onActionOnErrorSubscriptions))
283
- r.endsWith(e) && delete this._onActionOnErrorSubscriptions[r];
299
+ r.startsWith(e) && delete this._onActionOnErrorSubscriptions[r];
284
300
  }
285
301
  createOnActionAfterCallbackCollector(t, e) {
286
302
  return (r) => {
@@ -317,10 +333,10 @@ class Q extends D {
317
333
  return t.hydrationScheduler ?? this._hydrationScheduler;
318
334
  }
319
335
  defineSubscriberKey(t, e) {
320
- return `${t.name}-${e.$id}`;
336
+ return _(t.name, e);
321
337
  }
322
338
  defineSubscriptionKey(t, e) {
323
- return `${t}-${e.$id}`;
339
+ return _(t, e);
324
340
  }
325
341
  executeResetStoreCallbacks(t) {
326
342
  this._resetStoreCallback.get(t)?.forEach((e) => e(t));
@@ -329,56 +345,65 @@ class Q extends D {
329
345
  if (this._onActionStores.has(t) || typeof t.$onAction != "function")
330
346
  return;
331
347
  const e = this._onActionSubscriptions.get(t), r = this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t);
332
- !e?.size && !r || (this._onActionStores.add(t), t.$onAction(({ after: n, args: c, name: o, onError: h }) => {
333
- const l = this._onActionSubscriptions.get(t), d = l?.size;
334
- if (!d)
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)
335
351
  return;
336
- this.traceGroupStart(() => ({ args: c, callbacks: d, name: o }), t.$id, "onAction");
337
- let _ = !1;
338
- const y = () => {
339
- _ || (_ = !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));
340
356
  };
341
357
  try {
342
- const p = f(o, t);
343
- 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({
344
360
  after: this.createOnActionAfterCallbackCollector(o, t),
345
361
  args: c,
346
362
  name: o,
347
363
  onError: this.createOnActionOnErrorCallbackCollector(o, t)
348
- })), y(), this.executeStoreOnActionAfterCallbacks(n, p, t), this.executeStoreOnActionOnErrorCallbacks(h, p, t), this.clearOnActionCallbacksSubscriptions(p);
349
- } catch (p) {
350
- throw y(), p;
364
+ })), k(), this.executeStoreOnActionAfterCallbacks(n, S, t, o), this.executeStoreOnActionOnErrorCallbacks(u, S, t, o), this.clearOnActionCallbacksSubscriptions(S);
365
+ } catch (S) {
366
+ throw k(), S;
351
367
  }
352
368
  }));
353
369
  }
354
- executeStoreOnActionAfterCallbacks(t, e, r) {
355
- const n = this._onActionAfterSubscriptions[e], c = n?.length;
356
- c && t((o) => {
357
- this.traceGroupStart(() => ({
358
- callbacks: c,
359
- result: o
360
- }), r.$id, "execute:store:onAction:after:callbacks"), n.forEach((h) => {
370
+ executeStoreOnActionAfterCallbacks(t, e, r, n) {
371
+ const c = this._onActionAfterSubscriptions[e];
372
+ c?.length && t((o) => {
373
+ this.traceGroupStart(
374
+ () => ({
375
+ groupId: l.onActionAfter(r.$id),
376
+ result: o
377
+ }),
378
+ r.$id,
379
+ `store:onAction:after:${n}`
380
+ ), c.forEach((u) => {
361
381
  try {
362
- h(o);
363
- } catch (l) {
364
- this.traceError("executeStoreOnActionAfterCallbacks", l, r.$id);
382
+ u(o);
383
+ } catch (h) {
384
+ this.traceError("storeOnActionAfterCallbacks", h, r.$id, () => ({ "action name": n }));
365
385
  }
366
- }), this.traceGroupEnd(() => ({}), r.$id);
386
+ }), this.traceGroupEnd(() => ({ groupId: l.onActionAfter(r.$id) }), r.$id);
367
387
  });
368
388
  }
369
- executeStoreOnActionOnErrorCallbacks(t, e, r) {
370
- const n = this._onActionOnErrorSubscriptions[e];
371
- n?.length && t((c) => {
372
- this.traceGroupStart(() => ({
373
- callbacks: n.length,
374
- error: c
375
- }), r.$id, "execute:store:onAction:onError:callbacks"), n.forEach((o) => {
389
+ executeStoreOnActionOnErrorCallbacks(t, e, r, n) {
390
+ const c = this._onActionOnErrorSubscriptions[e];
391
+ c?.length && t((o) => {
392
+ this.traceGroupStart(
393
+ () => ({
394
+ "action name": n,
395
+ error: o,
396
+ groupId: l.onActionError(r.$id)
397
+ }),
398
+ r.$id,
399
+ `store:onAction:onError:${n}`
400
+ ), c.forEach((u) => {
376
401
  try {
377
- o(c);
402
+ u(o);
378
403
  } catch (h) {
379
- this.traceError("execute:store:onAction:onError:callbacks:error", h, r.$id);
404
+ this.traceError("store:onAction:onError:error", h, r.$id, () => ({ groupId: l.onActionError(r.$id) }));
380
405
  }
381
- }), this.traceGroupEnd(() => ({}), r.$id);
406
+ }), this.traceGroupEnd(() => ({ groupId: l.onActionError(r.$id) }), r.$id);
382
407
  });
383
408
  }
384
409
  executeSubscriber(t, e) {
@@ -397,13 +422,13 @@ class Q extends D {
397
422
  try {
398
423
  const o = e.afterHydration?.(t);
399
424
  o && typeof o.then == "function" && o.catch(
400
- (h) => this.traceError("subscriber:afterHydration", h, t.store.$id)
425
+ (u) => this.traceError("subscriber:afterHydration", u, t.store.$id)
401
426
  );
402
427
  } catch (o) {
403
428
  this.traceError("subscriber:afterHydration", o, t.store.$id);
404
429
  }
405
430
  };
406
- 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);
407
432
  }
408
433
  executeSubscriberSafely(t, e) {
409
434
  try {
@@ -416,31 +441,39 @@ class Q extends D {
416
441
  this._options?.tracerHandler && (this._tracer = this._options.tracerHandler.createTracer(j));
417
442
  }
418
443
  plugin({ store: t, options: e }) {
419
- if (this._subscribers.length) {
420
- this.traceGroupStart(() => ({
421
- environment: this.defineCurrentEnvironment(),
422
- subscribers: this._subscribers,
423
- store: t,
424
- options: e
425
- }), t.$id);
426
- try {
427
- this._subscribers.forEach(
428
- (r) => {
429
- const n = this.defineSubscriberKey(r, t), c = this.definePluginExecution(r);
430
- if (!(this._subscribersDelivered.has(n) || this._subscribersScheduled.has(n)) && this.shouldInvokePlugin(c)) {
431
- if (this.shouldScheduleAfterHydration(c)) {
432
- this.queueSubscriberExecution({ store: t, options: e }, r, n);
433
- return;
434
- }
435
- 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;
436
468
  }
469
+ this.executeSubscriber({ store: t, options: e }, n);
437
470
  }
438
- ), this.rewriteResetStore({ store: t }, t.$state), this._storeRollbackSnapshotsHandler.initFromPluginContext({ store: t, options: e }), this.executeStoreOnActionSubscription(t), this.registerStoreCleanup(t);
439
- } catch (r) {
440
- this.traceError("plugin:invoke", r, t.$id);
441
- }
442
- 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);
443
475
  }
476
+ this.traceGroupEnd(() => ({}), t.$id);
444
477
  }
445
478
  queueSubscriberExecution(t, e, r) {
446
479
  const n = this.defineHydrationScheduler(e);
@@ -455,13 +488,13 @@ class Q extends D {
455
488
  r.__piniaPluginSubscriptionDisposed || (r.__piniaPluginSubscriptionDisposed = !0, t.$dispose = () => (this._disposedStores.add(t), this.clearStoreTracking(t), this._storeRollbackSnapshotsHandler.clearStoreTracking(t), e()));
456
489
  }
457
490
  rewriteResetStore({ store: t }, e) {
458
- const r = u(e);
491
+ const r = d(e);
459
492
  t.$reset = () => {
460
- this.traceGroupStart(() => ({ initialState: r }), t.$id, "store:reset");
493
+ this.traceGroupStart(() => ({ initialState: r, groupId: l.reset(t.$id) }), t.$id, "store:reset");
461
494
  try {
462
- this.executeResetStoreCallbacks(t), t.$patch(u(r));
495
+ this.executeResetStoreCallbacks(t), t.$patch(d(r));
463
496
  } finally {
464
- this.traceGroupEnd(() => ({ initialState: r }), t.$id);
497
+ this.traceGroupEnd(() => ({ initialState: r, groupId: l.reset(t.$id) }), t.$id);
465
498
  }
466
499
  };
467
500
  }
@@ -472,28 +505,40 @@ class Q extends D {
472
505
  shouldScheduleAfterHydration(t) {
473
506
  return this.defineCurrentEnvironment() === "client" && t.hydration === "defer";
474
507
  }
475
- storeOnActionSubscription(t) {
476
- const { store: e, callback: r } = t?.storeOnActionSubscription?.() ?? {};
477
- !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));
478
511
  }
479
- storeMutationSubscription(t) {
480
- const { store: e, callback: r } = t();
481
- e.$subscribe((n) => {
482
- this.traceGroupStart(() => ({ mutation: n }), e.$id, "store:mutation");
483
- try {
484
- r(n);
485
- } finally {
486
- this.traceGroupEnd({}, 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
+ }
487
531
  }
488
532
  });
533
+ this._onMutationUnsubscribes.set(r, c);
489
534
  }
490
535
  subscriptionDelivery(t, e) {
491
536
  Object.entries(e).forEach(([r, n]) => {
492
537
  const c = this.defineSubscriptionKey(r, t.store), o = this.definePluginExecution(n.subscription);
493
538
  if (!(this._subscriptionsDelivered.has(c) || this._subscriptionsScheduled.has(c)) && this.shouldInvokePlugin(o)) {
494
539
  if (this.shouldScheduleAfterHydration(o)) {
495
- const h = this.defineHydrationScheduler(n.subscription);
496
- this._subscriptionsScheduled.add(c), h(() => {
540
+ const u = this.defineHydrationScheduler(n.subscription);
541
+ this._subscriptionsScheduled.add(c), u(() => {
497
542
  this._disposedStores.has(t.store) || (this._subscriptionsScheduled.delete(c), this.subscriptionDeliverySafely(t, r, n));
498
543
  });
499
544
  return;
@@ -506,24 +551,24 @@ class Q extends D {
506
551
  try {
507
552
  if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(r, t)))
508
553
  return;
509
- const { subscription: c, subscriptionOptions: o, stores: h } = n;
554
+ const { subscription: c, subscriptionOptions: o, stores: u } = n;
510
555
  this.trace("Subscription:delivery", t.$id, () => ({
511
556
  pluginName: r,
512
557
  subscription: c,
513
558
  subscriptionOptions: o
514
559
  }));
515
- const l = {
560
+ const h = {
516
561
  storeOptions: { ...e?.storeOptions ?? {}, ...o }
517
562
  };
518
563
  c.invoke(
519
- { store: t, options: { ...e, ...l } }
520
- ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(r, t)), O(h) || h?.forEach(
521
- (d) => c.invoke({
522
- store: d,
564
+ { store: t, options: { ...e, ...h } }
565
+ ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(r, t)), E(u) || u?.forEach(
566
+ (p) => c.invoke({
567
+ store: p,
523
568
  options: {
524
569
  ...e,
525
- ...l,
526
- ...d?.storeOptions ?? {}
570
+ ...h,
571
+ ...p?.storeOptions ?? {}
527
572
  }
528
573
  })
529
574
  );
@@ -532,29 +577,32 @@ class Q extends D {
532
577
  }
533
578
  }
534
579
  traceGroupEnd(t, e) {
535
- this._options?.tracerHandler && this._options.tracerHandler.closeGroup({
580
+ const r = this._options?.tracerHandler;
581
+ r && (r.hasTraceListeners?.() ?? !0) && r.closeGroup({
536
582
  ...typeof t == "function" ? t() : t ?? {},
537
583
  scope: e
538
584
  });
539
585
  }
540
586
  traceGroupStart(t, e, r) {
541
- 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({
542
589
  ...t,
543
590
  scope: e
544
591
  }), this.trace(r ?? "context", e, t));
545
592
  }
546
593
  }
547
594
  export {
548
- Q as P,
549
- D as T,
550
- J as a,
551
- q as b,
552
- z as c,
553
- N as d,
554
- I as g,
555
- P as h,
556
- O as i,
595
+ V as P,
596
+ G as T,
597
+ Q as a,
598
+ z as b,
599
+ B as c,
600
+ q as d,
601
+ U as g,
602
+ v as h,
603
+ E as i,
557
604
  j as p,
558
- B as s
605
+ J as s,
606
+ l as t
559
607
  };
560
- //# sourceMappingURL=PluginSubscription-DPu3ft-c.js.map
608
+ //# sourceMappingURL=PluginSubscription-ZO5mkdvZ.js.map