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

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
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8
8
  ## [Unreleased]
9
9
 
10
10
  ### Added
11
+ - `traceGroups` glossary, exported from `pinia-plugin-subscription/helpers`, building the `groupId` emitted by `PluginSubscription` (`onActionAfter`, `onActionError`, `onMutation`, `reset`); documented in `docs/tracing.md`
12
+ - `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
11
13
  - Added a dedicated tracing guide, `docs/tracing.md`; the README now only presents tracing and links to it
12
14
  - Moved the API reference, import subpaths, development, `Store` summary and testing sections from `README.md` to `docs/authoring-plugins.md`
13
15
  - `createTracerRegistry({ redact })`: opt-in redaction of event payloads before they reach listeners. `true` masks a default list of sensitive keys (`password`, `token`, `secret`, `authorization`, API/private keys, `credential`, `cookie`, `cipher`...); an object adds custom `keys`, a `replacement`, or drops the defaults with `includeDefaults: false`. It works on a copy, covers arrays/`Map`/`Set`, handles circular references, and replaces the payload by `__traceRedactionError` if redaction fails. `defaultRedactedKeys` and `TraceRedactOptions` are exported
package/README.md CHANGED
@@ -61,54 +61,6 @@ app.use(pinia)
61
61
  app.mount('#app')
62
62
  ```
63
63
 
64
- ## Usage — Examples
65
-
66
- **1) Subscriber using a `Store` subclass:**
67
-
68
- ```typescript
69
- import PluginSubscriber from 'pinia-plugin-subscription'
70
- import { Store } from 'pinia-plugin-subscription'
71
-
72
- class MyPlugin extends Store {
73
- protected override _className: string = 'MyPlugin'
74
- protected static override _requiredKeys?: string[] | undefined = ['my-plugin-option']
75
-
76
- constructor(store, options) {
77
- super(store, options)
78
- this.doSomething()
79
- }
80
- }
81
-
82
- class MyPluginSubscriber extends PluginSubscriber<MyPlugin> {
83
- constructor() {
84
- super('my-plugin', MyPlugin.customizeStore.bind(MyPlugin))
85
- }
86
- }
87
-
88
- export const myStoreSubscriber = new MyPluginSubscriber()
89
- ```
90
-
91
- **2) Simple subscriber implementing `PluginSubscriberInterface`:**
92
-
93
- ```typescript
94
- import type { PluginSubscriberInterface } from 'pinia-plugin-subscription'
95
-
96
- export const myStoreSubscriber: PluginSubscriberInterface = {
97
- name: 'my-plugin',
98
-
99
- invoke: (context) => {
100
- // context contains `store`, `options`, `pinia`
101
- console.log('store registered', context.store.$id)
102
-
103
- return true
104
- },
105
-
106
- resetStoreCallback: (store) => {
107
- console.log('store reset:', store.$id)
108
- }
109
- }
110
- ```
111
-
112
64
  ## Advanced Features
113
65
 
