pinia-plugin-subscription 1.0.0-beta.2 → 1.0.0-beta.20

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 (77) hide show
  1. package/CHANGELOG.md +73 -12
  2. package/CONTRIBUTING.md +10 -1
  3. package/README.md +48 -293
  4. package/SECURITY.md +6 -6
  5. package/dist/chunks/setup-context-3G3Hy1ep.js +909 -0
  6. package/dist/dictionaries/plugin-subscription-event.d.ts +20 -0
  7. package/dist/{core/Store.d.ts → enhanced-store/EnhancedStore.d.ts} +24 -29
  8. package/dist/enhanced-store/define-a-store.d.ts +64 -0
  9. package/dist/enhanced-store/setup-context.d.ts +27 -0
  10. package/dist/events/EventsEmitter.d.ts +9 -0
  11. package/dist/events/EventsSubscriber.d.ts +32 -0
  12. package/dist/events/EventsSubscription.d.ts +13 -0
  13. package/dist/events/StoreEvent.d.ts +6 -0
  14. package/dist/events/StoresEventsSubscriptionsRegistry.d.ts +31 -0
  15. package/dist/events/event.d.ts +8 -0
  16. package/dist/factories/events-registry.d.ts +9 -0
  17. package/dist/factories/events-subscriber.d.ts +11 -0
  18. package/dist/factories/messenger-registry.d.ts +16 -0
  19. package/dist/factories/plugin-subscriber.d.ts +17 -0
  20. package/dist/helpers.js +204 -312
  21. package/dist/lib/helpers.d.ts +9 -0
  22. package/dist/lib/main.d.ts +3 -12
  23. package/dist/orchestration/ExecutionPolicy.d.ts +15 -0
  24. package/dist/orchestration/PluginOrchestrator.d.ts +41 -0
  25. package/dist/orchestration/SubscriberScheduler.d.ts +31 -0
  26. package/dist/pinia-plugin-subscription.js +31 -426
  27. package/dist/plugins/createPlugin.d.ts +3 -13
  28. package/dist/services/messenger.d.ts +28 -0
  29. package/dist/services/messengerProvider.d.ts +45 -0
  30. package/dist/store-hooks/StoreHooksBinder.d.ts +20 -0
  31. package/dist/store-hooks/StoreHooksRegistry.d.ts +34 -0
  32. package/dist/{core/PluginSubscriber.d.ts → subscribers/StoreSubscriber.d.ts} +39 -28
  33. package/dist/tracing/TraceGroupHelper.d.ts +27 -0
  34. package/dist/tracing/TraceableEmitter.d.ts +60 -0
  35. package/dist/tracing/TraceableEventsSubscriber.d.ts +21 -0
  36. package/dist/tracing/TraceableStoreEventsSubscriber.d.ts +16 -0
  37. package/dist/tracing/trace-groups.d.ts +9 -0
  38. package/dist/types/core.d.ts +12 -0
  39. package/dist/types/events.d.ts +24 -0
  40. package/dist/types/index.d.ts +8 -34
  41. package/dist/types/messenger.d.ts +20 -0
  42. package/dist/types/plugin.d.ts +39 -20
  43. package/dist/types/store.d.ts +1 -9
  44. package/dist/types/trace.d.ts +35 -75
  45. package/dist/utils/constants.d.ts +4 -0
  46. package/dist/utils/events.d.ts +3 -0
  47. package/dist/utils/id.d.ts +20 -0
  48. package/dist/utils/store.d.ts +9 -93
  49. package/docs/authoring-plugins.md +330 -78
  50. package/docs/using-plugins.md +89 -53
  51. package/package.json +14 -8
  52. package/dist/chunks/constantes-CaTNAbzX.js +0 -261
  53. package/dist/chunks/constantes-CaTNAbzX.js.map +0 -1
  54. package/dist/core/PluginSubscription.d.ts +0 -92
  55. package/dist/core/RollbackStateSnapshotsHandler.d.ts +0 -65
  56. package/dist/factories/console-trace-listener.d.ts +0 -7
  57. package/dist/factories/state-snapshot.d.ts +0 -3
  58. package/dist/factories/trace-listener.d.ts +0 -16
  59. package/dist/factories/trace-registry.d.ts +0 -17
  60. package/dist/helpers/index.d.ts +0 -9
  61. package/dist/helpers.js.map +0 -1
  62. package/dist/pinia-plugin-subscription.js.map +0 -1
  63. package/dist/plugins/createHydrationPlugin.d.ts +0 -10
  64. package/dist/system/Tracer.d.ts +0 -20
  65. package/dist/system/log.d.ts +0 -35
  66. package/dist/tests/defineAStore.test-d.d.ts +0 -1
  67. package/dist/tests/storeOptionsExtensions.test-d.d.ts +0 -12
  68. package/dist/types/comparison.d.ts +0 -3
  69. package/dist/types/list.d.ts +0 -13
  70. package/dist/types/log.d.ts +0 -8
  71. package/dist/types/resourceId.d.ts +0 -4
  72. package/dist/utils/Performance.d.ts +0 -9
  73. package/dist/utils/constantes.d.ts +0 -1
  74. package/dist/utils/deep-clone.d.ts +0 -7
  75. package/dist/utils/defineStoreId.d.ts +0 -1
  76. package/dist/utils/object.d.ts +0 -4
  77. package/dist/utils/validation/object.d.ts +0 -9
