pinia-plugin-subscription 1.0.0-beta.13 → 1.0.0-beta.14

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +14 -2
  2. package/README.md +15 -4
  3. package/dist/chunks/trace-registry-Cm_2un0x.js +1009 -0
  4. package/dist/chunks/trace-registry-Cm_2un0x.js.map +1 -0
  5. package/dist/chunks/traceGroups-DfxYkUpm.js +105 -0
  6. package/dist/chunks/traceGroups-DfxYkUpm.js.map +1 -0
  7. package/dist/core/PluginSubscription.d.ts +13 -12
  8. package/dist/devtools/components.d.ts +40 -0
  9. package/dist/devtools/index.d.ts +9 -0
  10. package/dist/devtools/model.d.ts +17 -0
  11. package/dist/devtools/styles.d.ts +1 -0
  12. package/dist/devtools.js +377 -0
  13. package/dist/devtools.js.map +1 -0
  14. package/dist/factories/trace-listener.d.ts +6 -7
  15. package/dist/factories/trace-registry.d.ts +1 -1
  16. package/dist/helpers/index.d.ts +2 -2
  17. package/dist/helpers.js +50 -638
  18. package/dist/helpers.js.map +1 -1
  19. package/dist/pinia-plugin-subscription.js +44 -18
  20. package/dist/pinia-plugin-subscription.js.map +1 -1
  21. package/dist/system/TraceJournal.d.ts +6 -0
  22. package/dist/system/Tracer.d.ts +6 -2
  23. package/dist/types/devtools.d.ts +60 -0
  24. package/dist/types/index.d.ts +2 -2
  25. package/dist/types/plugin.d.ts +6 -0
  26. package/dist/types/trace.d.ts +79 -21
  27. package/dist/utils/constants.d.ts +2 -1
  28. package/dist/utils/toSerializable.d.ts +10 -0
  29. package/dist/utils/traceContext.d.ts +7 -0
  30. package/dist/utils/traceGroups.d.ts +11 -7
  31. package/docs/authoring-plugins.md +2 -2
  32. package/docs/tracing.md +119 -28
  33. package/docs/using-plugins.md +17 -12
  34. package/package.json +9 -1
  35. package/dist/chunks/PluginSubscription-Bkqj4uSK.js +0 -662
  36. package/dist/chunks/PluginSubscription-Bkqj4uSK.js.map +0 -1
  37. package/dist/factories/console-trace-listener.d.ts +0 -7
  38. package/dist/system/log/console-group.d.ts +0 -8
  39. package/dist/system/log/console.d.ts +0 -18
  40. package/dist/system/log/index.d.ts +0 -2
  41. package/dist/system/log/log-trace.d.ts +0 -8
  42. package/dist/system/log/print-array.d.ts +0 -7
  43. package/dist/system/log/print-error.d.ts +0 -6
  44. package/dist/system/log/print-object.d.ts +0 -1
  45. package/dist/system/log/print-trace-payload.d.ts +0 -1
  46. package/dist/system/log/print-trace.d.ts +0 -9
  47. package/dist/system/log/trace-console.d.ts +0 -9
  48. package/dist/types/comparison.d.ts +0 -3
  49. package/dist/types/list.d.ts +0 -13
  50. package/dist/types/log.d.ts +0 -8
  51. package/dist/types/resourceId.d.ts +0 -4
  52. package/dist/utils/Performance.d.ts +0 -9
  53. package/dist/utils/object.d.ts +0 -4