114
66
  - **Structured tracing:** Subscribe to typed trace events and filter them at runtime, per store or per namespace — see [Tracing](#tracing).
@@ -0,0 +1,576 @@
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;
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) {
7
+ if (typeof structuredClone == "function")
8
+ try {
9
+ return structuredClone(i);
10
+ } catch {
11
+ }
12
+ if (i === null || typeof i != "object")
13
+ return i;
14
+ if (i instanceof Map)
15
+ return new Map(
16
+ Array.from(i.entries(), ([t, e]) => [u(t), u(e)])
17
+ );
18
+ if (i instanceof Set)
19
+ return new Set(Array.from(i.values(), (t) => u(t)));
20
+ if (Array.isArray(i))
21
+ return i.map((t) => u(t));
22
+ if (Object.getPrototypeOf(i) === Object.prototype || Object.getPrototypeOf(i) === null)
23
+ return Object.fromEntries(
24
+ Object.entries(i).map(([t, e]) => [t, u(e)])
25
+ );
26
+ const s = Object.create(Object.getPrototypeOf(i));
27
+ for (const [t, e] of Object.entries(i))
28
+ s[t] = u(e);
29
+ return s;
30
+ }
31
+ function R(i, s = "all") {
32
+ if (Array.isArray(s)) {
33
+ const t = {};
34
+ for (const e of s)
35
+ e in i && (t[e] = u(k(i[e])));
36
+ return t;
37
+ }
38
+ if (s !== "all")
39
+ throw new Error(`Invalid keys parameter: ${s}`);
40
+ return u(k(i));
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")
45
+ return;
46
+ const t = i.$dispose;
47
+ i.$dispose = (...e) => (A.delete(i), b.delete(s), t.apply(i, e)), $.add(i);
48
+ }
49
+ function N(i, s, t) {
50
+ const e = t ? { storeOptions: t } : {};
51
+ return typeof s == "function" ? g(
52
+ i,
53
+ s,
54
+ e
55
+ ) : v(
56
+ i,
57
+ s,
58
+ e
59
+ );
60
+ }
61
+ function q(i, s, t) {
62
+ const e = t ? { storeOptions: { ...t, enhancedStore: !0 } } : { storeOptions: { enhancedStore: !0 } };
63
+ return g(
64
+ i,
65
+ s,
66
+ e
67
+ );
68
+ }
69
+ function g(i, s, t) {
70
+ const e = {
71
+ id: i,
72
+ extensions: {}
73
+ }, r = t?.storeOptions?.enhancedStore === !0, n = E(
74
+ i,
75
+ () => s(e),
76
+ t
77
+ );
78
+ return Object.assign(((...c) => {
79
+ r && b.set(i, e);
80
+ const o = n(...c);
81
+ return r && (A.set(o, e), b.delete(i), x(o, i)), o;
82
+ }), n);
83
+ }
84
+ function v(i, s, t) {
85
+ return t && (s = { ...s, ...t ?? {} }), E(i, s);
86
+ }
87
+ const P = /* @__PURE__ */ new Set(["_", "$"]);
88
+ function T(i) {
89
+ return P.has(i[0]);
90
+ }
91
+ function z(i) {
92
+ const s = i;
93
+ return A.get(i) ?? (typeof s?.$id == "string" ? b.get(s.$id) : void 0);
94
+ }
95
+ function B(i) {
96
+ const s = i.extensions.enhancedStore ?? i.extensions.extending;
97
+ if (!s)
98
+ throw new Error(`${i.id}Store - getEnhancedStore - Error: enhanced store is required`);
99
+ return s;
100
+ }
101
+ function J(i, s) {
102
+ i.extensions.enhancedStore = s, i.extensions.extending = s;
103
+ }
104
+ function S(i, s) {
105
+ return `${i}-${s.$id}`;
106
+ }
107
+ class D {
108
+ constructor() {
109
+ a(this, "_stateSnapshots", /* @__PURE__ */ new Map());
110
+ a(this, "_storeHasRollbackSnapshots", /* @__PURE__ */ new Set());
111
+ a(this, "_storeRollbackSnapshotsParams", /* @__PURE__ */ new Map());
112
+ }
113
+ addRollbackAction(s) {
114
+ typeof s.$rollbackAfterFailure != "function" && (s.$rollbackAfterFailure = async ({ action: t, stateKeys: e }, r = []) => {
115
+ const n = s[t];
116
+ if (typeof n != "function" || T(t))
117
+ return;
118
+ const c = !e || e.length === 0 ? "all" : e, o = this.saveStateSnapshot(s, t, c);
119
+ try {
120
+ await n.apply(s, r);
121
+ } catch (h) {
122
+ throw this.patchStore(o, s), h;
123
+ } finally {
124
+ this.removeStateSnapshot(o);
125
+ }
126
+ });
127
+ }
128
+ createOnActionCallbacks(s, t) {
129
+ const e = this._storeRollbackSnapshotsParams.get(
130
+ S(s, t)
131
+ );
132
+ if (!e)
133
+ return;
134
+ const r = e.length === 0 ? "all" : e, n = this.saveStateSnapshot(t, s, r);
135
+ return {
136
+ after: () => {
137
+ this.removeStateSnapshot(n);
138
+ },
139
+ onError: () => {
140
+ this.patchStore(n, t), this.removeStateSnapshot(n);
141
+ }
142
+ };
143
+ }
144
+ initFromPluginContext({ options: s, store: t }) {
145
+ const e = s?.storeOptions?.rollbackAfterFailure;
146
+ e && !this._storeHasRollbackSnapshots.has(t.$id) && (Object.keys(e).forEach((r) => {
147
+ const n = e[r];
148
+ n && this._storeRollbackSnapshotsParams.set(
149
+ S(r, t),
150
+ n
151
+ );
152
+ }), this._storeHasRollbackSnapshots.add(t.$id)), this.addRollbackAction(t);
153
+ }
154
+ clearStoreTracking(s) {
155
+ const t = `-${s.$id}`;
156
+ for (const e of Array.from(this._storeRollbackSnapshotsParams.keys()))
157
+ e.endsWith(t) && this._storeRollbackSnapshotsParams.delete(e);
158
+ this._storeHasRollbackSnapshots.delete(s.$id);
159
+ for (const e of Array.from(this._stateSnapshots.keys()))
160
+ e.description?.startsWith(`${s.$id}:`) && this._stateSnapshots.delete(e);
161
+ }
162
+ patchStore(s, t) {
163
+ const e = this._stateSnapshots.get(s);
164
+ if (e)
165
+ try {
166
+ t.$patch(e);
167
+ } catch (r) {
168
+ console.error(`Failed to patch store with snapshot ${String(s)}`, r);
169
+ }
170
+ }
171
+ removeStateSnapshot(s) {
172
+ s && this._stateSnapshots.delete(s);
173
+ }
174
+ saveStateSnapshot(s, t, e) {
175
+ const r = R(
176
+ s.$state,
177
+ e
178
+ ), n = /* @__PURE__ */ Symbol(`${s.$id}:${t}`);
179
+ return this._stateSnapshots.set(n, r), n;
180
+ }
181
+ storeHasRollbackSnapshots(s) {
182
+ return this._storeHasRollbackSnapshots.has(s.$id);
183
+ }
184
+ }
185
+ class G {
186
+ constructor() {
187
+ a(this, "_tracer");
188
+ a(this, "_traceOptions", {
189
+ addClassNameToLabel: !0
190
+ });
191
+ }
192
+ get classNameAddedToTraceNamespace() {
193
+ return this._traceOptions.addClassNameToLabel === !0;
194
+ }
195
+ set classNameAddedToTraceNamespace(s) {
196
+ this._traceOptions.addClassNameToLabel = s;
197
+ }
198
+ get tracer() {
199
+ return this._tracer;
200
+ }
201
+ trace(s, t, e, r) {
202
+ const n = this.tracer;
203
+ n && (s = this.traceLabelFormatter(s), n.debug(s, r ?? s.toLowerCase(), e, t));
204
+ }
205
+ traceError(s, t, e, r, n) {
206
+ const c = this.tracer;
207
+ c ? (s = this.traceLabelFormatter(s), c.error(s, n ?? s.toLowerCase(), t, r, e)) : console.error(t);
208
+ }
209
+ traceLabelFormatter(s) {
210
+ return this.classNameAddedToTraceNamespace ? `${this._className}:${s}` : s;
211
+ }
212
+ }
213
+ function O(i) {
214
+ 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
+ }
216
+ const j = "pinia-plugin-subscription", C = "PluginSubscription", Q = C.toLowerCase(), d = {
217
+ onActionAfter: (i) => `store:onAction:after:${i}`,
218
+ onActionError: (i) => `store:onAction:onError:${i}`,
219
+ onMutation: (i) => `store:onMutation:${i}`,
220
+ reset: (i) => `store:reset:${i}`
221
+ }, M = C, F = {
222
+ environment: "both",
223
+ hydration: "immediate"
224
+ };
225
+ function I(i) {
226
+ if (typeof window < "u" && typeof window.requestAnimationFrame == "function") {
227
+ window.requestAnimationFrame(() => globalThis.setTimeout(i, 0));
228
+ return;
229
+ }
230
+ globalThis.setTimeout(i, 0);
231
+ }
232
+ class U extends G {
233
+ constructor(t, e) {
234
+ super();
235
+ a(this, "_className", M);
236
+ a(this, "_hydrationScheduler");
237
+ a(this, "_onActionSubscriptions", /* @__PURE__ */ new Map());
238
+ a(this, "_onActionStores", /* @__PURE__ */ new Set());
239
+ a(this, "_onActionAfterSubscriptions", {});
240
+ a(this, "_onActionOnErrorSubscriptions", {});
241
+ a(this, "_options");
242
+ a(this, "_resetStoreCallback", /* @__PURE__ */ new Map());
243
+ a(this, "_disposedStores", /* @__PURE__ */ new WeakSet());
244
+ a(this, "_storeRollbackSnapshotsHandler", new D());
245
+ a(this, "_subscribers", []);
246
+ a(this, "_subscribersDelivered", /* @__PURE__ */ new Set());
247
+ a(this, "_subscribersScheduled", /* @__PURE__ */ new Set());
248
+ a(this, "_subscriptions", []);
249
+ a(this, "_subscriptionsDelivered", /* @__PURE__ */ new Set());
250
+ a(this, "_subscriptionsScheduled", /* @__PURE__ */ new Set());
251
+ this._hydrationScheduler = e?.hydrationScheduler ?? I, this._options = e, this._subscribers = t, this.initializeTracer();
252
+ }
253
+ set subscribers(t) {
254
+ this._subscribers = t;
255
+ }
256
+ get subscriptions() {
257
+ if (!O(this._subscriptions))
258
+ return this._subscriptions;
259
+ }
260
+ addOnActionAfterCallback(t, e, r) {
261
+ const n = S(t, e);
262
+ this._onActionAfterSubscriptions[n] || (this._onActionAfterSubscriptions[n] = []), this._onActionAfterSubscriptions[n]?.push(r);
263
+ }
264
+ addOnActionOnErrorCallback(t, e, r) {
265
+ const n = S(t, e);
266
+ this._onActionOnErrorSubscriptions[n] || (this._onActionOnErrorSubscriptions[n] = []), this._onActionOnErrorSubscriptions[n]?.push(r);
267
+ }
268
+ addResetStoreCallback(t, e) {
269
+ this._resetStoreCallback.has(t) || this._resetStoreCallback.set(t, /* @__PURE__ */ new Set()), this._resetStoreCallback.get(t).add(e);
270
+ }
271
+ clearOnActionCallbacksSubscriptions(t) {
272
+ delete this._onActionAfterSubscriptions[t], delete this._onActionOnErrorSubscriptions[t];
273
+ }
274
+ clearStoreTracking(t) {
275
+ this._onActionSubscriptions.delete(t), this._onActionStores.delete(t), this._resetStoreCallback.delete(t);
276
+ const e = `-${t.$id}`;
277
+ for (const r of Array.from(this._subscribersDelivered))
278
+ r.endsWith(e) && this._subscribersDelivered.delete(r);
279
+ for (const r of Array.from(this._subscribersScheduled))
280
+ r.endsWith(e) && this._subscribersScheduled.delete(r);
281
+ for (const r of Array.from(this._subscriptionsDelivered))
282
+ r.endsWith(e) && this._subscriptionsDelivered.delete(r);
283
+ for (const r of Array.from(this._subscriptionsScheduled))
284
+ r.endsWith(e) && this._subscriptionsScheduled.delete(r);
285
+ for (const r of Object.keys(this._onActionAfterSubscriptions))
286
+ r.endsWith(e) && delete this._onActionAfterSubscriptions[r];
287
+ for (const r of Object.keys(this._onActionOnErrorSubscriptions))
288
+ r.endsWith(e) && delete this._onActionOnErrorSubscriptions[r];
289
+ }
290
+ createOnActionAfterCallbackCollector(t, e) {
291
+ return (r) => {
292
+ this.addOnActionAfterCallback(t, e, r);
293
+ };
294
+ }
295
+ createOnActionOnErrorCallbackCollector(t, e) {
296
+ return (r) => {
297
+ this.addOnActionOnErrorCallback(t, e, r);
298
+ };
299
+ }
300
+ createRollbackSnapshot(t, e) {
301
+ const {
302
+ after: r,
303
+ onError: n
304
+ } = this._storeRollbackSnapshotsHandler.createOnActionCallbacks(
305
+ t,
306
+ e
307
+ ) ?? {};
308
+ !r || !n || (this.addOnActionAfterCallback(t, e, r), this.addOnActionOnErrorCallback(t, e, n), this.trace("store:onAction:rollbackSnapshot:created", e.$id));
309
+ }
310
+ defineCurrentEnvironment() {
311
+ return this._options?.runtimeEnvironment ?? (typeof window > "u" ? "server" : "client");
312
+ }
313
+ definePluginExecution(t) {
314
+ return {
315
+ ...F,
316
+ ...this._options?.execution ?? {},
317
+ ...t.execution ?? {},
318
+ ...this._options?.subscriberExecution?.[t.name] ?? {}
319
+ };
320
+ }
321
+ defineHydrationScheduler(t) {
322
+ return t.hydrationScheduler ?? this._hydrationScheduler;
323
+ }
324
+ defineSubscriberKey(t, e) {
325
+ return `${t.name}-${e.$id}`;
326
+ }
327
+ defineSubscriptionKey(t, e) {
328
+ return `${t}-${e.$id}`;
329
+ }
330
+ executeResetStoreCallbacks(t) {
331
+ this._resetStoreCallback.get(t)?.forEach((e) => e(t));
332
+ }
333
+ executeStoreOnActionSubscription(t) {
334
+ if (this._onActionStores.has(t) || typeof t.$onAction != "function")
335
+ return;
336
+ 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)
340
+ 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));
345
+ };
346
+ try {
347
+ const f = S(o, t);
348
+ this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t) && this.createRollbackSnapshot(o, t), l.forEach((m) => m({
349
+ after: this.createOnActionAfterCallbackCollector(o, t),
350
+ args: c,
351
+ name: o,
352
+ 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;
356
+ }
357
+ }));
358
+ }
359
+ executeStoreOnActionAfterCallbacks(t, e, r) {
360
+ const n = this._onActionAfterSubscriptions[e], c = n?.length;
361
+ c && t((o) => {
362
+ this.traceGroupStart(
363
+ () => ({
364
+ callbacks: c,
365
+ groupId: d.onActionAfter(r.$id),
366
+ result: o
367
+ }),
368
+ r.$id,
369
+ "store:onAction:after"
370
+ ), n.forEach((h) => {
371
+ try {
372
+ h(o);
373
+ } catch (l) {
374
+ this.traceError("storeOnActionAfterCallbacks", l, r.$id);
375
+ }
376
+ }), this.traceGroupEnd(() => ({ groupId: d.onActionAfter(r.$id) }), r.$id);
377
+ });
378
+ }
379
+ executeStoreOnActionOnErrorCallbacks(t, e, r) {
380
+ const n = this._onActionOnErrorSubscriptions[e];
381
+ n?.length && t((c) => {
382
+ this.traceGroupStart(
383
+ () => ({
384
+ callbacks: n.length,
385
+ error: c,
386
+ groupId: d.onActionError(r.$id)
387
+ }),
388
+ r.$id,
389
+ "store:onAction:onError"
390
+ ), n.forEach((o) => {
391
+ try {
392
+ o(c);
393
+ } catch (h) {
394
+ this.traceError("store:onAction:onError:error", h, r.$id);
395
+ }
396
+ }), this.traceGroupEnd(() => ({ groupId: d.onActionError(r.$id) }), r.$id);
397
+ });
398
+ }
399
+ executeSubscriber(t, e) {
400
+ const r = this.defineSubscriberKey(e, t.store);
401
+ if (this._subscribersDelivered.has(r) || !e.invoke(t, this._options?.tracerHandler?.createTracer))
402
+ return;
403
+ this._subscribersDelivered.add(r);
404
+ let n;
405
+ try {
406
+ n = e.hydrate?.(t);
407
+ } catch (o) {
408
+ this.traceError("subscriber:hydrate", o, t.store.$id);
409
+ return;
410
+ }
411
+ const c = () => {
412
+ try {
413
+ const o = e.afterHydration?.(t);
414
+ o && typeof o.then == "function" && o.catch(
415
+ (h) => this.traceError("subscriber:afterHydration", h, t.store.$id)
416
+ );
417
+ } catch (o) {
418
+ this.traceError("subscriber:afterHydration", o, t.store.$id);
419
+ }
420
+ };
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);
422
+ }
423
+ executeSubscriberSafely(t, e) {
424
+ try {
425
+ this.executeSubscriber(t, e);
426
+ } catch (r) {
427
+ this.traceError("subscriber:execute", r, t.store.$id);
428
+ }
429
+ }
430
+ initializeTracer() {
431
+ this._options?.tracerHandler && (this._tracer = this._options.tracerHandler.createTracer(j));
432
+ }
433
+ 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);
451
+ }
452
+ }
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);
458
+ }
459
+ }
460
+ queueSubscriberExecution(t, e, r) {
461
+ const n = this.defineHydrationScheduler(e);
462
+ this._subscribersScheduled.add(r), n(() => {
463
+ this._disposedStores.has(t.store) || (this._subscribersScheduled.delete(r), this.executeSubscriberSafely(t, e));
464
+ });
465
+ }
466
+ registerStoreCleanup(t) {
467
+ if (typeof t.$dispose != "function")
468
+ return;
469
+ const e = t.$dispose.bind(t), r = t;
470
+ r.__piniaPluginSubscriptionDisposed || (r.__piniaPluginSubscriptionDisposed = !0, t.$dispose = () => (this._disposedStores.add(t), this.clearStoreTracking(t), this._storeRollbackSnapshotsHandler.clearStoreTracking(t), e()));
471
+ }
472
+ rewriteResetStore({ store: t }, e) {
473
+ const r = u(e);
474
+ t.$reset = () => {
475
+ this.traceGroupStart(() => ({ initialState: r, groupId: d.reset(t.$id) }), t.$id, "store:reset");
476
+ try {
477
+ this.executeResetStoreCallbacks(t), t.$patch(u(r));
478
+ } finally {
479
+ this.traceGroupEnd(() => ({ initialState: r, groupId: d.reset(t.$id) }), t.$id);
480
+ }
481
+ };
482
+ }
483
+ shouldInvokePlugin(t) {
484
+ const e = this.defineCurrentEnvironment();
485
+ return t.environment === "both" || t.environment === e;
486
+ }
487
+ shouldScheduleAfterHydration(t) {
488
+ return this.defineCurrentEnvironment() === "client" && t.hydration === "defer";
489
+ }
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));
493
+ }
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);
502
+ }
503
+ });
504
+ }
505
+ subscriptionDelivery(t, e) {
506
+ Object.entries(e).forEach(([r, n]) => {
507
+ const c = this.defineSubscriptionKey(r, t.store), o = this.definePluginExecution(n.subscription);
508
+ if (!(this._subscriptionsDelivered.has(c) || this._subscriptionsScheduled.has(c)) && this.shouldInvokePlugin(o)) {
509
+ if (this.shouldScheduleAfterHydration(o)) {
510
+ const h = this.defineHydrationScheduler(n.subscription);
511
+ this._subscriptionsScheduled.add(c), h(() => {
512
+ this._disposedStores.has(t.store) || (this._subscriptionsScheduled.delete(c), this.subscriptionDeliverySafely(t, r, n));
513
+ });
514
+ return;
515
+ }
516
+ this.subscriptionDeliverySafely(t, r, n);
517
+ }
518
+ });
519
+ }
520
+ subscriptionDeliverySafely({ store: t, options: e }, r, n) {
521
+ try {
522
+ if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(r, t)))
523
+ return;
524
+ const { subscription: c, subscriptionOptions: o, stores: h } = n;
525
+ this.trace("Subscription:delivery", t.$id, () => ({
526
+ pluginName: r,
527
+ subscription: c,
528
+ subscriptionOptions: o
529
+ }));
530
+ const l = {
531
+ storeOptions: { ...e?.storeOptions ?? {}, ...o }
532
+ };
533
+ c.invoke(
534
+ { store: t, options: { ...e, ...l } }
535
+ ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(r, t)), O(h) || h?.forEach(
536
+ (p) => c.invoke({
537
+ store: p,
538
+ options: {
539
+ ...e,
540
+ ...l,
541
+ ...p?.storeOptions ?? {}
542
+ }
543
+ })
544
+ );
545
+ } catch (c) {
546
+ this.traceError("subscription:delivery", c, t.$id);
547
+ }
548
+ }
549
+ traceGroupEnd(t, e) {
550
+ this._options?.tracerHandler && this._options.tracerHandler.closeGroup({
551
+ ...typeof t == "function" ? t() : t ?? {},
552
+ scope: e
553
+ });
554
+ }
555
+ traceGroupStart(t, e, r) {
556
+ this._options?.tracerHandler && (t = typeof t == "function" ? t() : t ?? {}, this._options.tracerHandler.startGroup({
557
+ ...t,
558
+ scope: e
559
+ }), this.trace(r ?? "context", e, t));
560
+ }
561
+ }
562
+ export {
563
+ U as P,
564
+ G as T,
565
+ Q as a,
566
+ q as b,
567
+ B as c,
568
+ N as d,
569
+ z as g,
570
+ T as h,
571
+ O as i,
572
+ j as p,
573
+ J as s,
574
+ d as t
575
+ };
576
+ //# sourceMappingURL=PluginSubscription-C_a6v8oi.js.map