@@ -0,0 +1,909 @@
1
+ var B = Object.defineProperty;
2
+ var z = (s, e, t) => e in s ? B(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
3
+ var u = (s, e, t) => z(s, typeof e != "symbol" ? e + "" : e, t);
4
+ import { defineStore as j } from "pinia";
5
+ class X {
6
+ constructor(e, { payload: t, scope: r, source: i } = {}) {
7
+ u(this, "name");
8
+ u(this, "payload");
9
+ u(this, "scope");
10
+ u(this, "source");
11
+ this.name = e, this.payload = t, this.scope = r, this.source = i;
12
+ }
13
+ }
14
+ class U extends X {
15
+ constructor(t, { payload: r, scope: i, source: n, instanceId: o } = {}) {
16
+ super(t, { payload: r, scope: i, source: n });
17
+ u(this, "instanceId");
18
+ this.instanceId = o;
19
+ }
20
+ }
21
+ class V extends U {
22
+ constructor(t, r, i) {
23
+ super(t, i);
24
+ u(this, "_consumer");
25
+ this._consumer = r;
26
+ }
27
+ consume(t) {
28
+ const r = (i) => console.error("[EventSubscription] Error consuming event:", i);
29
+ try {
30
+ const i = this._consumer(t);
31
+ i !== null && (typeof i == "object" || typeof i == "function") && "then" in i && typeof i.then == "function" && Promise.resolve(i).catch(r);
32
+ } catch (i) {
33
+ r(i);
34
+ }
35
+ }
36
+ subscribed(t) {
37
+ return this.name === t.name && ["payload", "scope", "source", "instanceId"].every(
38
+ (r) => !this[r] || t[r] && t[r] === this[r]
39
+ );
40
+ }
41
+ }
42
+ const H = /* @__PURE__ */ new Map(), C = /* @__PURE__ */ new WeakMap();
43
+ function K(s) {
44
+ const e = (H.get(s) ?? 0) + 1;
45
+ return H.set(s, e), `${s}-${e}`;
46
+ }
47
+ function J(s, e) {
48
+ return C.has(e) || C.set(e, K(s)), C.get(e);
49
+ }
50
+ const Q = /* @__PURE__ */ new Set(["_", "$"]);
51
+ function Te(s) {
52
+ return Q.has(s[0]);
53
+ }
54
+ function F(s) {
55
+ return J(s.$id, s);
56
+ }
57
+ function ke(s, e) {
58
+ return Y(s, e);
59
+ }
60
+ function W(s) {
61
+ const e = String(s.$id);
62
+ return `${e.length}:${e}:`;
63
+ }
64
+ function Y(s, e) {
65
+ return `${W(e)}${s.length}:${s}`;
66
+ }
67
+ function Z(s) {
68
+ return W({ $id: F(s) });
69
+ }
70
+ function _(s, e) {
71
+ return `${Z(e)}${s.length}:${s}`;
72
+ }
73
+ class ee {
74
+ constructor({ prefixLabelWithName: e } = { prefixLabelWithName: !1 }) {
75
+ u(this, "_eventsRegistry");
76
+ u(this, "traceOptions", { prefixLabelWithName: !1 });
77
+ u(this, "_tracer");
78
+ this.traceOptions.prefixLabelWithName = e ?? !1;
79
+ }
80
+ get name() {
81
+ return this._name;
82
+ }
83
+ get tracer() {
84
+ return this._tracer;
85
+ }
86
+ emit(e, t, r) {
87
+ this.storeHasEventSubscription(e, t) && this._eventsRegistry?.emitter?.emit(new U(t, {
88
+ payload: r,
89
+ source: this.name,
90
+ scope: e.$id,
91
+ instanceId: F(e)
92
+ }));
93
+ }
94
+ storeHasEventSubscription(e, t) {
95
+ return this._eventsRegistry?.subscriptionsRegistry.has(e, t) ?? !1;
96
+ }
97
+ trace(e, t, r, i) {
98
+ this.tracer && (e = this.traceLabelFormatter(e), this.tracer.debug(e, i ?? e.toLowerCase(), r, t));
99
+ }
100
+ traceError(e, t, r, i, n) {
101
+ this.tracer ? (e = this.traceLabelFormatter(e), this.tracer.error(e, n ?? e.toLowerCase(), t, i, r)) : console.error(t);
102
+ }
103
+ traceLabelFormatter(e) {
104
+ return this.traceOptions.prefixLabelWithName && (e = `${this._name}:${e}`), e;
105
+ }
106
+ }
107
+ function x(s) {
108
+ return s.join(":");
109
+ }
110
+ function R(s) {
111
+ return x(["plugin", ...s]);
112
+ }
113
+ function f(s) {
114
+ return x(["store", ...s]);
115
+ }
116
+ function te(s) {
117
+ return x(["subscriber", ...s]);
118
+ }
119
+ const re = f(["action", "after"]), se = f(["action", "before"]), ie = f(["action", "error"]), ne = f(["action", "onError"]), T = {
120
+ EXECUTION: {
121
+ END: R(["execution", "end"]),
122
+ ERROR: R(["execution", "error"]),
123
+ START: R(["execution", "start"])
124
+ }
125
+ }, g = {
126
+ ACTION_AFTER: re,
127
+ ACTION_BEFORE: se,
128
+ ACTION_ERROR: ie,
129
+ ACTION_ON_ERROR: ne,
130
+ DISPOSE: f(["dispose"]),
131
+ MUTATION: f(["mutation"]),
132
+ RESET: f(["reset"])
133
+ }, oe = {
134
+ INVOKED: te(["invoked"])
135
+ };
136
+ function q(s) {
137
+ return s ? Array.isArray(s) ? s.length === 0 : typeof s == "object" ? Object.keys(s).length === 0 : typeof s == "string" ? s.trim() === "" : !1 : !(typeof s == "boolean" || typeof s == "number");
138
+ }
139
+ const ce = "pinia-plugin-subscription";
140
+ class L {
141
+ constructor(e, t) {
142
+ u(this, "_messenger");
143
+ u(this, "_messengerChannel");
144
+ this._messenger = e, this._messengerChannel = t;
145
+ }
146
+ get messenger() {
147
+ return this._messenger;
148
+ }
149
+ get messengerChannel() {
150
+ return this._messengerChannel;
151
+ }
152
+ }
153
+ class ue extends L {
154
+ emit(e) {
155
+ this.messenger.send(this._messengerChannel, e);
156
+ }
157
+ }
158
+ class ae extends L {
159
+ constructor(t, r, i) {
160
+ super(t, r);
161
+ u(this, "_id");
162
+ this._id = i, this.subscribe();
163
+ }
164
+ get id() {
165
+ return this._id;
166
+ }
167
+ clear() {
168
+ }
169
+ receive(t) {
170
+ this.isCustomer(t) && this.consume(t);
171
+ }
172
+ subscribe() {
173
+ this.messenger.addSubscriber(this._messengerChannel, this);
174
+ }
175
+ unsubscribe() {
176
+ try {
177
+ this.messenger.removeSubscriber(this._messengerChannel, this), this.clear();
178
+ } catch (t) {
179
+ console.error("[MessengerSubscriber] Error unsubscribing:", t);
180
+ }
181
+ }
182
+ }
183
+ class he extends ae {
184
+ constructor(t, r) {
185
+ super(t, "events", r);
186
+ u(this, "_subscriptions", []);
187
+ u(this, "_subscriptionsToConsume", []);
188
+ }
189
+ set subscriptions(t) {
190
+ this._subscriptions = t;
191
+ }
192
+ addSubscription(t, r, i) {
193
+ try {
194
+ const n = new V(t, r, i);
195
+ this._subscriptions.push(n);
196
+ } catch (n) {
197
+ console.error("[EventsSubscriber] Error adding subscription:", n);
198
+ }
199
+ }
200
+ clear() {
201
+ this._subscriptions = [], this._subscriptionsToConsume = [];
202
+ }
203
+ consume(t) {
204
+ try {
205
+ this._subscriptionsToConsume.forEach((r) => r.consume(t));
206
+ } catch (r) {
207
+ console.error("[EventsSubscriber] Error consuming event:", r);
208
+ }
209
+ this._subscriptionsToConsume = [];
210
+ }
211
+ isCustomer(t) {
212
+ return this.initSubscriptionsToConsume(t), this._subscriptionsToConsume.length > 0;
213
+ }
214
+ initSubscriptionsToConsume(t) {
215
+ this._subscriptionsToConsume = this._subscriptions.filter((r) => {
216
+ try {
217
+ return r.subscribed(t);
218
+ } catch (i) {
219
+ return console.error("[EventsSubscriber] Error checking subscription:", i), !1;
220
+ }
221
+ });
222
+ }
223
+ }
224
+ function de(s, e, t) {
225
+ const r = new he(s, e);
226
+ return t && (r.subscriptions = t), r;
227
+ }
228
+ class pe {
229
+ constructor() {
230
+ u(this, "_subscribers", /* @__PURE__ */ new Map());
231
+ }
232
+ get subscribers() {
233
+ return this._subscribers;
234
+ }
235
+ addSubscriber(e, t) {
236
+ this._subscribers.has(e) || this._subscribers.set(e, []), this._subscribers.get(e).push(t);
237
+ }
238
+ clearSubscribers() {
239
+ this._subscribers.forEach((e) => {
240
+ e.forEach((t) => t.clear());
241
+ }), this._subscribers = /* @__PURE__ */ new Map();
242
+ }
243
+ send(e, t) {
244
+ this._subscribers.has(e) && this._subscribers.get(e).forEach((r) => r.receive(t));
245
+ }
246
+ removeSubscriber(e, t) {
247
+ this._subscribers.has(e) && this._subscribers.set(
248
+ e,
249
+ this._subscribers.get(e).filter((r) => r.id !== t.id)
250
+ );
251
+ }
252
+ }
253
+ function le(s, e, t) {
254
+ const r = new pe();
255
+ return {
256
+ emitter: new t(r, s),
257
+ createSubscriber: e(r)
258
+ };
259
+ }
260
+ class be extends ue {
261
+ constructor(e) {
262
+ super(e, "events");
263
+ }
264
+ }
265
+ class ge {
266
+ constructor() {
267
+ u(this, "subscriptions", /* @__PURE__ */ new Map());
268
+ }
269
+ add(e, t) {
270
+ this.subscriptions.has(e) || this.subscriptions.set(e, /* @__PURE__ */ new Set()), this.subscriptions.get(e).add(t);
271
+ }
272
+ clear(e) {
273
+ e ? this.subscriptions.delete(e) : this.subscriptions.clear();
274
+ }
275
+ has(e, t) {
276
+ return this.subscriptions.has(e) && this.subscriptions.get(e).has(t);
277
+ }
278
+ remove(e, t) {
279
+ this.subscriptions.has(e) && (this.subscriptions.get(e).delete(t), this.subscriptions.get(e).size === 0 && this.subscriptions.delete(e));
280
+ }
281
+ }
282
+ function fe() {
283
+ return {
284
+ ...le(
285
+ "events",
286
+ (s) => (e, t) => de(
287
+ s,
288
+ K(e),
289
+ t
290
+ ),
291
+ be
292
+ ),
293
+ subscriptionsRegistry: new ge()
294
+ };
295
+ }
296
+ const k = (s, e, t) => `${s}/onAction:${e}#${t}`, N = (s) => `${s}/plugin`, p = {
297
+ onAction: k,
298
+ onActionAfter: (s, e, t) => `${k(s, e, t)}/after`,
299
+ onActionError: (s, e, t) => `${k(s, e, t)}/onError`,
300
+ onMutation: (s, e) => `${s}/onMutation#${e}`,
301
+ plugin: N,
302
+ pluginDeferred: (s, e, t) => `${N(s)}/deferred:${e}#${t}`,
303
+ reset: (s, e) => `${s}/reset#${e}`
304
+ }, _e = {
305
+ environment: "both",
306
+ hydration: "immediate",
307
+ order: 9
308
+ };
309
+ function Se(s) {
310
+ if (typeof window < "u" && typeof window.requestAnimationFrame == "function") {
311
+ window.requestAnimationFrame(() => globalThis.setTimeout(s, 0));
312
+ return;
313
+ }
314
+ globalThis.setTimeout(s, 0);
315
+ }
316
+ class ye {
317
+ constructor(e) {
318
+ u(this, "_hydrationScheduler");
319
+ u(this, "_options");
320
+ this._options = e, this._hydrationScheduler = e?.hydrationScheduler ?? Se;
321
+ }
322
+ get currentEnvironment() {
323
+ return this._options?.runtimeEnvironment ?? (typeof window > "u" ? "server" : "client");
324
+ }
325
+ get hydrationScheduler() {
326
+ return this._hydrationScheduler;
327
+ }
328
+ resolveExecution(e) {
329
+ return {
330
+ ..._e,
331
+ ...this._options?.execution ?? {},
332
+ ...e.execution ?? {},
333
+ ...this._options?.subscriberExecution?.[e.name] ?? {}
334
+ };
335
+ }
336
+ resolveHydrationScheduler(e) {
337
+ return e.hydrationScheduler ?? this._hydrationScheduler;
338
+ }
339
+ shouldInvoke(e) {
340
+ return e.environment === "both" || e.environment === this.currentEnvironment;
341
+ }
342
+ shouldScheduleAfterHydration(e) {
343
+ return this.currentEnvironment === "client" && e.hydration === "defer";
344
+ }
345
+ }
346
+ class Ee {
347
+ constructor(e, t, r, i) {
348
+ u(this, "_events");
349
+ u(this, "_groups");
350
+ u(this, "_registry");
351
+ u(this, "_reporter");
352
+ this._registry = e, this._groups = t, this._events = r, this._reporter = i;
353
+ }
354
+ hookStoreMutations(e) {
355
+ if (this._registry.hasMutationUnsubscribe(e))
356
+ return;
357
+ const t = e.$subscribe(async (r) => {
358
+ const i = this._registry.getMutationSubscriptions(e);
359
+ if (!i?.size)
360
+ return;
361
+ const n = p.onMutation(e.$id, this._groups.nextSeq());
362
+ this._groups.start(() => ({ mutation: r, groupId: n }), e.$id, "store:onMutation"), this._events.emit(e, g.MUTATION, () => ({ mutation: r }));
363
+ try {
364
+ await Promise.all([...i].map(async (o) => {
365
+ try {
366
+ await o(r);
367
+ } catch (c) {
368
+ this._reporter.traceError("store:onMutation", c, e.$id, () => ({ groupId: n }));
369
+ }
370
+ }));
371
+ } finally {
372
+ this._groups.end(() => ({ groupId: n }), e.$id);
373
+ }
374
+ });
375
+ this._registry.setMutationUnsubscribe(e, t);
376
+ }
377
+ executeStoreOnActionSubscription(e, t = !1) {
378
+ const { ACTION_BEFORE: r, ACTION_AFTER: i, ACTION_ERROR: n, ACTION_ON_ERROR: o } = g, c = () => [r, i, n, o].some(
379
+ (a) => this._events.hasEventSubscription(e, a)
380
+ );
381
+ this._registry.isOnActionHooked(e) || typeof e.$onAction != "function" || !this._registry.getOnActionSubscriptions(e)?.size && !c() && !t || (this._registry.markOnActionHooked(e), e.$onAction(({ after: a, args: h, name: d, onError: l }) => {
382
+ const b = c() ? Symbol(_(d, e)) : void 0, y = this._registry.getOnActionSubscriptions(e);
383
+ if (!y?.size && !b)
384
+ return;
385
+ const O = _(d, e), E = this._groups.nextSeq(), m = `${e.$id}#${E}`, $ = p.onAction(e.$id, d, E);
386
+ let w = !1;
387
+ const M = () => {
388
+ y?.size && !w && (w = !0, this._groups.end({ groupId: $ }, e.$id));
389
+ };
390
+ y?.size && this._groups.start(() => ({ actionId: m, args: h, groupId: $ }), e.$id, `store:onAction:${d}`), this._events.emit(e, g.ACTION_BEFORE, () => ({
391
+ action: d,
392
+ args: h,
393
+ invocationKey: b
394
+ }));
395
+ try {
396
+ const S = [];
397
+ y?.forEach((G) => {
398
+ const D = this._groups.runSafely(
399
+ () => G({
400
+ after: (v) => this._registry.addOnActionAfterCallback(d, e, v),
401
+ args: h,
402
+ name: d,
403
+ onError: (v) => this._registry.addOnActionOnErrorCallback(d, e, v)
404
+ }),
405
+ "store:onAction:callback:error",
406
+ e.$id,
407
+ () => ({ actionId: m, groupId: $ })
408
+ );
409
+ D && S.push(D);
410
+ }), this._groups.endAfter(S, M), this.executeStoreOnActionAfterCallbacks(a, O, e, d, m, E, b), this.executeStoreOnActionOnErrorCallbacks(l, O, e, d, m, E, b), this._registry.clearOnActionCallbacks(O, e);
411
+ } catch (S) {
412
+ throw this._events.emit(e, g.ACTION_ERROR, () => ({
413
+ action: d,
414
+ error: S,
415
+ invocationKey: b
416
+ })), M(), S;
417
+ }
418
+ }));
419
+ }
420
+ executeStoreOnActionAfterCallbacks(e, t, r, i, n, o, c) {
421
+ const a = this._registry.getOnActionAfterCallbacks(t, r);
422
+ (a?.length || c) && e((h) => {
423
+ if (c && !this._registry.isDisposed(r) && this._events.emit(r, g.ACTION_AFTER, () => ({
424
+ action: i,
425
+ result: h,
426
+ invocationKey: c
427
+ })), !a?.length)
428
+ return;
429
+ this._groups.start(
430
+ () => ({
431
+ actionId: n,
432
+ groupId: p.onActionAfter(r.$id, i, o),
433
+ result: h
434
+ }),
435
+ r.$id,
436
+ `store:onAction:after:${i}`
437
+ );
438
+ const d = a.map((l) => this._groups.runSafely(
439
+ () => l(h),
440
+ "storeOnActionAfterCallbacks",
441
+ r.$id,
442
+ () => ({ actionId: n, "action name": i, groupId: p.onActionAfter(r.$id, i, o) })
443
+ ));
444
+ this._groups.endAfter(d, () => this._groups.end(() => ({ groupId: p.onActionAfter(r.$id, i, o) }), r.$id));
445
+ });
446
+ }
447
+ executeStoreOnActionOnErrorCallbacks(e, t, r, i, n, o, c) {
448
+ const a = this._registry.getOnActionOnErrorCallbacks(t, r);
449
+ (a?.length || c) && e((h) => {
450
+ if (c && !this._registry.isDisposed(r) && this._events.emit(r, g.ACTION_ON_ERROR, () => ({
451
+ action: i,
452
+ error: h,
453
+ invocationKey: c
454
+ })), !a?.length)
455
+ return;
456
+ this._groups.start(
457
+ () => ({
458
+ actionId: n,
459
+ "action name": i,
460
+ error: h,
461
+ groupId: p.onActionError(r.$id, i, o)
462
+ }),
463
+ r.$id,
464
+ `store:onAction:onError:${i}`
465
+ );
466
+ const d = a.map((l) => this._groups.runSafely(
467
+ () => l(h),
468
+ "store:onAction:onError:error",
469
+ r.$id,
470
+ () => ({ actionId: n, groupId: p.onActionError(r.$id, i, o) })
471
+ ));
472
+ this._groups.endAfter(d, () => this._groups.end(() => ({ groupId: p.onActionError(r.$id, i, o) }), r.$id));
473
+ });
474
+ }
475
+ }
476
+ class me {
477
+ constructor() {
478
+ u(this, "_disposedStores", /* @__PURE__ */ new WeakSet());
479
+ u(this, "_storesRegistry", /* @__PURE__ */ new Map());
480
+ }
481
+ addOnActionAfterCallback(e, t, r) {
482
+ const i = _(e, t), n = this._storesRegistry.get(t).get("afterCallbacks"), o = n.get(i) ?? [];
483
+ o.push(r), n.set(i, o);
484
+ }
485
+ addOnActionOnErrorCallback(e, t, r) {
486
+ const i = _(e, t), n = this._storesRegistry.get(t).get("errorCallbacks"), o = n.get(i) ?? [];
487
+ o.push(r), n.set(i, o);
488
+ }
489
+ addOnActionSubscription(e, t, r) {
490
+ this.getOnActionSubscriptions(e)?.add(t), this._storesRegistry.get(e)?.get("onActionTraces")?.push(r);
491
+ }
492
+ addMutationSubscription(e, t, r) {
493
+ this.getMutationSubscriptions(e)?.add(t), this._storesRegistry.get(e)?.get("mutationTraces")?.push(r);
494
+ }
495
+ clearOnActionCallbacks(e, t) {
496
+ this._storesRegistry.get(t)?.get("afterCallbacks")?.delete(e), this._storesRegistry.get(t)?.get("errorCallbacks")?.delete(e);
497
+ }
498
+ clearStore(e) {
499
+ this._storesRegistry.get(e)?.get("mutationUnsubscribes")?.(), this._storesRegistry.delete(e);
500
+ }
501
+ getMutationSubscriptions(e) {
502
+ return this._storesRegistry.get(e)?.get("mutationSubscriptions");
503
+ }
504
+ getOnActionAfterCallbacks(e, t) {
505
+ return this._storesRegistry.get(t)?.get("afterCallbacks")?.get(e);
506
+ }
507
+ getOnActionOnErrorCallbacks(e, t) {
508
+ return this._storesRegistry.get(t)?.get("errorCallbacks")?.get(e);
509
+ }
510
+ getOnActionSubscriptions(e) {
511
+ return this._storesRegistry.get(e)?.get("onActionSubscriptions");
512
+ }
513
+ hasMutationUnsubscribe(e) {
514
+ return !!this._storesRegistry.get(e)?.get("mutationUnsubscribes");
515
+ }
516
+ initStoreRegistry(e) {
517
+ if (!this._storesRegistry.has(e)) {
518
+ const t = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
519
+ t.set("afterCallbacks", r);
520
+ const i = /* @__PURE__ */ new Map();
521
+ t.set("errorCallbacks", i);
522
+ const n = /* @__PURE__ */ new Set();
523
+ t.set("mutationSubscriptions", n);
524
+ const o = [];
525
+ t.set("mutationTraces", o), t.set("onActionStore", !1), t.set("onActionSubscriptions", /* @__PURE__ */ new Set());
526
+ const c = [];
527
+ t.set("onActionTraces", c), this._storesRegistry.set(e, t);
528
+ }
529
+ }
530
+ isDisposed(e) {
531
+ return this._disposedStores.has(e);
532
+ }
533
+ isOnActionHooked(e) {
534
+ return !!this._storesRegistry.get(e)?.get("onActionStore");
535
+ }
536
+ markOnActionHooked(e) {
537
+ this._storesRegistry.get(e)?.set("onActionStore", !0);
538
+ }
539
+ setTraces(e, t) {
540
+ this._storesRegistry.get(e)?.set("onActionTraces", t.onAction), this._storesRegistry.get(e)?.set("mutationTraces", t.onMutation);
541
+ }
542
+ setMutationUnsubscribe(e, t) {
543
+ this._storesRegistry.get(e)?.set("mutationUnsubscribes", t);
544
+ }
545
+ registerStoreCleanup(e, { onDispose: t, onError: r }) {
546
+ if (typeof e.$dispose != "function")
547
+ return;
548
+ const i = e.$dispose.bind(e), n = e;
549
+ n.__piniaPluginSubscriptionDisposed || (n.__piniaPluginSubscriptionDisposed = !0, e.$dispose = () => {
550
+ try {
551
+ this._disposedStores.add(e), this.clearStore(e), t(e);
552
+ } catch (o) {
553
+ r(o, e);
554
+ }
555
+ return i();
556
+ });
557
+ }
558
+ }
559
+ class Ae {
560
+ constructor(e, t, r, i) {
561
+ u(this, "_groups");
562
+ u(this, "_policy");
563
+ u(this, "_registry");
564
+ u(this, "_reporter");
565
+ u(this, "_stores", /* @__PURE__ */ new WeakMap());
566
+ this._policy = e, this._registry = t, this._groups = r, this._reporter = i;
567
+ }
568
+ clearStore(e) {
569
+ this._stores.delete(e);
570
+ }
571
+ defineSubscriberKey(e, t) {
572
+ return _(e.name, t);
573
+ }
574
+ defineSubscriptionKey(e, t) {
575
+ return _(e, t);
576
+ }
577
+ isSubscriberDelivered(e, t) {
578
+ return this.getStoreState(e).subscribersDelivered.has(t);
579
+ }
580
+ isSubscriberHandled(e, t) {
581
+ const r = this.getStoreState(e);
582
+ return r.subscribersDelivered.has(t) || r.subscribersScheduled.has(t);
583
+ }
584
+ markSubscriberDelivered(e, t) {
585
+ this.getStoreState(e).subscribersDelivered.add(t);
586
+ }
587
+ isSubscriptionScheduled(e, t) {
588
+ return this.getStoreState(e).subscriptionsScheduled.has(t);
589
+ }
590
+ queueSubscriberExecution(e, t, r, i) {
591
+ const n = this._policy.resolveHydrationScheduler(t);
592
+ this.getStoreState(e.store).subscribersScheduled.add(r), n(() => {
593
+ if (this._registry.isDisposed(e.store))
594
+ return;
595
+ this.getStoreState(e.store).subscribersScheduled.delete(r);
596
+ const o = e.store.$id, c = p.pluginDeferred(o, t.name, this._groups.nextSeq());
597
+ this._groups.start(() => ({ groupId: c, subscriber: t.name }), o, "subscriber:deferred");
598
+ const a = () => this._groups.end(() => ({ groupId: c }), o);
599
+ let h;
600
+ try {
601
+ h = i(e, t);
602
+ } finally {
603
+ h ? this._groups.closeWhenSettled(h, a, o, t.name) : a();
604
+ }
605
+ });
606
+ }
607
+ subscriptionDelivery(e, t) {
608
+ const r = this.getStoreState(e.store);
609
+ Object.entries(t).forEach(([i, n]) => {
610
+ const o = this.defineSubscriptionKey(i, e.store), c = this._policy.resolveExecution(n.subscription);
611
+ if (!(r.subscriptionsDelivered.has(o) || r.subscriptionsScheduled.has(o)) && this._policy.shouldInvoke(c)) {
612
+ if (this._policy.shouldScheduleAfterHydration(c)) {
613
+ const a = this._policy.resolveHydrationScheduler(n.subscription);
614
+ r.subscriptionsScheduled.add(o), a(() => {
615
+ this._registry.isDisposed(e.store) || (this.getStoreState(e.store).subscriptionsScheduled.delete(o), this.subscriptionDeliverySafely(e, i, n));
616
+ });
617
+ return;
618
+ }
619
+ this.subscriptionDeliverySafely(e, i, n);
620
+ }
621
+ });
622
+ }
623
+ subscriptionDeliverySafely({ store: e, options: t }, r, i) {
624
+ try {
625
+ const n = this.getStoreState(e);
626
+ if (n.subscriptionsDelivered.has(this.defineSubscriptionKey(r, e)))
627
+ return;
628
+ const { subscription: o, subscriptionOptions: c, stores: a } = i;
629
+ this._reporter.trace("Subscription:delivery", e.$id, () => ({
630
+ groupId: p.plugin(e.$id),
631
+ pluginName: r,
632
+ subscription: o,
633
+ subscriptionOptions: c
634
+ }));
635
+ const h = {
636
+ storeOptions: { ...t?.storeOptions ?? {}, ...c }
637
+ };
638
+ o.invoke(
639
+ { store: e, options: { ...t, ...h } }
640
+ ), n.subscriptionsDelivered.add(this.defineSubscriptionKey(r, e)), q(a) || a?.forEach(
641
+ (d) => o.invoke({
642
+ store: d,
643
+ options: {
644
+ ...t,
645
+ ...h,
646
+ ...d?.storeOptions ?? {}
647
+ }
648
+ })
649
+ );
650
+ } catch (n) {
651
+ this._reporter.traceError("subscription:delivery", n, e.$id, () => ({ groupId: p.plugin(e.$id) }));
652
+ }
653
+ }
654
+ getStoreState(e) {
655
+ let t = this._stores.get(e);
656
+ return t || (t = {
657
+ subscribersDelivered: /* @__PURE__ */ new Set(),
658
+ subscribersScheduled: /* @__PURE__ */ new Set(),
659
+ subscriptionsDelivered: /* @__PURE__ */ new Set(),
660
+ subscriptionsScheduled: /* @__PURE__ */ new Set()
661
+ }, this._stores.set(e, t)), t;
662
+ }
663
+ }
664
+ const Oe = 2500;
665
+ class $e {
666
+ constructor(e, t) {
667
+ u(this, "_options");
668
+ u(this, "_reporter");
669
+ u(this, "_seq", 0);
670
+ this._options = e, this._reporter = t;
671
+ }
672
+ nextSeq() {
673
+ return ++this._seq;
674
+ }
675
+ start(e, t, r, i = !0) {
676
+ const n = this._options?.tracerHandler;
677
+ n && (n.hasTraceListeners?.() ?? !0) && (e = typeof e == "function" ? e() : e ?? {}, n.startGroup({
678
+ ...e,
679
+ scope: t
680
+ }), i && this._reporter.trace(r ?? "context", t, e));
681
+ }
682
+ end(e, t) {
683
+ const r = this._options?.tracerHandler;
684
+ r && (r.hasTraceListeners?.() ?? !0) && r.closeGroup({
685
+ ...typeof e == "function" ? e() : e ?? {},
686
+ scope: t
687
+ });
688
+ }
689
+ endAfter(e, t) {
690
+ const r = e.filter((i) => !!i);
691
+ r.length ? Promise.all(r).then(t) : t();
692
+ }
693
+ closeWhenSettled(e, t, r, i) {
694
+ let n = !1;
695
+ const o = () => {
696
+ n || (n = !0, globalThis.clearTimeout(a), t());
697
+ }, c = this._options?.deferredTraceGroupTimeout ?? Oe, a = globalThis.setTimeout(() => {
698
+ n || (this._reporter.traceError(
699
+ "subscriber:deferred:timeout",
700
+ new Error(`Subscriber "${i}" hydration still pending after ${c}ms`),
701
+ r
702
+ ), o());
703
+ }, c);
704
+ e.then(o, o);
705
+ }
706
+ runSafely(e, t, r, i) {
707
+ try {
708
+ const n = e();
709
+ if (n && typeof n.then == "function")
710
+ return Promise.resolve(n).then(
711
+ () => {
712
+ },
713
+ (o) => this._reporter.traceError(t, o, r, i)
714
+ );
715
+ } catch (n) {
716
+ this._reporter.traceError(t, n, r, i);
717
+ }
718
+ }
719
+ }
720
+ class xe extends ee {
721
+ constructor(t, r) {
722
+ super({ prefixLabelWithName: !0 });
723
+ u(this, "_eventsRegistry", fe());
724
+ u(this, "_name", "PluginOrchestrator");
725
+ u(this, "_actionExecutor");
726
+ u(this, "_deliverer");
727
+ u(this, "_groups");
728
+ u(this, "_options");
729
+ u(this, "_policy");
730
+ u(this, "_registry");
731
+ u(this, "_subscribers", []);
732
+ u(this, "_subscriptions", []);
733
+ this._options = r, this._subscribers = t.sort((n, o) => (typeof n?.execution > "u" ? n.execution = { order: 9 } : n.execution && typeof n.execution?.order > "u" && (n.execution.order = 9), typeof o?.execution > "u" ? o.execution = { order: 9 } : o.execution && typeof o.execution?.order > "u" && (o.execution.order = 9), (n.execution.order ?? 0) - (o.execution?.order ?? 0)));
734
+ const i = {
735
+ trace: (...n) => this.trace(...n),
736
+ traceError: (...n) => this.traceError(...n)
737
+ };
738
+ this._policy = new ye(r), this._registry = new me(), this._groups = new $e(r, i), this._deliverer = new Ae(this._policy, this._registry, this._groups, i), this._actionExecutor = new Ee(
739
+ this._registry,
740
+ this._groups,
741
+ {
742
+ emit: (n, o, c) => this.emit(n, o, c),
743
+ hasEventSubscription: (n, o) => this.storeHasEventSubscription(n, o)
744
+ },
745
+ i
746
+ ), this.initializeTracer();
747
+ }
748
+ set subscribers(t) {
749
+ this._subscribers = t;
750
+ }
751
+ get subscriptions() {
752
+ if (!q(this._subscriptions))
753
+ return this._subscriptions;
754
+ }
755
+ executeSubscriber(t, r) {
756
+ const i = this._deliverer.defineSubscriberKey(r, t.store);
757
+ if (this._deliverer.isSubscriberDelivered(t.store, i) || !r.invoke(t, {
758
+ createTracer: this._options?.tracerHandler?.createTracer,
759
+ eventsRegistry: this._eventsRegistry
760
+ }))
761
+ return;
762
+ this.emit(t.store, oe.INVOKED, () => ({ subscriber: r.name })), this._deliverer.markSubscriberDelivered(t.store, i);
763
+ let n;
764
+ try {
765
+ n = r.hydrate?.(t);
766
+ } catch (a) {
767
+ this.traceError("subscriber:hydrate", a, t.store.$id);
768
+ return;
769
+ }
770
+ const o = () => {
771
+ try {
772
+ const a = r.afterHydration?.(t);
773
+ if (a && typeof a.then == "function")
774
+ return a.catch(
775
+ (h) => this.traceError("subscriber:afterHydration", h, t.store.$id)
776
+ );
777
+ } catch (a) {
778
+ this.traceError("subscriber:afterHydration", a, t.store.$id);
779
+ }
780
+ };
781
+ let c;
782
+ return n && typeof n.then == "function" ? c = n.then(() => o()).catch((a) => this.traceError("subscriber:hydrate", a, t.store.$id)) : c = o(), r.subscriptions && this._deliverer.subscriptionDelivery(t, r.subscriptions), r.storeMutationSubscription && this.registerMutationSubscription(r, r.storeMutationSubscription), this.registerOnActionSubscription(r, t), c;
783
+ }
784
+ executeSubscriberSafely(t, r) {
785
+ try {
786
+ return this.executeSubscriber(t, r);
787
+ } catch (i) {
788
+ this.traceError("subscriber:execute", i, t.store.$id, () => ({
789
+ subscriber: r.name
790
+ }));
791
+ }
792
+ }
793
+ initializeTracer() {
794
+ this._options?.tracerHandler && (this._tracer = this._options.tracerHandler.createTracer(ce));
795
+ }
796
+ plugin({ store: t, options: r }) {
797
+ if (!this._subscribers.length)
798
+ return;
799
+ this._registry.initStoreRegistry(t);
800
+ const i = {
801
+ $onAction: [],
802
+ $subscribe: []
803
+ }, n = p.plugin(t.$id), o = () => ({
804
+ groupId: n,
805
+ subscribers: this._subscribers.map((c) => ({
806
+ name: c.name,
807
+ execution: c.execution,
808
+ pluginOptions: c.pluginOptions
809
+ })),
810
+ "hooks subscriptions": i,
811
+ store: t,
812
+ options: r
813
+ });
814
+ this._groups.start(() => ({ groupId: n }), t.$id, void 0, !1), this.emit(t, T.EXECUTION.START), this._registry.setTraces(t, { onAction: i.$onAction, onMutation: i.$subscribe });
815
+ try {
816
+ let c = !1;
817
+ this._subscribers.forEach(
818
+ (a) => {
819
+ const h = this._deliverer.defineSubscriberKey(a, t), d = this._policy.resolveExecution(a);
820
+ if (!this._deliverer.isSubscriberHandled(t, h) && this._policy.shouldInvoke(d)) {
821
+ if (this._policy.shouldScheduleAfterHydration(d)) {
822
+ c = !0, this._deliverer.queueSubscriberExecution(
823
+ { store: t, options: r },
824
+ a,
825
+ h,
826
+ (l, b) => this.executeSubscriberSafely(l, b)
827
+ );
828
+ return;
829
+ }
830
+ this.executeSubscriber({ store: t, options: r }, a);
831
+ }
832
+ }
833
+ ), this._actionExecutor.executeStoreOnActionSubscription(t, c), this.registerStoreCleanup(t);
834
+ } catch (c) {
835
+ this.traceError("plugin:invoke", c, t.$id), this.emit(t, T.EXECUTION.ERROR, () => ({ error: c }));
836
+ }
837
+ this.trace("context", t.$id, o), this._groups.end(() => ({ groupId: n }), t.$id), this.emit(t, T.EXECUTION.END);
838
+ }
839
+ registerMutationSubscription(t, r) {
840
+ const { store: i, callback: n } = r();
841
+ !i || !n || (this._registry.addMutationSubscription(i, n, t.name), this._actionExecutor.hookStoreMutations(i));
842
+ }
843
+ registerOnActionSubscription(t, r) {
844
+ const { store: i, callback: n } = t?.storeOnActionSubscription?.(r) ?? {};
845
+ !i || !n || this._registry.addOnActionSubscription(i, n, t.name);
846
+ }
847
+ registerStoreCleanup(t) {
848
+ this._registry.registerStoreCleanup(t, {
849
+ onDispose: () => {
850
+ this._deliverer.clearStore(t), this.emit(t, g.DISPOSE);
851
+ },
852
+ onError: (r) => this.traceError("store:dispose:error", r, t.$id)
853
+ });
854
+ }
855
+ }
856
+ const I = /* @__PURE__ */ new WeakMap(), A = /* @__PURE__ */ new Map(), P = /* @__PURE__ */ new WeakSet();
857
+ function Ie(s, e, t) {
858
+ let r;
859
+ const i = t?.storeOptions?.enhancedStore === !0, n = j(
860
+ s,
861
+ () => (r = {
862
+ id: s,
863
+ extensions: {}
864
+ }, i && A.set(s, r), e(r)),
865
+ t
866
+ );
867
+ return Object.assign(((...o) => {
868
+ const c = n(...o);
869
+ return i && r && (I.set(c, r), A.delete(s), ve(c, s)), c;
870
+ }), n);
871
+ }
872
+ function we(s) {
873
+ const e = s;
874
+ return I.get(s) ?? (typeof e?.$id == "string" ? A.get(e.$id) : void 0);
875
+ }
876
+ function Me(s) {
877
+ const e = s.extensions.enhancedStore;
878
+ if (!e)
879
+ throw new Error(`${s.id}Store - getEnhancedStore - Error: enhanced store is required`);
880
+ return e;
881
+ }
882
+ function ve(s, e) {
883
+ if (P.has(s) || typeof s.$dispose != "function")
884
+ return;
885
+ const t = s.$dispose;
886
+ s.$dispose = (...r) => (I.delete(s), A.delete(e), t.apply(s, r)), P.add(s);
887
+ }
888
+ function De(s, e) {
889
+ s.extensions.enhancedStore = e;
890
+ }
891
+ export {
892
+ V as E,
893
+ T as P,
894
+ g as S,
895
+ ee as T,
896
+ we as a,
897
+ xe as b,
898
+ oe as c,
899
+ ke as d,
900
+ W as e,
901
+ Ie as f,
902
+ F as g,
903
+ Te as h,
904
+ q as i,
905
+ Me as j,
906
+ ce as p,
907
+ De as s,
908
+ p as t
909
+ };