pinia-plugin-subscription 1.0.0-beta.17 → 1.0.0-beta.19
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 +6 -3
- package/README.md +22 -7
- package/dist/chunks/PluginSubscription-A8aoeAIQ.js +599 -0
- package/dist/core/PluginSubscriber.d.ts +47 -4
- package/dist/core/PluginSubscription.d.ts +3 -5
- package/dist/core/Store.d.ts +15 -6
- package/dist/events/events-emitter.d.ts +73 -0
- package/dist/events/events-registry.d.ts +23 -0
- package/dist/events/store-events-subscriber.d.ts +1 -1
- package/dist/helpers/index.d.ts +0 -1
- package/dist/helpers.js +134 -106
- package/dist/pinia-plugin-subscription.js +7 -7
- package/dist/types/plugin.d.ts +1 -1
- package/docs/authoring-plugins.md +55 -25
- package/docs/using-plugins.md +28 -21
- package/package.json +1 -1
- package/dist/chunks/PluginSubscription-BwmFDjfB.js +0 -626
- package/dist/core/PluginSubscriberWithoutInstance.d.ts +0 -52
- package/dist/system/TraceEmitter.d.ts +0 -12
- package/dist/system/TraceEmitterBase.d.ts +0 -20
package/CHANGELOG.md
CHANGED
|
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
24
24
|
- Added unit tests covering the tracing system and the leak detection logic
|
|
25
25
|
|
|
26
26
|
### Changed
|
|
27
|
+
- `PluginSubscriber` passes the shared store-events handler to its `Store` instance; both can register store-event subscribers.
|
|
27
28
|
- Subscribers now execute in ascending `execution.order` order (default `9`), preserving declaration order for equal priorities; `PluginSubscriber.tracer` exposes the tracer created for that subscriber and passes the same instance to its `Store` factory.
|
|
28
29
|
- Trace groups follow the lifecycles of a store: `groupId` is now a `/` separated path (`<storeId>/plugin`, `<storeId>/onAction:<name>#<n>`, with `/after` and `/onError` sub-groups, `<storeId>/onMutation#<n>`, `<storeId>/reset#<n>`, `<storeId>/plugin/deferred:<subscriber>#<n>`). Every trace emitted for a store while a group is open, including those of subscribers and `Store` subclasses, joins the innermost one. `traceGroups` builders take the call counter; `TraceGroupId` is removed, and `groupId` is lifted from the payload to the event like `actionId`
|
|
29
30
|
- `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
|
|
@@ -39,14 +40,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
39
40
|
- Error events are delivered to matching listeners and reported with `console.error` when none match
|
|
40
41
|
- Store trace labels can include the store class name by default, and callers can override the event namespace
|
|
41
42
|
- Excluded `scripts/**` from coverage thresholds, as build tooling is not shipped to consumers
|
|
42
|
-
- `Store` and `PluginSubscription`
|
|
43
|
+
- `Store`, `PluginSubscriber` and `PluginSubscription` share store-event emission and tracing helpers; `Store` receives the shared event handler from its subscriber.
|
|
43
44
|
- `Tracer` listener matching no longer allocates intermediate arrays beyond the matched set, and duplicated unhandled-error reporting was factored out
|
|
44
45
|
- The `pluginsubscription` namespace prefix is now a shared constant (`pluginSubscriptionNamespace`) instead of a hard-coded string
|
|
45
46
|
|
|
46
47
|
### Changed (breaking)
|
|
48
|
+
- Class-based subscribers now define a unique `_name` instead of passing a name to `super()`; `PluginSubscriber` takes only its instance factory. `Store` subclasses use `_name` instead of `_className`. `trace()` and `traceError()` are protected, and subclasses configure label prefixes through `traceOptions.prefixLabelWithName` instead of `classNameAddedToTraceNamespace`.
|
|
49
|
+
- Removed `PluginSubscriberWithoutInstance`; use a plain-object subscriber or `PluginSubscriber` with a `Store` subclass.
|
|
47
50
|
- Move automatic and manual rollback, including their types, to `pinia-plugin-state-snapshots`; register its subscriber explicitly.
|
|
48
|
-
- Move `$reset` ownership and reset callback execution to
|
|
49
|
-
-
|
|
51
|
+
- Move `$reset` ownership and reset callback execution to the state-snapshots package; callbacks are registered with `$onResetStore()`, while `PluginSubscription` only orchestrates subscribers.
|
|
52
|
+
- Store-event subscriber failures no longer propagate to the emitter, and `shouldPropagateError` has been removed. Synchronous failures and async rejections are logged; async subscribers are not awaited.
|
|
50
53
|
- removed the built-in console output of traces (`createConsoleGroupTraceListener`, `ConsoleTraceGroupListener`, `TraceGroupListener`, `TraceListenerGroupBy`, the `groupBy` option of `addListener()`, and the internal console printers). Listeners now only forward events to their own `handler` (a no-op when omitted); `startGroup` / `closeGroup` only maintain the open-group stack that sets `groupId`. Fixed `tracerHandler.journal` not being exposed by the registry
|
|
51
54
|
- The tracing solution moved to the new `pinia-plugin-subscription-devtools` package (`packages/devtools/`, same repository). `pinia-plugin-subscription` now only emits trace events through the `Tracer` / `TracerHandler` contract exported from `pinia-plugin-subscription/types`; without a `tracerHandler` it emits nothing. The compressed package drops from about 117 kB to about 50 kB, and the new package weighs about 19 kB.
|
|
52
55
|
- `createTracerRegistry`, `Tracer` (class), `defaultRedactedKeys`, `stringifyTraceSnapshot`, `getTraceGroupName` and `mountTraceDevtools` are now exported by `pinia-plugin-subscription-devtools` instead of `pinia-plugin-subscription/helpers` and `pinia-plugin-subscription/devtools`. `traceGroups` stays in `pinia-plugin-subscription/helpers`.
|
package/README.md
CHANGED
|
@@ -6,8 +6,7 @@ This project provides:
|
|
|
6
6
|
- a lightweight mechanism to declare "subscribers" that are invoked when stores are registered or updated by Pinia;
|
|
7
7
|
- a `Store` base class (helper wrapper) to ease interacting with Pinia stores from subscribers or other plugin code;
|
|
8
8
|
- an API to create a Pinia plugin from a list of subscribers;
|
|
9
|
-
-
|
|
10
|
-
- optional state rollback through the separate [`pinia-plugin-state-snapshots`](packages/state-snapshots/) package.
|
|
9
|
+
- optional `$reset` support and state rollback through the separate [`pinia-plugin-state-snapshots`](packages/state-snapshots/) package.
|
|
11
10
|
|
|
12
11
|
Deferred subscribers attach their action and reset callbacks to their own store after hydration; disposing a store removes its callbacks.
|
|
13
12
|
If a snapshot cannot be created, the state-snapshots subscriber logs the error
|
|
@@ -65,10 +64,22 @@ app.use(pinia)
|
|
|
65
64
|
app.mount('#app')
|
|
66
65
|
```
|
|
67
66
|
|
|
67
|
+
## Compatible Plugins
|
|
68
|
+
|
|
69
|
+
These plugins build on `pinia-plugin-subscription`; register their subscribers
|
|
70
|
+
with `createPlugin()` to use their features:
|
|
71
|
+
|
|
72
|
+
- **[pinia-plugin-subscription-devtools](packages/devtools/README.md):** structured tracing with filtered listeners, a per-store journal, payload redaction and an in-page panel.
|
|
73
|
+
- **[pinia-plugin-state-snapshots](packages/state-snapshots/README.md):** opt-in `$reset` wrapping and reset callbacks, plus state rollback when configured actions fail.
|
|
74
|
+
- **[pinia-plugin-extending-store](https://github.com/AlxBDo/extending-pinia-store):** compose state, getters and actions from parent stores into a child store.
|
|
75
|
+
- **[pinia-plugin-persist-state](https://github.com/AlxBDo/pinia-plugin-persist-state):** persist store state to browser storage or IndexedDB, with optional encryption.
|
|
76
|
+
- **[pinia-plugin-action-flow](https://github.com/AlxBDo/pinia-plugin-action-flow):** define before/after flows around store actions, including validation and side effects.
|
|
77
|
+
|
|
78
|
+
Discover more compatible plugins on the [`pinia-plugin-subscription` GitHub topic](https://github.com/topics/pinia-plugin-subscription).
|
|
79
|
+
|
|
68
80
|
## Advanced Features
|
|
69
81
|
|
|
70
|
-
- **Structured tracing:** the plugin and `Store` emit typed trace events through an optional `tracerHandler
|
|
71
|
-
- **Reset callbacks:** The separate [`pinia-plugin-state-snapshots`](packages/state-snapshots/README.md) package owns reset behavior. Register callbacks with the store's `$onResetStore()` method; `PluginSubscription` itself does not replace or reset `$reset`.
|
|
82
|
+
- **Structured tracing:** the plugin and `Store` emit typed trace events through an optional `tracerHandler`; without one, no trace events are emitted. See [Tracing](#tracing).
|
|
72
83
|
- **SSR and hydration control:** Configure whether each subscribed plugin runs on the server, the client, or only after client hydration.
|
|
73
84
|
- **Lifecycle safety:** Internal hydration ordering is now guarded so `hydrate()` completes before `afterHydration()` and disposed stores do not retain stale subscriber metadata.
|
|
74
85
|
|
|
@@ -76,9 +87,13 @@ For subscriber authors, store action events are emitted even without a `storeOnA
|
|
|
76
87
|
`ACTION_BEFORE` receives `(name, invocationKey)`, `ACTION_AFTER` receives
|
|
77
88
|
`(name, invocationKey)` after success, and `ACTION_ON_ERROR` receives `(name, error, invocationKey)`
|
|
78
89
|
after failure. The symbol identifies one invocation.
|
|
79
|
-
Event subscribers
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
Event subscribers cannot interrupt the emitter: synchronous failures and async
|
|
91
|
+
rejections are reported with `console.error`, and other subscribers still run.
|
|
92
|
+
Async subscribers are not awaited.
|
|
93
|
+
Class-based subscribers define a unique `_name`; their `Store` wrappers can
|
|
94
|
+
register event callbacks through protected helpers using the shared handler.
|
|
95
|
+
The orchestrator clears those subscriptions when the store is disposed. See
|
|
96
|
+
the [plugin authoring guide](docs/authoring-plugins.md#store-event-registration).
|
|
82
97
|
|
|
83
98
|
## Tracing
|
|
84
99
|
|
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
var N = Object.defineProperty;
|
|
2
|
+
var P = (s, r, t) => r in s ? N(s, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[r] = t;
|
|
3
|
+
var h = (s, r, t) => P(s, typeof r != "symbol" ? r + "" : r, t);
|
|
4
|
+
import { defineStore as D } from "pinia";
|
|
5
|
+
class W {
|
|
6
|
+
constructor({ prefixLabelWithName: r } = { prefixLabelWithName: !1 }) {
|
|
7
|
+
h(this, "traceOptions", { prefixLabelWithName: !1 });
|
|
8
|
+
h(this, "_storeEventsSubscribersHandler");
|
|
9
|
+
h(this, "_tracer");
|
|
10
|
+
this.traceOptions.prefixLabelWithName = r ?? !1;
|
|
11
|
+
}
|
|
12
|
+
get name() {
|
|
13
|
+
return this._name;
|
|
14
|
+
}
|
|
15
|
+
get tracer() {
|
|
16
|
+
return this._tracer;
|
|
17
|
+
}
|
|
18
|
+
clearSubscribers(r) {
|
|
19
|
+
this._storeEventsSubscribersHandler?.clearSubscribers(r);
|
|
20
|
+
}
|
|
21
|
+
emit(r, t, i) {
|
|
22
|
+
this.hasSubscribers(r.$id) && this._storeEventsSubscribersHandler?.emit(r, t, i);
|
|
23
|
+
}
|
|
24
|
+
hasSubscribers(r) {
|
|
25
|
+
return (this._storeEventsSubscribersHandler?.getSubscribers(r).length ?? 0) > 0;
|
|
26
|
+
}
|
|
27
|
+
trace(r, t, i, e) {
|
|
28
|
+
this.tracer && (r = this.traceLabelFormatter(r), this.tracer.debug(r, e ?? r.toLowerCase(), i, t));
|
|
29
|
+
}
|
|
30
|
+
traceError(r, t, i, e, n) {
|
|
31
|
+
this.tracer ? (r = this.traceLabelFormatter(r), this.tracer.error(r, n ?? r.toLowerCase(), t, e, i)) : console.error(t);
|
|
32
|
+
}
|
|
33
|
+
traceLabelFormatter(r) {
|
|
34
|
+
return this.traceOptions.prefixLabelWithName && (r = `${this._name}:${r}`), r;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function C(s) {
|
|
38
|
+
return s.join(":");
|
|
39
|
+
}
|
|
40
|
+
function g(s) {
|
|
41
|
+
return C(["plugin", ...s]);
|
|
42
|
+
}
|
|
43
|
+
function l(s) {
|
|
44
|
+
return C(["store", ...s]);
|
|
45
|
+
}
|
|
46
|
+
function U(s) {
|
|
47
|
+
return C(["subscriber", ...s]);
|
|
48
|
+
}
|
|
49
|
+
const L = l(["action", "after"]), F = l(["action", "before"]), K = l(["action", "error"]), j = l(["action", "onError"]), $ = {
|
|
50
|
+
EXECUTION: {
|
|
51
|
+
END: g(["execution", "end"]),
|
|
52
|
+
ERROR: g(["execution", "error"]),
|
|
53
|
+
START: g(["execution", "start"])
|
|
54
|
+
}
|
|
55
|
+
}, S = {
|
|
56
|
+
ACTION_AFTER: L,
|
|
57
|
+
ACTION_BEFORE: F,
|
|
58
|
+
ACTION_ERROR: K,
|
|
59
|
+
ACTION_ON_ERROR: j,
|
|
60
|
+
DISPOSE: l(["dispose"]),
|
|
61
|
+
MUTATION: l(["mutation"]),
|
|
62
|
+
RESET: l(["reset"])
|
|
63
|
+
}, q = {
|
|
64
|
+
INVOKED: U(["invoked"])
|
|
65
|
+
}, T = /* @__PURE__ */ new WeakMap(), E = /* @__PURE__ */ new Map(), w = /* @__PURE__ */ new WeakSet();
|
|
66
|
+
function z(s, r) {
|
|
67
|
+
if (w.has(s) || typeof s.$dispose != "function")
|
|
68
|
+
return;
|
|
69
|
+
const t = s.$dispose;
|
|
70
|
+
s.$dispose = (...i) => (T.delete(s), E.delete(r), t.apply(s, i)), w.add(s);
|
|
71
|
+
}
|
|
72
|
+
function it(s, r, t) {
|
|
73
|
+
const i = t ? { storeOptions: t } : {};
|
|
74
|
+
return typeof r == "function" ? R(
|
|
75
|
+
s,
|
|
76
|
+
r,
|
|
77
|
+
i
|
|
78
|
+
) : B(
|
|
79
|
+
s,
|
|
80
|
+
r,
|
|
81
|
+
i
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
function rt(s, r, t) {
|
|
85
|
+
const i = t ? { storeOptions: { ...t, enhancedStore: !0 } } : { storeOptions: { enhancedStore: !0 } };
|
|
86
|
+
return R(
|
|
87
|
+
s,
|
|
88
|
+
r,
|
|
89
|
+
i
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
function R(s, r, t) {
|
|
93
|
+
let i;
|
|
94
|
+
const e = t?.storeOptions?.enhancedStore === !0, n = D(
|
|
95
|
+
s,
|
|
96
|
+
() => (i = {
|
|
97
|
+
id: s,
|
|
98
|
+
extensions: {}
|
|
99
|
+
}, e && E.set(s, i), r(i)),
|
|
100
|
+
t
|
|
101
|
+
);
|
|
102
|
+
return Object.assign(((...o) => {
|
|
103
|
+
const c = n(...o);
|
|
104
|
+
return e && i && (T.set(c, i), E.delete(s), z(c, s)), c;
|
|
105
|
+
}), n);
|
|
106
|
+
}
|
|
107
|
+
function B(s, r, t) {
|
|
108
|
+
return t && (r = { ...r, ...t ?? {} }), D(s, r);
|
|
109
|
+
}
|
|
110
|
+
const X = /* @__PURE__ */ new Set(["_", "$"]);
|
|
111
|
+
function st(s) {
|
|
112
|
+
return X.has(s[0]);
|
|
113
|
+
}
|
|
114
|
+
function nt(s) {
|
|
115
|
+
const r = s;
|
|
116
|
+
return T.get(s) ?? (typeof r?.$id == "string" ? E.get(r.$id) : void 0);
|
|
117
|
+
}
|
|
118
|
+
function ot(s) {
|
|
119
|
+
const r = s.extensions.enhancedStore;
|
|
120
|
+
if (!r)
|
|
121
|
+
throw new Error(`${s.id}Store - getEnhancedStore - Error: enhanced store is required`);
|
|
122
|
+
return r;
|
|
123
|
+
}
|
|
124
|
+
function ct(s, r) {
|
|
125
|
+
s.extensions.enhancedStore = r;
|
|
126
|
+
}
|
|
127
|
+
function A(s, r) {
|
|
128
|
+
return m(s, r);
|
|
129
|
+
}
|
|
130
|
+
function G(s) {
|
|
131
|
+
const r = String(s.$id);
|
|
132
|
+
return `${r.length}:${r}:`;
|
|
133
|
+
}
|
|
134
|
+
function m(s, r) {
|
|
135
|
+
return `${G(r)}${s.length}:${s}`;
|
|
136
|
+
}
|
|
137
|
+
class V {
|
|
138
|
+
constructor() {
|
|
139
|
+
h(this, "_subscribers", /* @__PURE__ */ new Map());
|
|
140
|
+
}
|
|
141
|
+
clearSubscribers(r) {
|
|
142
|
+
this._subscribers.get(r)?.forEach((i) => i.clear()), this._subscribers.delete(r);
|
|
143
|
+
}
|
|
144
|
+
emit(r, t, i) {
|
|
145
|
+
const e = this.getSubscribers(r.$id), n = (o) => console.error(`Error emitting event '${t}' for store '${r.$id}':`, o);
|
|
146
|
+
for (const o of e)
|
|
147
|
+
try {
|
|
148
|
+
const c = o.getSubscription(t, r);
|
|
149
|
+
if (typeof c == "function") {
|
|
150
|
+
const u = c(...i?.() ?? []);
|
|
151
|
+
u !== null && (typeof u == "object" || typeof u == "function") && "then" in u && typeof u.then == "function" && Promise.resolve(u).catch(n);
|
|
152
|
+
}
|
|
153
|
+
} catch (c) {
|
|
154
|
+
n(c);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
getSubscribers(r) {
|
|
158
|
+
return this._subscribers.get(r) ?? [];
|
|
159
|
+
}
|
|
160
|
+
subscribe(r, t) {
|
|
161
|
+
this._subscribers.has(r) || this._subscribers.set(r, []), this._subscribers.get(r)?.push(t);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function M(s) {
|
|
165
|
+
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");
|
|
166
|
+
}
|
|
167
|
+
const J = "pinia-plugin-subscription", O = (s, r, t) => `${s}/onAction:${r}#${t}`, I = (s) => `${s}/plugin`, f = {
|
|
168
|
+
onAction: O,
|
|
169
|
+
onActionAfter: (s, r, t) => `${O(s, r, t)}/after`,
|
|
170
|
+
onActionError: (s, r, t) => `${O(s, r, t)}/onError`,
|
|
171
|
+
onMutation: (s, r) => `${s}/onMutation#${r}`,
|
|
172
|
+
plugin: I,
|
|
173
|
+
pluginDeferred: (s, r, t) => `${I(s)}/deferred:${r}#${t}`,
|
|
174
|
+
reset: (s, r) => `${s}/reset#${r}`
|
|
175
|
+
}, Q = {
|
|
176
|
+
environment: "both",
|
|
177
|
+
hydration: "immediate",
|
|
178
|
+
order: 9
|
|
179
|
+
}, Y = 2500;
|
|
180
|
+
function Z(s) {
|
|
181
|
+
if (typeof window < "u" && typeof window.requestAnimationFrame == "function") {
|
|
182
|
+
window.requestAnimationFrame(() => globalThis.setTimeout(s, 0));
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
globalThis.setTimeout(s, 0);
|
|
186
|
+
}
|
|
187
|
+
class ut extends W {
|
|
188
|
+
constructor(t, i) {
|
|
189
|
+
super({ prefixLabelWithName: !0 });
|
|
190
|
+
h(this, "_hydrationScheduler");
|
|
191
|
+
h(this, "_name", "PluginSubscription");
|
|
192
|
+
h(this, "_onActionSubscriptions", /* @__PURE__ */ new Map());
|
|
193
|
+
h(this, "_onActionSubscriptionsTraces", /* @__PURE__ */ new Map());
|
|
194
|
+
h(this, "_actionSeq", 0);
|
|
195
|
+
h(this, "_onActionStores", /* @__PURE__ */ new Set());
|
|
196
|
+
h(this, "_onActionAfterSubscriptions", {});
|
|
197
|
+
h(this, "_onActionOnErrorSubscriptions", {});
|
|
198
|
+
h(this, "_onMutationSubscriptions", /* @__PURE__ */ new Map());
|
|
199
|
+
h(this, "_onMutationSubscriptionsTraces", /* @__PURE__ */ new Map());
|
|
200
|
+
h(this, "_onMutationUnsubscribes", /* @__PURE__ */ new Map());
|
|
201
|
+
h(this, "_options");
|
|
202
|
+
h(this, "_disposedStores", /* @__PURE__ */ new WeakSet());
|
|
203
|
+
h(this, "_subscribers", []);
|
|
204
|
+
h(this, "_subscribersDelivered", /* @__PURE__ */ new Set());
|
|
205
|
+
h(this, "_subscribersScheduled", /* @__PURE__ */ new Set());
|
|
206
|
+
h(this, "_subscriptions", []);
|
|
207
|
+
h(this, "_subscriptionsDelivered", /* @__PURE__ */ new Set());
|
|
208
|
+
h(this, "_subscriptionsScheduled", /* @__PURE__ */ new Set());
|
|
209
|
+
this._storeEventsSubscribersHandler = new V(), this._hydrationScheduler = i?.hydrationScheduler ?? Z, this._options = i, this._subscribers = t.sort((e, n) => (typeof e?.execution > "u" ? e.execution = { order: 9 } : e.execution && typeof e.execution?.order > "u" && (e.execution.order = 9), typeof n?.execution > "u" ? n.execution = { order: 9 } : n.execution && typeof n.execution?.order > "u" && (n.execution.order = 9), (e.execution.order ?? 0) - (n.execution?.order ?? 0))), this.initializeTracer();
|
|
210
|
+
}
|
|
211
|
+
set subscribers(t) {
|
|
212
|
+
this._subscribers = t;
|
|
213
|
+
}
|
|
214
|
+
get subscriptions() {
|
|
215
|
+
if (!M(this._subscriptions))
|
|
216
|
+
return this._subscriptions;
|
|
217
|
+
}
|
|
218
|
+
addOnActionAfterCallback(t, i, e) {
|
|
219
|
+
const n = A(t, i);
|
|
220
|
+
this._onActionAfterSubscriptions[n] || (this._onActionAfterSubscriptions[n] = []), this._onActionAfterSubscriptions[n]?.push(e);
|
|
221
|
+
}
|
|
222
|
+
addOnActionOnErrorCallback(t, i, e) {
|
|
223
|
+
const n = A(t, i);
|
|
224
|
+
this._onActionOnErrorSubscriptions[n] || (this._onActionOnErrorSubscriptions[n] = []), this._onActionOnErrorSubscriptions[n]?.push(e);
|
|
225
|
+
}
|
|
226
|
+
clearOnActionCallbacksSubscriptions(t) {
|
|
227
|
+
delete this._onActionAfterSubscriptions[t], delete this._onActionOnErrorSubscriptions[t];
|
|
228
|
+
}
|
|
229
|
+
clearStoreTracking(t) {
|
|
230
|
+
this._onActionSubscriptions.delete(t), this._onActionStores.delete(t), this._onMutationUnsubscribes.get(t)?.(), this._onMutationUnsubscribes.delete(t), this._onMutationSubscriptions.delete(t), this._onActionSubscriptionsTraces.delete(t.$id), this._onMutationSubscriptionsTraces.delete(t.$id);
|
|
231
|
+
const i = G(t);
|
|
232
|
+
for (const e of Array.from(this._subscribersDelivered))
|
|
233
|
+
e.startsWith(i) && this._subscribersDelivered.delete(e);
|
|
234
|
+
for (const e of Array.from(this._subscribersScheduled))
|
|
235
|
+
e.startsWith(i) && this._subscribersScheduled.delete(e);
|
|
236
|
+
for (const e of Array.from(this._subscriptionsDelivered))
|
|
237
|
+
e.startsWith(i) && this._subscriptionsDelivered.delete(e);
|
|
238
|
+
for (const e of Array.from(this._subscriptionsScheduled))
|
|
239
|
+
e.startsWith(i) && this._subscriptionsScheduled.delete(e);
|
|
240
|
+
for (const e of Object.keys(this._onActionAfterSubscriptions))
|
|
241
|
+
e.startsWith(i) && delete this._onActionAfterSubscriptions[e];
|
|
242
|
+
for (const e of Object.keys(this._onActionOnErrorSubscriptions))
|
|
243
|
+
e.startsWith(i) && delete this._onActionOnErrorSubscriptions[e];
|
|
244
|
+
this.emit(t, S.DISPOSE);
|
|
245
|
+
}
|
|
246
|
+
closeGroupWhenSettled(t, i, e, n) {
|
|
247
|
+
let o = !1;
|
|
248
|
+
const c = () => {
|
|
249
|
+
o || (o = !0, globalThis.clearTimeout(a), i());
|
|
250
|
+
}, u = this._options?.deferredTraceGroupTimeout ?? Y, a = globalThis.setTimeout(() => {
|
|
251
|
+
o || (this.traceError(
|
|
252
|
+
"subscriber:deferred:timeout",
|
|
253
|
+
new Error(`Subscriber "${n}" hydration still pending after ${u}ms`),
|
|
254
|
+
e
|
|
255
|
+
), c());
|
|
256
|
+
}, u);
|
|
257
|
+
t.then(c, c);
|
|
258
|
+
}
|
|
259
|
+
createOnActionAfterCallbackCollector(t, i) {
|
|
260
|
+
return (e) => {
|
|
261
|
+
this.addOnActionAfterCallback(t, i, e);
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
createOnActionOnErrorCallbackCollector(t, i) {
|
|
265
|
+
return (e) => {
|
|
266
|
+
this.addOnActionOnErrorCallback(t, i, e);
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
defineCurrentEnvironment() {
|
|
270
|
+
return this._options?.runtimeEnvironment ?? (typeof window > "u" ? "server" : "client");
|
|
271
|
+
}
|
|
272
|
+
definePluginExecution(t) {
|
|
273
|
+
return {
|
|
274
|
+
...Q,
|
|
275
|
+
...this._options?.execution ?? {},
|
|
276
|
+
...t.execution ?? {},
|
|
277
|
+
...this._options?.subscriberExecution?.[t.name] ?? {}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
defineHydrationScheduler(t) {
|
|
281
|
+
return t.hydrationScheduler ?? this._hydrationScheduler;
|
|
282
|
+
}
|
|
283
|
+
defineSubscriberKey(t, i) {
|
|
284
|
+
return m(t.name, i);
|
|
285
|
+
}
|
|
286
|
+
defineSubscriptionKey(t, i) {
|
|
287
|
+
return m(t, i);
|
|
288
|
+
}
|
|
289
|
+
endGroupAfter(t, i) {
|
|
290
|
+
const e = t.filter((n) => !!n);
|
|
291
|
+
e.length ? Promise.all(e).then(i) : i();
|
|
292
|
+
}
|
|
293
|
+
executeStoreOnActionSubscription(t, i = !1) {
|
|
294
|
+
this._onActionStores.has(t) || typeof t.$onAction != "function" || !this._onActionSubscriptions.get(t)?.size && !this.hasSubscribers(t.$id) && !i || (this._onActionStores.add(t), t.$onAction(({ after: e, args: n, name: o, onError: c }) => {
|
|
295
|
+
const u = this.hasSubscribers(t.$id) ? Symbol(A(o, t)) : void 0, a = this._onActionSubscriptions.get(t);
|
|
296
|
+
if (!a?.size && !u)
|
|
297
|
+
return;
|
|
298
|
+
this.emit(t, S.ACTION_BEFORE, () => [o, u]);
|
|
299
|
+
const d = ++this._actionSeq, p = `${t.$id}#${d}`, b = A(o, t), y = f.onAction(t.$id, o, d);
|
|
300
|
+
a?.size && this.traceGroupStart(() => ({ actionId: p, args: n, groupId: y }), t.$id, `store:onAction:${o}`);
|
|
301
|
+
let k = !1;
|
|
302
|
+
const x = () => {
|
|
303
|
+
a?.size && !k && (k = !0, this.traceGroupEnd({ groupId: y }, t.$id));
|
|
304
|
+
};
|
|
305
|
+
try {
|
|
306
|
+
const _ = [];
|
|
307
|
+
a?.forEach((H) => {
|
|
308
|
+
const v = this.runCallbackSafely(
|
|
309
|
+
() => H({
|
|
310
|
+
after: this.createOnActionAfterCallbackCollector(o, t),
|
|
311
|
+
args: n,
|
|
312
|
+
name: o,
|
|
313
|
+
onError: this.createOnActionOnErrorCallbackCollector(o, t)
|
|
314
|
+
}),
|
|
315
|
+
"store:onAction:callback:error",
|
|
316
|
+
t.$id,
|
|
317
|
+
() => ({ actionId: p, groupId: y })
|
|
318
|
+
);
|
|
319
|
+
v && _.push(v);
|
|
320
|
+
}), this.endGroupAfter(_, x), this.executeStoreOnActionAfterCallbacks(e, b, t, o, p, d, u), this.executeStoreOnActionOnErrorCallbacks(c, b, t, o, p, d, u), this.clearOnActionCallbacksSubscriptions(b);
|
|
321
|
+
} catch (_) {
|
|
322
|
+
throw this.emit(t, S.ACTION_ERROR, () => [o, _, u]), x(), _;
|
|
323
|
+
}
|
|
324
|
+
}));
|
|
325
|
+
}
|
|
326
|
+
executeStoreOnActionAfterCallbacks(t, i, e, n, o, c, u) {
|
|
327
|
+
const a = this._onActionAfterSubscriptions[i];
|
|
328
|
+
(a?.length || u) && t((d) => {
|
|
329
|
+
if (u && !this._disposedStores.has(e) && this.emit(e, S.ACTION_AFTER, () => [n, u]), !a?.length)
|
|
330
|
+
return;
|
|
331
|
+
this.traceGroupStart(
|
|
332
|
+
() => ({
|
|
333
|
+
actionId: o,
|
|
334
|
+
groupId: f.onActionAfter(e.$id, n, c),
|
|
335
|
+
result: d
|
|
336
|
+
}),
|
|
337
|
+
e.$id,
|
|
338
|
+
`store:onAction:after:${n}`
|
|
339
|
+
);
|
|
340
|
+
const p = a.map((b) => this.runCallbackSafely(
|
|
341
|
+
() => b(d),
|
|
342
|
+
"storeOnActionAfterCallbacks",
|
|
343
|
+
e.$id,
|
|
344
|
+
() => ({ actionId: o, "action name": n, groupId: f.onActionAfter(e.$id, n, c) })
|
|
345
|
+
));
|
|
346
|
+
this.endGroupAfter(p, () => this.traceGroupEnd(() => ({ groupId: f.onActionAfter(e.$id, n, c) }), e.$id));
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
executeStoreOnActionOnErrorCallbacks(t, i, e, n, o, c, u) {
|
|
350
|
+
const a = this._onActionOnErrorSubscriptions[i];
|
|
351
|
+
(a?.length || u) && t((d) => {
|
|
352
|
+
if (u && !this._disposedStores.has(e) && this.emit(e, S.ACTION_ON_ERROR, () => [n, d, u]), !a?.length)
|
|
353
|
+
return;
|
|
354
|
+
this.traceGroupStart(
|
|
355
|
+
() => ({
|
|
356
|
+
actionId: o,
|
|
357
|
+
"action name": n,
|
|
358
|
+
error: d,
|
|
359
|
+
groupId: f.onActionError(e.$id, n, c)
|
|
360
|
+
}),
|
|
361
|
+
e.$id,
|
|
362
|
+
`store:onAction:onError:${n}`
|
|
363
|
+
);
|
|
364
|
+
const p = a.map((b) => this.runCallbackSafely(
|
|
365
|
+
() => b(d),
|
|
366
|
+
"store:onAction:onError:error",
|
|
367
|
+
e.$id,
|
|
368
|
+
() => ({ actionId: o, groupId: f.onActionError(e.$id, n, c) })
|
|
369
|
+
));
|
|
370
|
+
this.endGroupAfter(p, () => this.traceGroupEnd(() => ({ groupId: f.onActionError(e.$id, n, c) }), e.$id));
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
executeSubscriber(t, i) {
|
|
374
|
+
const e = this.defineSubscriberKey(i, t.store);
|
|
375
|
+
if (this._subscribersDelivered.has(e) || !i.invoke(t, {
|
|
376
|
+
createTracer: this._options?.tracerHandler?.createTracer,
|
|
377
|
+
eventsHandler: this._storeEventsSubscribersHandler
|
|
378
|
+
}))
|
|
379
|
+
return;
|
|
380
|
+
this.emit(t.store, q.INVOKED, () => [i]), this._subscribersDelivered.add(e);
|
|
381
|
+
let n;
|
|
382
|
+
try {
|
|
383
|
+
n = i.hydrate?.(t);
|
|
384
|
+
} catch (u) {
|
|
385
|
+
this.traceError("subscriber:hydrate", u, t.store.$id);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const o = () => {
|
|
389
|
+
try {
|
|
390
|
+
const u = i.afterHydration?.(t);
|
|
391
|
+
if (u && typeof u.then == "function")
|
|
392
|
+
return u.catch(
|
|
393
|
+
(a) => this.traceError("subscriber:afterHydration", a, t.store.$id)
|
|
394
|
+
);
|
|
395
|
+
} catch (u) {
|
|
396
|
+
this.traceError("subscriber:afterHydration", u, t.store.$id);
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
let c;
|
|
400
|
+
return n && typeof n.then == "function" ? c = n.then(() => o()).catch((u) => this.traceError("subscriber:hydrate", u, t.store.$id)) : c = o(), i.subscriptions && this.subscriptionDelivery(t, i.subscriptions), i.storeMutationSubscription && this.storeMutationSubscription(i, i.storeMutationSubscription), this.storeOnActionSubscription(i, t), c;
|
|
401
|
+
}
|
|
402
|
+
executeSubscriberSafely(t, i) {
|
|
403
|
+
try {
|
|
404
|
+
return this.executeSubscriber(t, i);
|
|
405
|
+
} catch (e) {
|
|
406
|
+
this.traceError("subscriber:execute", e, t.store.$id, () => ({
|
|
407
|
+
subscriber: i.name
|
|
408
|
+
}));
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
initializeTracer() {
|
|
412
|
+
this._options?.tracerHandler && (this._tracer = this._options.tracerHandler.createTracer(J));
|
|
413
|
+
}
|
|
414
|
+
plugin({ store: t, options: i }) {
|
|
415
|
+
if (!this._subscribers.length)
|
|
416
|
+
return;
|
|
417
|
+
const e = {
|
|
418
|
+
$onAction: [],
|
|
419
|
+
$subscribe: []
|
|
420
|
+
}, n = f.plugin(t.$id), o = () => ({
|
|
421
|
+
groupId: n,
|
|
422
|
+
subscribers: this._subscribers.map((c) => ({
|
|
423
|
+
name: c.name,
|
|
424
|
+
execution: c.execution,
|
|
425
|
+
pluginOptions: c.pluginOptions
|
|
426
|
+
})),
|
|
427
|
+
"hooks subscriptions": e,
|
|
428
|
+
store: t,
|
|
429
|
+
options: i
|
|
430
|
+
});
|
|
431
|
+
this.traceGroupStart(() => ({ groupId: n }), t.$id, void 0, !1), this.emit(t, $.EXECUTION.START, () => [this._subscribers]), this._onActionSubscriptionsTraces.set(t.$id, e.$onAction), this._onMutationSubscriptionsTraces.set(t.$id, e.$subscribe);
|
|
432
|
+
try {
|
|
433
|
+
let c = !1;
|
|
434
|
+
this._subscribers.forEach(
|
|
435
|
+
(u) => {
|
|
436
|
+
const a = this.defineSubscriberKey(u, t), d = this.definePluginExecution(u);
|
|
437
|
+
if (!(this._subscribersDelivered.has(a) || this._subscribersScheduled.has(a)) && this.shouldInvokePlugin(d)) {
|
|
438
|
+
if (this.shouldScheduleAfterHydration(d)) {
|
|
439
|
+
c = !0, this.queueSubscriberExecution({ store: t, options: i }, u, a);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
this.executeSubscriber({ store: t, options: i }, u);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
), this.executeStoreOnActionSubscription(t, c), this.registerStoreCleanup(t);
|
|
446
|
+
} catch (c) {
|
|
447
|
+
this.traceError("plugin:invoke", c, t.$id), this.emit(t, $.EXECUTION.ERROR, () => [c]);
|
|
448
|
+
}
|
|
449
|
+
this.trace("context", t.$id, o), this.traceGroupEnd(() => ({ groupId: n }), t.$id), this.emit(t, $.EXECUTION.END);
|
|
450
|
+
}
|
|
451
|
+
queueSubscriberExecution(t, i, e) {
|
|
452
|
+
const n = this.defineHydrationScheduler(i);
|
|
453
|
+
this._subscribersScheduled.add(e), n(() => {
|
|
454
|
+
if (this._disposedStores.has(t.store))
|
|
455
|
+
return;
|
|
456
|
+
this._subscribersScheduled.delete(e);
|
|
457
|
+
const o = t.store.$id, c = f.pluginDeferred(o, i.name, ++this._actionSeq);
|
|
458
|
+
this.traceGroupStart(() => ({ groupId: c, subscriber: i.name }), o, "subscriber:deferred");
|
|
459
|
+
const u = () => this.traceGroupEnd(() => ({ groupId: c }), o);
|
|
460
|
+
let a;
|
|
461
|
+
try {
|
|
462
|
+
a = this.executeSubscriberSafely(t, i);
|
|
463
|
+
} finally {
|
|
464
|
+
a ? this.closeGroupWhenSettled(a, u, o, i.name) : u();
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
registerStoreCleanup(t) {
|
|
469
|
+
if (typeof t.$dispose != "function")
|
|
470
|
+
return;
|
|
471
|
+
const i = t.$dispose.bind(t), e = t;
|
|
472
|
+
e.__piniaPluginSubscriptionDisposed || (e.__piniaPluginSubscriptionDisposed = !0, t.$dispose = () => (this._disposedStores.add(t), this.clearStoreTracking(t), this.clearSubscribers(t.$id), i()));
|
|
473
|
+
}
|
|
474
|
+
runCallbackSafely(t, i, e, n) {
|
|
475
|
+
try {
|
|
476
|
+
const o = t();
|
|
477
|
+
if (o && typeof o.then == "function")
|
|
478
|
+
return Promise.resolve(o).then(
|
|
479
|
+
() => {
|
|
480
|
+
},
|
|
481
|
+
(c) => this.traceError(i, c, e, n)
|
|
482
|
+
);
|
|
483
|
+
} catch (o) {
|
|
484
|
+
this.traceError(i, o, e, n);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
shouldInvokePlugin(t) {
|
|
488
|
+
const i = this.defineCurrentEnvironment();
|
|
489
|
+
return t.environment === "both" || t.environment === i;
|
|
490
|
+
}
|
|
491
|
+
shouldScheduleAfterHydration(t) {
|
|
492
|
+
return this.defineCurrentEnvironment() === "client" && t.hydration === "defer";
|
|
493
|
+
}
|
|
494
|
+
storeOnActionSubscription(t, i) {
|
|
495
|
+
const { store: e, callback: n } = t?.storeOnActionSubscription?.(i) ?? {};
|
|
496
|
+
!e || !n || (this._onActionSubscriptions.has(e) || this._onActionSubscriptions.set(e, /* @__PURE__ */ new Set()), this._onActionSubscriptions.get(e).add(n), this._onActionSubscriptionsTraces.get(e.$id)?.push(t.name));
|
|
497
|
+
}
|
|
498
|
+
storeMutationSubscription(t, i) {
|
|
499
|
+
const { store: e, callback: n } = i();
|
|
500
|
+
if (!e || !n || (this._onMutationSubscriptions.has(e) || this._onMutationSubscriptions.set(e, /* @__PURE__ */ new Set()), this._onMutationSubscriptions.get(e).add(n), this._onMutationSubscriptionsTraces.get(e.$id)?.push(t.name), this._onMutationUnsubscribes.has(e)))
|
|
501
|
+
return;
|
|
502
|
+
const o = e.$subscribe(async (c) => {
|
|
503
|
+
const u = this._onMutationSubscriptions.get(e);
|
|
504
|
+
if (!u?.size)
|
|
505
|
+
return;
|
|
506
|
+
const a = f.onMutation(e.$id, ++this._actionSeq);
|
|
507
|
+
this.traceGroupStart(() => ({ mutation: c, groupId: a }), e.$id, "store:onMutation"), this.emit(e, S.MUTATION, () => [c]);
|
|
508
|
+
try {
|
|
509
|
+
await Promise.all([...u].map(async (d) => {
|
|
510
|
+
try {
|
|
511
|
+
await d(c);
|
|
512
|
+
} catch (p) {
|
|
513
|
+
this.traceError("store:onMutation", p, e.$id, () => ({ groupId: a }));
|
|
514
|
+
}
|
|
515
|
+
}));
|
|
516
|
+
} finally {
|
|
517
|
+
this.traceGroupEnd(() => ({ groupId: a }), e.$id);
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
this._onMutationUnsubscribes.set(e, o);
|
|
521
|
+
}
|
|
522
|
+
subscriptionDelivery(t, i) {
|
|
523
|
+
Object.entries(i).forEach(([e, n]) => {
|
|
524
|
+
const o = this.defineSubscriptionKey(e, t.store), c = this.definePluginExecution(n.subscription);
|
|
525
|
+
if (!(this._subscriptionsDelivered.has(o) || this._subscriptionsScheduled.has(o)) && this.shouldInvokePlugin(c)) {
|
|
526
|
+
if (this.shouldScheduleAfterHydration(c)) {
|
|
527
|
+
const u = this.defineHydrationScheduler(n.subscription);
|
|
528
|
+
this._subscriptionsScheduled.add(o), u(() => {
|
|
529
|
+
this._disposedStores.has(t.store) || (this._subscriptionsScheduled.delete(o), this.subscriptionDeliverySafely(t, e, n));
|
|
530
|
+
});
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
this.subscriptionDeliverySafely(t, e, n);
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
subscriptionDeliverySafely({ store: t, options: i }, e, n) {
|
|
538
|
+
try {
|
|
539
|
+
if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(e, t)))
|
|
540
|
+
return;
|
|
541
|
+
const { subscription: o, subscriptionOptions: c, stores: u } = n;
|
|
542
|
+
this.trace("Subscription:delivery", t.$id, () => ({
|
|
543
|
+
groupId: f.plugin(t.$id),
|
|
544
|
+
pluginName: e,
|
|
545
|
+
subscription: o,
|
|
546
|
+
subscriptionOptions: c
|
|
547
|
+
}));
|
|
548
|
+
const a = {
|
|
549
|
+
storeOptions: { ...i?.storeOptions ?? {}, ...c }
|
|
550
|
+
};
|
|
551
|
+
o.invoke(
|
|
552
|
+
{ store: t, options: { ...i, ...a } }
|
|
553
|
+
), this._subscriptionsDelivered.add(this.defineSubscriptionKey(e, t)), M(u) || u?.forEach(
|
|
554
|
+
(d) => o.invoke({
|
|
555
|
+
store: d,
|
|
556
|
+
options: {
|
|
557
|
+
...i,
|
|
558
|
+
...a,
|
|
559
|
+
...d?.storeOptions ?? {}
|
|
560
|
+
}
|
|
561
|
+
})
|
|
562
|
+
);
|
|
563
|
+
} catch (o) {
|
|
564
|
+
this.traceError("subscription:delivery", o, t.$id, () => ({ groupId: f.plugin(t.$id) }));
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
traceGroupEnd(t, i) {
|
|
568
|
+
const e = this._options?.tracerHandler;
|
|
569
|
+
e && (e.hasTraceListeners?.() ?? !0) && e.closeGroup({
|
|
570
|
+
...typeof t == "function" ? t() : t ?? {},
|
|
571
|
+
scope: i
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
traceGroupStart(t, i, e, n = !0) {
|
|
575
|
+
const o = this._options?.tracerHandler;
|
|
576
|
+
o && (o.hasTraceListeners?.() ?? !0) && (t = typeof t == "function" ? t() : t ?? {}, o.startGroup({
|
|
577
|
+
...t,
|
|
578
|
+
scope: i
|
|
579
|
+
}), n && this.trace(e ?? "context", i, t));
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
export {
|
|
583
|
+
W as E,
|
|
584
|
+
$ as P,
|
|
585
|
+
S,
|
|
586
|
+
ut as a,
|
|
587
|
+
q as b,
|
|
588
|
+
A as c,
|
|
589
|
+
G as d,
|
|
590
|
+
it as e,
|
|
591
|
+
rt as f,
|
|
592
|
+
nt as g,
|
|
593
|
+
st as h,
|
|
594
|
+
M as i,
|
|
595
|
+
ot as j,
|
|
596
|
+
J as p,
|
|
597
|
+
ct as s,
|
|
598
|
+
f as t
|
|
599
|
+
};
|