@@ -1,662 +0,0 @@
1
- var H = Object.defineProperty;
2
- var R = (i, s, t) => s in i ? H(i, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[s] = t;
3
- var u = (i, s, t) => R(i, typeof s != "symbol" ? s + "" : s, t);
4
- import { toRaw as m } from "vue";
5
- import { defineStore as C } from "pinia";
6
- function l(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]) => [l(t), l(e)])
17
- );
18
- if (i instanceof Set)
19
- return new Set(Array.from(i.values(), (t) => l(t)));
20
- if (Array.isArray(i))
21
- return i.map((t) => l(t));
22
- if (Object.getPrototypeOf(i) === Object.prototype || Object.getPrototypeOf(i) === null)
23
- return Object.fromEntries(
24
- Object.entries(i).map(([t, e]) => [t, l(e)])
25
- );
26
- const s = Object.create(Object.getPrototypeOf(i));
27
- for (const [t, e] of Object.entries(i))
28
- s[t] = l(e);
29
- return s;
30
- }
31
- function G(i, s = "all") {
32
- if (Array.isArray(s)) {
33
- const t = {};
34
- for (const e of s)
35
- e in i && (t[e] = l(m(i[e])));
36
- return t;
37
- }
38
- if (s !== "all")
39
- throw new Error(`Invalid keys parameter: ${s}`);
40
- return l(m(i));
41
- }
42
- const A = /* @__PURE__ */ new WeakMap(), b = /* @__PURE__ */ new Map(), E = /* @__PURE__ */ new WeakSet();
43
- function P(i, s) {
44
- if (E.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)), E.add(i);
48
- }
49
- function B(i, s, t) {
50
- const e = t ? { storeOptions: t } : {};
51
- return typeof s == "function" ? w(
52
- i,
53
- s,
54
- e
55
- ) : x(
56
- i,
57
- s,
58
- e
59
- );
60
- }
61
- function J(i, s, t) {
62
- const e = t ? { storeOptions: { ...t, enhancedStore: !0 } } : { storeOptions: { enhancedStore: !0 } };
63
- return w(
64
- i,
65
- s,
66
- e
67
- );
68
- }
69
- function w(i, s, t) {
70
- let e;
71
- const r = t?.storeOptions?.enhancedStore === !0, n = C(
72
- i,
73
- () => (e = {
74
- id: i,
75
- extensions: {}
76
- }, r && b.set(i, e), s(e)),
77
- t
78
- );
79
- return Object.assign(((...o) => {
80
- const c = n(...o);
81
- return r && e && (A.set(c, e), b.delete(i), P(c, i)), c;
82
- }), n);
83
- }
84
- function x(i, s, t) {
85
- return t && (s = { ...s, ...t ?? {} }), C(i, s);
86
- }
87
- const v = /* @__PURE__ */ new Set(["_", "$"]);
88
- function D(i) {
89
- return v.has(i[0]);
90
- }
91
- function Q(i) {
92
- const s = i;
93
- return A.get(i) ?? (typeof s?.$id == "string" ? b.get(s.$id) : void 0);
94
- }
95
- function V(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 X(i, s) {
102
- i.extensions.enhancedStore = s, i.extensions.extending = s;
103
- }
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}`;
113
- }
114
- class j {
115
- constructor() {
116
- u(this, "_stateSnapshots", /* @__PURE__ */ new Map());
117
- u(this, "_storeHasRollbackSnapshots", /* @__PURE__ */ new Set());
118
- u(this, "_storeRollbackSnapshotsParams", /* @__PURE__ */ new Map());
119
- }
120
- addRollbackAction(s) {
121
- typeof s.$rollbackAfterFailure != "function" && (s.$rollbackAfterFailure = async ({ action: t, stateKeys: e }, r = []) => {
122
- const n = s[t];
123
- if (typeof n != "function" || D(t))
124
- return;
125
- const o = !e || e.length === 0 ? "all" : e, c = this.saveStateSnapshot(s, t, o);
126
- try {
127
- await n.apply(s, r);
128
- } catch (a) {
129
- throw this.patchStore(c, s), a;
130
- } finally {
131
- this.removeStateSnapshot(c);
132
- }
133
- });
134
- }
135
- createOnActionCallbacks(s, t) {
136
- const e = this._storeRollbackSnapshotsParams.get(
137
- f(s, t)
138
- );
139
- if (!e)
140
- return;
141
- const r = e.length === 0 || e[0] === "all" ? "all" : e, n = this.saveStateSnapshot(t, s, r);
142
- return {
143
- after: () => {
144
- this.removeStateSnapshot(n);
145
- },
146
- onError: () => {
147
- this.patchStore(n, t), this.removeStateSnapshot(n);
148
- }
149
- };
150
- }
151
- initFromPluginContext({ options: s, store: t }) {
152
- const e = s?.storeOptions?.rollbackAfterFailure;
153
- e && !this._storeHasRollbackSnapshots.has(t.$id) && (Object.keys(e).forEach((r) => {
154
- const n = e[r];
155
- n && this._storeRollbackSnapshotsParams.set(
156
- f(r, t),
157
- n
158
- );
159
- }), this._storeHasRollbackSnapshots.add(t.$id)), this.addRollbackAction(t);
160
- }
161
- clearStoreTracking(s) {
162
- const t = y(s);
163
- for (const e of Array.from(this._storeRollbackSnapshotsParams.keys()))
164
- e.startsWith(t) && this._storeRollbackSnapshotsParams.delete(e);
165
- this._storeHasRollbackSnapshots.delete(s.$id);
166
- for (const e of Array.from(this._stateSnapshots.keys()))
167
- e.description?.startsWith(t) && this._stateSnapshots.delete(e);
168
- }
169
- patchStore(s, t) {
170
- const e = this._stateSnapshots.get(s);
171
- if (e)
172
- try {
173
- t.$patch(e);
174
- } catch (r) {
175
- console.error(`Failed to patch store with snapshot ${String(s)}`, r);
176
- }
177
- }
178
- removeStateSnapshot(s) {
179
- s && this._stateSnapshots.delete(s);
180
- }
181
- saveStateSnapshot(s, t, e) {
182
- const r = G(
183
- s.$state,
184
- e
185
- ), n = Symbol(f(t, s));
186
- return this._stateSnapshots.set(n, r), n;
187
- }
188
- storeHasRollbackSnapshots(s) {
189
- return this._storeHasRollbackSnapshots.has(s.$id);
190
- }
191
- }
192
- class I {
193
- constructor() {
194
- u(this, "_tracer");
195
- u(this, "_traceOptions", {
196
- addClassNameToLabel: !0
197
- });
198
- }
199
- get classNameAddedToTraceNamespace() {
200
- return this._traceOptions.addClassNameToLabel === !0;
201
- }
202
- set classNameAddedToTraceNamespace(s) {
203
- this._traceOptions.addClassNameToLabel = s;
204
- }
205
- get tracer() {
206
- return this._tracer;
207
- }
208
- trace(s, t, e, r) {
209
- const n = this.tracer;
210
- n && (s = this.traceLabelFormatter(s), n.debug(s, r ?? s.toLowerCase(), e, t));
211
- }
212
- traceError(s, t, e, r, n) {
213
- const o = this.tracer;
214
- o ? (s = this.traceLabelFormatter(s), o.error(s, n ?? s.toLowerCase(), t, r, e)) : console.error(t);
215
- }
216
- traceLabelFormatter(s) {
217
- return this.classNameAddedToTraceNamespace ? `${this._className}:${s}` : s;
218
- }
219
- }
220
- function O(i) {
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");
222
- }
223
- const F = "pinia-plugin-subscription", T = "PluginSubscription", Y = T.toLowerCase(), d = {
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
- }, K = T, W = {
229
- environment: "both",
230
- hydration: "immediate"
231
- }, L = 2500;
232
- function N(i) {
233
- if (typeof window < "u" && typeof window.requestAnimationFrame == "function") {
234
- window.requestAnimationFrame(() => globalThis.setTimeout(i, 0));
235
- return;
236
- }
237
- globalThis.setTimeout(i, 0);
238
- }
239
- class Z extends I {
240
- constructor(t, e) {
241
- super();
242
- u(this, "_className", K);
243
- u(this, "_hydrationScheduler");
244
- u(this, "_onActionSubscriptions", /* @__PURE__ */ new Map());
245
- u(this, "_onActionSubscriptionsTraces", /* @__PURE__ */ new Map());
246
- u(this, "_onActionStores", /* @__PURE__ */ new Set());
247
- u(this, "_onActionAfterSubscriptions", {});
248
- u(this, "_onActionOnErrorSubscriptions", {});
249
- u(this, "_onMutationSubscriptions", /* @__PURE__ */ new Map());
250
- u(this, "_onMutationSubscriptionsTraces", /* @__PURE__ */ new Map());
251
- u(this, "_onMutationUnsubscribes", /* @__PURE__ */ new Map());
252
- u(this, "_options");
253
- u(this, "_resetStoreCallback", /* @__PURE__ */ new Map());
254
- u(this, "_disposedStores", /* @__PURE__ */ new WeakSet());
255
- u(this, "_storeRollbackSnapshotsHandler", new j());
256
- u(this, "_subscribers", []);
257
- u(this, "_subscribersDelivered", /* @__PURE__ */ new Set());
258
- u(this, "_subscribersScheduled", /* @__PURE__ */ new Set());
259
- u(this, "_subscriptions", []);
260
- u(this, "_subscriptionsDelivered", /* @__PURE__ */ new Set());
261
- u(this, "_subscriptionsScheduled", /* @__PURE__ */ new Set());
262
- this._hydrationScheduler = e?.hydrationScheduler ?? N, this._options = e, this._subscribers = t, this.initializeTracer();
263
- }
264
- set subscribers(t) {
265
- this._subscribers = t;
266
- }
267
- get subscriptions() {
268
- if (!O(this._subscriptions))
269
- return this._subscriptions;
270
- }
271
- addOnActionAfterCallback(t, e, r) {
272
- const n = f(t, e);
273
- this._onActionAfterSubscriptions[n] || (this._onActionAfterSubscriptions[n] = []), this._onActionAfterSubscriptions[n]?.push(r);
274
- }
275
- addOnActionOnErrorCallback(t, e, r) {
276
- const n = f(t, e);
277
- this._onActionOnErrorSubscriptions[n] || (this._onActionOnErrorSubscriptions[n] = []), this._onActionOnErrorSubscriptions[n]?.push(r);
278
- }
279
- addResetStoreCallback(t, e) {
280
- this._resetStoreCallback.has(t) || this._resetStoreCallback.set(t, /* @__PURE__ */ new Set()), this._resetStoreCallback.get(t).add(e);
281
- }
282
- clearOnActionCallbacksSubscriptions(t) {
283
- delete this._onActionAfterSubscriptions[t], delete this._onActionOnErrorSubscriptions[t];
284
- }
285
- clearStoreTracking(t) {
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);
288
- for (const r of Array.from(this._subscribersDelivered))
289
- r.startsWith(e) && this._subscribersDelivered.delete(r);
290
- for (const r of Array.from(this._subscribersScheduled))
291
- r.startsWith(e) && this._subscribersScheduled.delete(r);
292
- for (const r of Array.from(this._subscriptionsDelivered))
293
- r.startsWith(e) && this._subscriptionsDelivered.delete(r);
294
- for (const r of Array.from(this._subscriptionsScheduled))
295
- r.startsWith(e) && this._subscriptionsScheduled.delete(r);
296
- for (const r of Object.keys(this._onActionAfterSubscriptions))
297
- r.startsWith(e) && delete this._onActionAfterSubscriptions[r];
298
- for (const r of Object.keys(this._onActionOnErrorSubscriptions))
299
- r.startsWith(e) && delete this._onActionOnErrorSubscriptions[r];
300
- }
301
- createOnActionAfterCallbackCollector(t, e) {
302
- return (r) => {
303
- this.addOnActionAfterCallback(t, e, r);
304
- };
305
- }
306
- createOnActionOnErrorCallbackCollector(t, e) {
307
- return (r) => {
308
- this.addOnActionOnErrorCallback(t, e, r);
309
- };
310
- }
311
- createRollbackSnapshot(t, e) {
312
- const {
313
- after: r,
314
- onError: n
315
- } = this._storeRollbackSnapshotsHandler.createOnActionCallbacks(
316
- t,
317
- e
318
- ) ?? {};
319
- !r || !n || (this.addOnActionAfterCallback(t, e, r), this.addOnActionOnErrorCallback(t, e, n), this.trace("store:onAction:rollbackSnapshot:created", e.$id));
320
- }
321
- defineCurrentEnvironment() {
322
- return this._options?.runtimeEnvironment ?? (typeof window > "u" ? "server" : "client");
323
- }
324
- definePluginExecution(t) {
325
- return {
326
- ...W,
327
- ...this._options?.execution ?? {},
328
- ...t.execution ?? {},
329
- ...this._options?.subscriberExecution?.[t.name] ?? {}
330
- };
331
- }
332
- defineHydrationScheduler(t) {
333
- return t.hydrationScheduler ?? this._hydrationScheduler;
334
- }
335
- defineSubscriberKey(t, e) {
336
- return _(t.name, e);
337
- }
338
- defineSubscriptionKey(t, e) {
339
- return _(t, e);
340
- }
341
- executeResetStoreCallbacks(t) {
342
- this._resetStoreCallback.get(t)?.forEach((e) => e(t));
343
- }
344
- executeStoreOnActionSubscription(t) {
345
- if (this._onActionStores.has(t) || typeof t.$onAction != "function")
346
- return;
347
- const e = this._onActionSubscriptions.get(t), r = this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t);
348
- !e?.size && !r || (this._onActionStores.add(t), t.$onAction(({ after: n, args: o, name: c, onError: a }) => {
349
- const h = this._onActionSubscriptions.get(t);
350
- if (!h?.size)
351
- return;
352
- this.traceGroupStart(() => ({ args: o }), t.$id, `store:onAction:${c}`);
353
- let p = !1;
354
- const k = () => {
355
- p || (p = !0, this.traceGroupEnd({}, t.$id));
356
- };
357
- try {
358
- const S = f(c, t);
359
- this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t) && this.createRollbackSnapshot(c, t);
360
- const $ = [];
361
- h.forEach((M) => {
362
- const g = this.runCallbackSafely(
363
- () => M({
364
- after: this.createOnActionAfterCallbackCollector(c, t),
365
- args: o,
366
- name: c,
367
- onError: this.createOnActionOnErrorCallbackCollector(c, t)
368
- }),
369
- "store:onAction:callback:error",
370
- t.$id
371
- );
372
- g && $.push(g);
373
- }), this.endGroupAfter($, k), this.executeStoreOnActionAfterCallbacks(n, S, t, c), this.executeStoreOnActionOnErrorCallbacks(a, S, t, c), this.clearOnActionCallbacksSubscriptions(S);
374
- } catch (S) {
375
- throw k(), S;
376
- }
377
- }));
378
- }
379
- runCallbackSafely(t, e, r, n) {
380
- try {
381
- const o = t();
382
- if (o && typeof o.then == "function")
383
- return Promise.resolve(o).then(
384
- () => {
385
- },
386
- (c) => this.traceError(e, c, r, n)
387
- );
388
- } catch (o) {
389
- this.traceError(e, o, r, n);
390
- }
391
- }
392
- endGroupAfter(t, e) {
393
- const r = t.filter((n) => !!n);
394
- r.length ? Promise.all(r).then(e) : e();
395
- }
396
- executeStoreOnActionAfterCallbacks(t, e, r, n) {
397
- const o = this._onActionAfterSubscriptions[e];
398
- o?.length && t((c) => {
399
- this.traceGroupStart(
400
- () => ({
401
- groupId: d.onActionAfter(r.$id),
402
- result: c
403
- }),
404
- r.$id,
405
- `store:onAction:after:${n}`
406
- );
407
- const a = o.map((h) => this.runCallbackSafely(
408
- () => h(c),
409
- "storeOnActionAfterCallbacks",
410
- r.$id,
411
- () => ({ "action name": n })
412
- ));
413
- this.endGroupAfter(a, () => this.traceGroupEnd(() => ({ groupId: d.onActionAfter(r.$id) }), r.$id));
414
- });
415
- }
416
- executeStoreOnActionOnErrorCallbacks(t, e, r, n) {
417
- const o = this._onActionOnErrorSubscriptions[e];
418
- o?.length && t((c) => {
419
- this.traceGroupStart(
420
- () => ({
421
- "action name": n,
422
- error: c,
423
- groupId: d.onActionError(r.$id)
424
- }),
425
- r.$id,
426
- `store:onAction:onError:${n}`
427
- );
428
- const a = o.map((h) => this.runCallbackSafely(
429
- () => h(c),
430
- "store:onAction:onError:error",
431
- r.$id,
432
- () => ({ groupId: d.onActionError(r.$id) })
433
- ));
434
- this.endGroupAfter(a, () => this.traceGroupEnd(() => ({ groupId: d.onActionError(r.$id) }), r.$id));
435
- });
436
- }
437
- executeSubscriber(t, e) {
438
- const r = this.defineSubscriberKey(e, t.store);
439
- if (this._subscribersDelivered.has(r) || !e.invoke(t, this._options?.tracerHandler?.createTracer))
440
- return;
441
- this._subscribersDelivered.add(r);
442
- let n;
443
- try {
444
- n = e.hydrate?.(t);
445
- } catch (a) {
446
- this.traceError("subscriber:hydrate", a, t.store.$id);
447
- return;
448
- }
449
- const o = () => {
450
- try {
451
- const a = e.afterHydration?.(t);
452
- if (a && typeof a.then == "function")
453
- return a.catch(
454
- (h) => this.traceError("subscriber:afterHydration", h, t.store.$id)
455
- );
456
- } catch (a) {
457
- this.traceError("subscriber:afterHydration", a, t.store.$id);
458
- }
459
- };
460
- let c;
461
- return n && typeof n.then == "function" ? c = n.then(() => o()).catch((a) => this.traceError("subscriber:hydrate", a, t.store.$id)) : c = o(), 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), c;
462
- }
463
- executeSubscriberSafely(t, e) {
464
- try {
465
- return this.executeSubscriber(t, e);
466
- } catch (r) {
467
- this.traceError("subscriber:execute", r, t.store.$id);
468
- }
469
- }
470
- initializeTracer() {
471
- this._options?.tracerHandler && (this._tracer = this._options.tracerHandler.createTracer(F));
472
- }
473
- plugin({ store: t, options: e }) {
474
- if (!this._subscribers.length)
475
- return;
476
- const r = {
477
- $onAction: [],
478
- $subscribe: []
479
- };
480
- this.traceGroupStart(() => ({
481
- environment: {
482
- current: this.defineCurrentEnvironment(),
483
- ...this._options?.execution ?? {}
484
- },
485
- subscribers: this._subscribers,
486
- "hooks subscriptions": r,
487
- store: t,
488
- options: e
489
- }), t.$id), this._onActionSubscriptionsTraces.set(t.$id, r.$onAction), this._onMutationSubscriptionsTraces.set(t.$id, r.$subscribe);
490
- try {
491
- this._subscribers.forEach(
492
- (n) => {
493
- const o = this.defineSubscriberKey(n, t), c = this.definePluginExecution(n);
494
- if (!(this._subscribersDelivered.has(o) || this._subscribersScheduled.has(o)) && this.shouldInvokePlugin(c)) {
495
- if (this.shouldScheduleAfterHydration(c)) {
496
- this.queueSubscriberExecution({ store: t, options: e }, n, o);
497
- return;
498
- }
499
- this.executeSubscriber({ store: t, options: e }, n);
500
- }
501
- }
502
- ), this.rewriteResetStore({ store: t }, t.$state), this._storeRollbackSnapshotsHandler.initFromPluginContext({ store: t, options: e }), this.executeStoreOnActionSubscription(t), this.registerStoreCleanup(t);
503
- } catch (n) {
504
- this.traceError("plugin:invoke", n, t.$id);
505
- }
506
- this.traceGroupEnd(() => ({}), t.$id);
507
- }
508
- queueSubscriberExecution(t, e, r) {
509
- const n = this.defineHydrationScheduler(e);
510
- this._subscribersScheduled.add(r), n(() => {
511
- if (this._disposedStores.has(t.store))
512
- return;
513
- this._subscribersScheduled.delete(r);
514
- const o = t.store.$id;
515
- this.traceGroupStart(() => ({ subscriber: e.name }), o, "subscriber:deferred");
516
- const c = () => this.traceGroupEnd(() => ({}), o);
517
- let a;
518
- try {
519
- a = this.executeSubscriberSafely(t, e);
520
- } finally {
521
- a ? this.closeGroupWhenSettled(a, c, o, e.name) : c();
522
- }
523
- });
524
- }
525
- closeGroupWhenSettled(t, e, r, n) {
526
- let o = !1;
527
- const c = () => {
528
- o || (o = !0, globalThis.clearTimeout(h), e());
529
- }, a = this._options?.deferredTraceGroupTimeout ?? L, h = globalThis.setTimeout(() => {
530
- o || (this.traceError(
531
- "subscriber:deferred:timeout",
532
- new Error(`Subscriber "${n}" hydration still pending after ${a}ms`),
533
- r
534
- ), c());
535
- }, a);
536
- t.then(c, c);
537
- }
538
- registerStoreCleanup(t) {
539
- if (typeof t.$dispose != "function")
540
- return;
541
- const e = t.$dispose.bind(t), r = t;
542
- r.__piniaPluginSubscriptionDisposed || (r.__piniaPluginSubscriptionDisposed = !0, t.$dispose = () => (this._disposedStores.add(t), this.clearStoreTracking(t), this._storeRollbackSnapshotsHandler.clearStoreTracking(t), e()));
543
- }
544
- rewriteResetStore({ store: t }, e) {
545
- const r = l(e);
546
- t.$reset = () => {
547
- this.traceGroupStart(() => ({ initialState: r, groupId: d.reset(t.$id) }), t.$id, "store:reset");
548
- try {
549
- this.executeResetStoreCallbacks(t), t.$patch(l(r));
550
- } finally {
551
- this.traceGroupEnd(() => ({ initialState: r, groupId: d.reset(t.$id) }), t.$id);
552
- }
553
- };
554
- }
555
- shouldInvokePlugin(t) {
556
- const e = this.defineCurrentEnvironment();
557
- return t.environment === "both" || t.environment === e;
558
- }
559
- shouldScheduleAfterHydration(t) {
560
- return this.defineCurrentEnvironment() === "client" && t.hydration === "defer";
561
- }
562
- storeOnActionSubscription(t, e) {
563
- const { store: r, callback: n } = t?.storeOnActionSubscription?.(e) ?? {};
564
- !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));
565
- }
566
- storeMutationSubscription(t, e) {
567
- const { store: r, callback: n } = e();
568
- 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)))
569
- return;
570
- const o = r.$subscribe(async (c) => {
571
- const a = this._onMutationSubscriptions.get(r);
572
- if (a?.size) {
573
- this.traceGroupStart(() => ({ mutation: c, groupId: d.onMutation(r.$id) }), r.$id, "store:onMutation");
574
- try {
575
- await Promise.all([...a].map(async (h) => {
576
- try {
577
- await h(c);
578
- } catch (p) {
579
- this.traceError("store:onMutation", p, r.$id);
580
- }
581
- }));
582
- } finally {
583
- this.traceGroupEnd(() => ({ mutation: c, groupId: d.onMutation(r.$id) }), r.$id);
584
- }
585
- }
586
- });
587
- this._onMutationUnsubscribes.set(r, o);
588
- }
589
- subscriptionDelivery(t, e) {
590
- Object.entries(e).forEach(([r, n]) => {
591
- const o = this.defineSubscriptionKey(r, t.store), c = this.definePluginExecution(n.subscription);
592
- if (!(this._subscriptionsDelivered.has(o) || this._subscriptionsScheduled.has(o)) && this.shouldInvokePlugin(c)) {
593
- if (this.shouldScheduleAfterHydration(c)) {
594
- const a = this.defineHydrationScheduler(n.subscription);
595
- this._subscriptionsScheduled.add(o), a(() => {
596
- this._disposedStores.has(t.store) || (this._subscriptionsScheduled.delete(o), this.subscriptionDeliverySafely(t, r, n));
597
- });
598
- return;
599
- }
600
- this.subscriptionDeliverySafely(t, r, n);
601
- }
602
- });
603
- }
604
- subscriptionDeliverySafely({ store: t, options: e }, r, n) {
605
- try {
606
- if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(r, t)))
607
- return;
608
- const { subscription: o, subscriptionOptions: c, stores: a } = n;
609
- this.trace("Subscription:delivery", t.$id, () => ({
610
- pluginName: r,
611
- subscription: o,
612
- subscriptionOptions: c
613
- }));
614
- const h = {
615
- storeOptions: { ...e?.storeOptions ?? {}, ...c }
616
- };
617
- o.invoke(
618
- { store: t, options: { ...e, ...h } }
619
- ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(r, t)), O(a) || a?.forEach(
620
- (p) => o.invoke({
621
- store: p,
622
- options: {
623
- ...e,
624
- ...h,
625
- ...p?.storeOptions ?? {}
626
- }
627
- })
628
- );
629
- } catch (o) {
630
- this.traceError("subscription:delivery", o, t.$id);
631
- }
632
- }
633
- traceGroupEnd(t, e) {
634
- const r = this._options?.tracerHandler;
635
- r && (r.hasTraceListeners?.() ?? !0) && r.closeGroup({
636
- ...typeof t == "function" ? t() : t ?? {},
637
- scope: e
638
- });
639
- }
640
- traceGroupStart(t, e, r) {
641
- const n = this._options?.tracerHandler;
642
- n && (n.hasTraceListeners?.() ?? !0) && (t = typeof t == "function" ? t() : t ?? {}, n.startGroup({
643
- ...t,
644
- scope: e
645
- }), this.trace(r ?? "context", e, t));
646
- }
647
- }
648
- export {
649
- Z as P,
650
- I as T,
651
- Y as a,
652
- J as b,
653
- V as c,
654
- B as d,
655
- Q as g,
656
- D as h,
657
- O as i,
658
- F as p,
659
- X as s,
660
- d as t
661
- };
662
- //# sourceMappingURL=PluginSubscription-Bkqj4uSK.js.map