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.
- package/CHANGELOG.md +73 -12
- package/CONTRIBUTING.md +10 -1
- package/README.md +48 -293
- package/SECURITY.md +6 -6
- package/dist/chunks/setup-context-3G3Hy1ep.js +909 -0
- package/dist/dictionaries/plugin-subscription-event.d.ts +20 -0
- package/dist/{core/Store.d.ts → enhanced-store/EnhancedStore.d.ts} +24 -29
- package/dist/enhanced-store/define-a-store.d.ts +64 -0
- package/dist/enhanced-store/setup-context.d.ts +27 -0
- package/dist/events/EventsEmitter.d.ts +9 -0
- package/dist/events/EventsSubscriber.d.ts +32 -0
- package/dist/events/EventsSubscription.d.ts +13 -0
- package/dist/events/StoreEvent.d.ts +6 -0
- package/dist/events/StoresEventsSubscriptionsRegistry.d.ts +31 -0
- package/dist/events/event.d.ts +8 -0
- package/dist/factories/events-registry.d.ts +9 -0
- package/dist/factories/events-subscriber.d.ts +11 -0
- package/dist/factories/messenger-registry.d.ts +16 -0
- package/dist/factories/plugin-subscriber.d.ts +17 -0
- package/dist/helpers.js +204 -312
- package/dist/lib/helpers.d.ts +9 -0
- package/dist/lib/main.d.ts +3 -12
- package/dist/orchestration/ExecutionPolicy.d.ts +15 -0
- package/dist/orchestration/PluginOrchestrator.d.ts +41 -0
- package/dist/orchestration/SubscriberScheduler.d.ts +31 -0
- package/dist/pinia-plugin-subscription.js +31 -426
- package/dist/plugins/createPlugin.d.ts +3 -13
- package/dist/services/messenger.d.ts +28 -0
- package/dist/services/messengerProvider.d.ts +45 -0
- package/dist/store-hooks/StoreHooksBinder.d.ts +20 -0
- package/dist/store-hooks/StoreHooksRegistry.d.ts +34 -0
- package/dist/{core/PluginSubscriber.d.ts → subscribers/StoreSubscriber.d.ts} +39 -28
- package/dist/tracing/TraceGroupHelper.d.ts +27 -0
- package/dist/tracing/TraceableEmitter.d.ts +60 -0
- package/dist/tracing/TraceableEventsSubscriber.d.ts +21 -0
- package/dist/tracing/TraceableStoreEventsSubscriber.d.ts +16 -0
- package/dist/tracing/trace-groups.d.ts +9 -0
- package/dist/types/core.d.ts +12 -0
- package/dist/types/events.d.ts +24 -0
- package/dist/types/index.d.ts +8 -34
- package/dist/types/messenger.d.ts +20 -0
- package/dist/types/plugin.d.ts +39 -20
- package/dist/types/store.d.ts +1 -9
- package/dist/types/trace.d.ts +35 -75
- package/dist/utils/constants.d.ts +4 -0
- package/dist/utils/events.d.ts +3 -0
- package/dist/utils/id.d.ts +20 -0
- package/dist/utils/store.d.ts +9 -93
- package/docs/authoring-plugins.md +330 -78
- package/docs/using-plugins.md +89 -53
- package/package.json +14 -8
- package/dist/chunks/constantes-CaTNAbzX.js +0 -261
- package/dist/chunks/constantes-CaTNAbzX.js.map +0 -1
- package/dist/core/PluginSubscription.d.ts +0 -92
- package/dist/core/RollbackStateSnapshotsHandler.d.ts +0 -65
- package/dist/factories/console-trace-listener.d.ts +0 -7
- package/dist/factories/state-snapshot.d.ts +0 -3
- package/dist/factories/trace-listener.d.ts +0 -16
- package/dist/factories/trace-registry.d.ts +0 -17
- package/dist/helpers/index.d.ts +0 -9
- package/dist/helpers.js.map +0 -1
- package/dist/pinia-plugin-subscription.js.map +0 -1
- package/dist/plugins/createHydrationPlugin.d.ts +0 -10
- package/dist/system/Tracer.d.ts +0 -20
- package/dist/system/log.d.ts +0 -35
- package/dist/tests/defineAStore.test-d.d.ts +0 -1
- package/dist/tests/storeOptionsExtensions.test-d.d.ts +0 -12
- package/dist/types/comparison.d.ts +0 -3
- package/dist/types/list.d.ts +0 -13
- package/dist/types/log.d.ts +0 -8
- package/dist/types/resourceId.d.ts +0 -4
- package/dist/utils/Performance.d.ts +0 -9
- package/dist/utils/constantes.d.ts +0 -1
- package/dist/utils/deep-clone.d.ts +0 -7
- package/dist/utils/defineStoreId.d.ts +0 -1
- package/dist/utils/object.d.ts +0 -4
- package/dist/utils/validation/object.d.ts +0 -9
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { PiniaPluginContext, Store } from "pinia";
|
|
2
|
+
import type { TraceReporter } from "../types/core";
|
|
3
|
+
import type { StoreSubscriberInterface, PluginOrchestrator, PluginSubscriptions } from "../types/plugin";
|
|
4
|
+
import type ExecutionPolicy from "./ExecutionPolicy";
|
|
5
|
+
import type StoreHooksRegistry from "../store-hooks/StoreHooksRegistry";
|
|
6
|
+
import type TraceGroupHelper from "../tracing/TraceGroupHelper";
|
|
7
|
+
/** Delivers subscriptions to plugins and tracks which subscribers/subscriptions already ran or are scheduled. */
|
|
8
|
+
export default class SubscriberScheduler {
|
|
9
|
+
private _groups;
|
|
10
|
+
private _policy;
|
|
11
|
+
private _registry;
|
|
12
|
+
private _reporter;
|
|
13
|
+
private _stores;
|
|
14
|
+
constructor(policy: ExecutionPolicy, registry: StoreHooksRegistry, groups: TraceGroupHelper, reporter: TraceReporter);
|
|
15
|
+
clearStore(store: Store): void;
|
|
16
|
+
defineSubscriberKey(subscriber: StoreSubscriberInterface, store: Store): string;
|
|
17
|
+
defineSubscriptionKey(pluginName: string, store: Store): string;
|
|
18
|
+
isSubscriberDelivered(store: Store, subscriberKey: string): boolean;
|
|
19
|
+
/** A subscriber is handled once it ran or while its deferred run is scheduled. */
|
|
20
|
+
isSubscriberHandled(store: Store, subscriberKey: string): boolean;
|
|
21
|
+
markSubscriberDelivered(store: Store, subscriberKey: string): void;
|
|
22
|
+
isSubscriptionScheduled(store: Store, subscriptionKey: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Schedules the subscriber execution once the store is hydrated.
|
|
25
|
+
* The execution is wrapped in a trace group, kept open until async hydration hooks settle.
|
|
26
|
+
*/
|
|
27
|
+
queueSubscriberExecution(context: PiniaPluginContext, subscriber: StoreSubscriberInterface, subscriberKey: string, execute: (context: PiniaPluginContext, subscriber: StoreSubscriberInterface) => Promise<unknown> | undefined): void;
|
|
28
|
+
subscriptionDelivery(context: PiniaPluginContext, pluginSubscriptions: PluginSubscriptions): void;
|
|
29
|
+
subscriptionDeliverySafely({ store, options }: PiniaPluginContext, pluginName: string, pluginSubscription: PluginOrchestrator): void;
|
|
30
|
+
private getStoreState;
|
|
31
|
+
}
|
|
@@ -1,432 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function h(r) {
|
|
8
|
-
if (typeof structuredClone == "function")
|
|
9
|
-
try {
|
|
10
|
-
return structuredClone(r);
|
|
11
|
-
} catch {
|
|
12
|
-
}
|
|
13
|
-
if (r === null || typeof r != "object")
|
|
14
|
-
return r;
|
|
15
|
-
if (r instanceof Map)
|
|
16
|
-
return new Map(
|
|
17
|
-
Array.from(r.entries(), ([e, i]) => [h(e), h(i)])
|
|
18
|
-
);
|
|
19
|
-
if (r instanceof Set)
|
|
20
|
-
return new Set(Array.from(r.values(), (e) => h(e)));
|
|
21
|
-
if (Array.isArray(r))
|
|
22
|
-
return r.map((e) => h(e));
|
|
23
|
-
if (Object.getPrototypeOf(r) === Object.prototype || Object.getPrototypeOf(r) === null)
|
|
24
|
-
return Object.fromEntries(
|
|
25
|
-
Object.entries(r).map(([e, i]) => [e, h(i)])
|
|
26
|
-
);
|
|
27
|
-
const t = Object.create(Object.getPrototypeOf(r));
|
|
28
|
-
for (const [e, i] of Object.entries(r))
|
|
29
|
-
t[e] = h(i);
|
|
30
|
-
return t;
|
|
1
|
+
import { b as u, f as o } from "./chunks/setup-context-3G3Hy1ep.js";
|
|
2
|
+
import { p as A, j as g } from "./chunks/setup-context-3G3Hy1ep.js";
|
|
3
|
+
import { defineStore as c } from "pinia";
|
|
4
|
+
function i(r, e) {
|
|
5
|
+
const t = new u(r, e);
|
|
6
|
+
return t.plugin.bind(t);
|
|
31
7
|
}
|
|
32
|
-
function
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
8
|
+
function S(r, e, t) {
|
|
9
|
+
const n = t ? { storeOptions: t } : {};
|
|
10
|
+
return typeof e == "function" ? o(
|
|
11
|
+
r,
|
|
12
|
+
e,
|
|
13
|
+
n
|
|
14
|
+
) : p(
|
|
15
|
+
r,
|
|
16
|
+
e,
|
|
17
|
+
n
|
|
18
|
+
);
|
|
42
19
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
typeof t.$rollbackAfterFailure != "function" && (t.$rollbackAfterFailure = async ({ action: e, stateKeys: i }, s = []) => {
|
|
51
|
-
const n = t[e];
|
|
52
|
-
if (typeof n != "function" || O(e))
|
|
53
|
-
return;
|
|
54
|
-
const o = !i || i.length === 0 ? "all" : i, a = this.saveStateSnapshot(t, e, o);
|
|
55
|
-
try {
|
|
56
|
-
await n.apply(t, s);
|
|
57
|
-
} catch (u) {
|
|
58
|
-
throw this.patchStore(a, t), u;
|
|
59
|
-
} finally {
|
|
60
|
-
this.removeStateSnapshot(a);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
createOnActionCallbacks(t, e) {
|
|
65
|
-
const i = this._storeRollbackSnapshotsParams.get(
|
|
66
|
-
b(t, e)
|
|
67
|
-
);
|
|
68
|
-
if (!i)
|
|
69
|
-
return;
|
|
70
|
-
const s = i.length === 0 ? "all" : i, n = this.saveStateSnapshot(e, t, s);
|
|
71
|
-
return {
|
|
72
|
-
after: () => {
|
|
73
|
-
this.removeStateSnapshot(n);
|
|
74
|
-
},
|
|
75
|
-
onError: () => {
|
|
76
|
-
this.patchStore(n, e), this.removeStateSnapshot(n);
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
initFromPluginContext({ options: t, store: e }) {
|
|
81
|
-
const i = t?.storeOptions?.rollbackAfterFailure;
|
|
82
|
-
i && !this._storeHasRollbackSnapshots.has(e.$id) && (Object.keys(i).forEach((s) => {
|
|
83
|
-
this._storeRollbackSnapshotsParams.set(
|
|
84
|
-
b(s, e),
|
|
85
|
-
i[s]
|
|
86
|
-
);
|
|
87
|
-
}), this._storeHasRollbackSnapshots.add(e.$id)), this.addRollbackAction(e);
|
|
88
|
-
}
|
|
89
|
-
clearStoreTracking(t) {
|
|
90
|
-
const e = `-${t.$id}`;
|
|
91
|
-
for (const i of Array.from(this._storeRollbackSnapshotsParams.keys()))
|
|
92
|
-
i.endsWith(e) && this._storeRollbackSnapshotsParams.delete(i);
|
|
93
|
-
this._storeHasRollbackSnapshots.delete(t.$id);
|
|
94
|
-
for (const i of Array.from(this._stateSnapshots.keys()))
|
|
95
|
-
i.description?.startsWith(`${t.$id}:`) && this._stateSnapshots.delete(i);
|
|
96
|
-
}
|
|
97
|
-
patchStore(t, e) {
|
|
98
|
-
const i = this._stateSnapshots.get(t);
|
|
99
|
-
if (i)
|
|
100
|
-
try {
|
|
101
|
-
e.$patch(i);
|
|
102
|
-
} catch (s) {
|
|
103
|
-
console.error(`Failed to patch store with snapshot ${String(t)}`, s);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
removeStateSnapshot(t) {
|
|
107
|
-
t && this._stateSnapshots.delete(t);
|
|
108
|
-
}
|
|
109
|
-
saveStateSnapshot(t, e, i) {
|
|
110
|
-
const s = m(
|
|
111
|
-
t.$state,
|
|
112
|
-
i
|
|
113
|
-
), n = /* @__PURE__ */ Symbol(`${t.$id}:${e}`);
|
|
114
|
-
return this._stateSnapshots.set(n, s), n;
|
|
115
|
-
}
|
|
116
|
-
storeHasRollbackSnapshots(t) {
|
|
117
|
-
return this._storeHasRollbackSnapshots.has(t.$id);
|
|
118
|
-
}
|
|
20
|
+
function a(r, e, t) {
|
|
21
|
+
const n = t ? { storeOptions: { ...t, enhancedStore: !0 } } : { storeOptions: { enhancedStore: !0 } };
|
|
22
|
+
return o(
|
|
23
|
+
r,
|
|
24
|
+
e,
|
|
25
|
+
n
|
|
26
|
+
);
|
|
119
27
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
hydration: "immediate"
|
|
123
|
-
};
|
|
124
|
-
function R(r) {
|
|
125
|
-
if (typeof window < "u" && typeof window.requestAnimationFrame == "function") {
|
|
126
|
-
window.requestAnimationFrame(() => globalThis.setTimeout(r, 0));
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
globalThis.setTimeout(r, 0);
|
|
130
|
-
}
|
|
131
|
-
class A {
|
|
132
|
-
constructor(t, e) {
|
|
133
|
-
c(this, "_className", $);
|
|
134
|
-
c(this, "_hydrationScheduler");
|
|
135
|
-
c(this, "_onActionSubscriptions", /* @__PURE__ */ new Map());
|
|
136
|
-
c(this, "_onActionStores", /* @__PURE__ */ new Set());
|
|
137
|
-
c(this, "_onActionAfterSubscriptions", {});
|
|
138
|
-
c(this, "_onActionOnErrorSubscriptions", {});
|
|
139
|
-
c(this, "_options");
|
|
140
|
-
c(this, "_resetStoreCallback", /* @__PURE__ */ new Map());
|
|
141
|
-
c(this, "_disposedStores", /* @__PURE__ */ new WeakSet());
|
|
142
|
-
c(this, "_storeRollbackSnapshotsHandler", new C());
|
|
143
|
-
c(this, "_subscribers", []);
|
|
144
|
-
c(this, "_subscribersDelivered", /* @__PURE__ */ new Set());
|
|
145
|
-
c(this, "_subscribersScheduled", /* @__PURE__ */ new Set());
|
|
146
|
-
c(this, "_subscriptions", []);
|
|
147
|
-
c(this, "_subscriptionsDelivered", /* @__PURE__ */ new Set());
|
|
148
|
-
c(this, "_subscriptionsScheduled", /* @__PURE__ */ new Set());
|
|
149
|
-
c(this, "_tracer");
|
|
150
|
-
this._hydrationScheduler = e?.hydrationScheduler ?? R, this._options = e, this._subscribers = t, this.initializeTracer();
|
|
151
|
-
}
|
|
152
|
-
set subscribers(t) {
|
|
153
|
-
this._subscribers = t;
|
|
154
|
-
}
|
|
155
|
-
get subscriptions() {
|
|
156
|
-
if (!_(this._subscriptions))
|
|
157
|
-
return this._subscriptions;
|
|
158
|
-
}
|
|
159
|
-
addOnActionAfterCallback(t, e, i) {
|
|
160
|
-
const s = b(t, e);
|
|
161
|
-
this._onActionAfterSubscriptions[s] || (this._onActionAfterSubscriptions[s] = []), this._onActionAfterSubscriptions[s]?.push(i);
|
|
162
|
-
}
|
|
163
|
-
addOnActionOnErrorCallback(t, e, i) {
|
|
164
|
-
const s = b(t, e);
|
|
165
|
-
this._onActionOnErrorSubscriptions[s] || (this._onActionOnErrorSubscriptions[s] = []), this._onActionOnErrorSubscriptions[s]?.push(i);
|
|
166
|
-
}
|
|
167
|
-
addResetStoreCallback(t, e) {
|
|
168
|
-
this._resetStoreCallback.has(t) || this._resetStoreCallback.set(t, /* @__PURE__ */ new Set()), this._resetStoreCallback.get(t).add(e);
|
|
169
|
-
}
|
|
170
|
-
clearOnActionCallbacksSubscriptions(t) {
|
|
171
|
-
delete this._onActionAfterSubscriptions[t], delete this._onActionOnErrorSubscriptions[t];
|
|
172
|
-
}
|
|
173
|
-
clearStoreTracking(t) {
|
|
174
|
-
this._onActionSubscriptions.delete(t), this._onActionStores.delete(t), this._resetStoreCallback.delete(t);
|
|
175
|
-
const e = `-${t.$id}`;
|
|
176
|
-
for (const i of Array.from(this._subscribersDelivered))
|
|
177
|
-
i.endsWith(e) && this._subscribersDelivered.delete(i);
|
|
178
|
-
for (const i of Array.from(this._subscribersScheduled))
|
|
179
|
-
i.endsWith(e) && this._subscribersScheduled.delete(i);
|
|
180
|
-
for (const i of Array.from(this._subscriptionsDelivered))
|
|
181
|
-
i.endsWith(e) && this._subscriptionsDelivered.delete(i);
|
|
182
|
-
for (const i of Array.from(this._subscriptionsScheduled))
|
|
183
|
-
i.endsWith(e) && this._subscriptionsScheduled.delete(i);
|
|
184
|
-
for (const i of Object.keys(this._onActionAfterSubscriptions))
|
|
185
|
-
i.endsWith(e) && delete this._onActionAfterSubscriptions[i];
|
|
186
|
-
for (const i of Object.keys(this._onActionOnErrorSubscriptions))
|
|
187
|
-
i.endsWith(e) && delete this._onActionOnErrorSubscriptions[i];
|
|
188
|
-
}
|
|
189
|
-
createOnActionAfterCallbackCollector(t, e) {
|
|
190
|
-
return (i) => {
|
|
191
|
-
this.addOnActionAfterCallback(t, e, i);
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
createOnActionOnErrorCallbackCollector(t, e) {
|
|
195
|
-
return (i) => {
|
|
196
|
-
this.addOnActionOnErrorCallback(t, e, i);
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
createRollbackSnapshot(t, e) {
|
|
200
|
-
const {
|
|
201
|
-
after: i,
|
|
202
|
-
onError: s
|
|
203
|
-
} = this._storeRollbackSnapshotsHandler.createOnActionCallbacks(
|
|
204
|
-
t,
|
|
205
|
-
e
|
|
206
|
-
) ?? {};
|
|
207
|
-
!i || !s || (this.addOnActionAfterCallback(t, e, i), this.addOnActionOnErrorCallback(t, e, s));
|
|
208
|
-
}
|
|
209
|
-
defineCurrentEnvironment() {
|
|
210
|
-
return this._options?.runtimeEnvironment ?? (typeof window > "u" ? "server" : "client");
|
|
211
|
-
}
|
|
212
|
-
definePluginExecution(t) {
|
|
213
|
-
return {
|
|
214
|
-
...w,
|
|
215
|
-
...this._options?.execution ?? {},
|
|
216
|
-
...t.execution ?? {},
|
|
217
|
-
...this._options?.subscriberExecution?.[t.name] ?? {}
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
defineHydrationScheduler(t) {
|
|
221
|
-
return t.hydrationScheduler ?? this._hydrationScheduler;
|
|
222
|
-
}
|
|
223
|
-
defineSubscriberKey(t, e) {
|
|
224
|
-
return `${t.name}-${e.$id}`;
|
|
225
|
-
}
|
|
226
|
-
defineSubscriptionKey(t, e) {
|
|
227
|
-
return `${t}-${e.$id}`;
|
|
228
|
-
}
|
|
229
|
-
executeResetStoreCallbacks(t) {
|
|
230
|
-
this._resetStoreCallback.get(t)?.forEach((e) => e(t));
|
|
231
|
-
}
|
|
232
|
-
executeStoreOnActionSubscription(t) {
|
|
233
|
-
if (this._onActionStores.has(t) || typeof t.$onAction != "function")
|
|
234
|
-
return;
|
|
235
|
-
const e = this._onActionSubscriptions.get(t), i = this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t);
|
|
236
|
-
!e?.size && !i || (this._onActionStores.add(t), t.$onAction(({ after: s, args: n, name: o, onError: a }) => {
|
|
237
|
-
const u = b(o, t);
|
|
238
|
-
this.trace("plugin:onAction", {
|
|
239
|
-
args: n,
|
|
240
|
-
name: o,
|
|
241
|
-
store: t
|
|
242
|
-
}, t.$id), this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t) && this.createRollbackSnapshot(o, t);
|
|
243
|
-
const l = this._onActionSubscriptions.get(t);
|
|
244
|
-
l?.size && l.forEach((d) => d({
|
|
245
|
-
after: this.createOnActionAfterCallbackCollector(o, t),
|
|
246
|
-
args: n,
|
|
247
|
-
name: o,
|
|
248
|
-
onError: this.createOnActionOnErrorCallbackCollector(o, t)
|
|
249
|
-
}));
|
|
250
|
-
const p = this._onActionAfterSubscriptions[u], f = this._onActionOnErrorSubscriptions[u];
|
|
251
|
-
this.clearOnActionCallbacksSubscriptions(u), p?.length && s((d) => {
|
|
252
|
-
p.forEach((S) => S(d));
|
|
253
|
-
}), f?.length && a((d) => {
|
|
254
|
-
f.forEach((S) => S(d));
|
|
255
|
-
});
|
|
256
|
-
}));
|
|
257
|
-
}
|
|
258
|
-
executeSubscriber(t, e) {
|
|
259
|
-
const i = this.defineSubscriberKey(e, t.store);
|
|
260
|
-
if (this._subscribersDelivered.has(i) || !e.invoke(t, this._options?.createTracer))
|
|
261
|
-
return;
|
|
262
|
-
this._subscribersDelivered.add(i);
|
|
263
|
-
let s;
|
|
264
|
-
try {
|
|
265
|
-
s = e.hydrate?.(t);
|
|
266
|
-
} catch (o) {
|
|
267
|
-
this.traceError("subscriber:hydrate", o, () => ({ store: t.store, options: t.options }), t.store.$id);
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
const n = () => {
|
|
271
|
-
try {
|
|
272
|
-
const o = e.afterHydration?.(t);
|
|
273
|
-
o && typeof o.then == "function" && o.catch((a) => this.traceError("subscriber:afterHydration", a, () => ({ store: t.store, options: t.options }), t.store.$id));
|
|
274
|
-
} catch (o) {
|
|
275
|
-
this.traceError("subscriber:afterHydration", o, () => ({ store: t.store, options: t.options }), t.store.$id);
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
s && typeof s.then == "function" ? s.then(() => n()).catch((o) => this.traceError("subscriber:hydrate", o, () => ({ store: t.store, options: t.options }), t.store.$id)) : n(), e.subscriptions && this.subscriptionDelivery(t, e.subscriptions), e.storeMutationSubscription && this.storeMutationSubscription(e.storeMutationSubscription), this.storeOnActionSubscription(e), e.resetStoreCallback && this.addResetStoreCallback(t.store, e.resetStoreCallback);
|
|
279
|
-
}
|
|
280
|
-
executeSubscriberSafely(t, e) {
|
|
281
|
-
try {
|
|
282
|
-
this.executeSubscriber(t, e);
|
|
283
|
-
} catch (i) {
|
|
284
|
-
this.traceError("subscriber:execute", i, () => ({ store: t.store, options: t.options }), t.store.$id);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
initializeTracer() {
|
|
288
|
-
this._options?.createTracer && (this._tracer = this._options.createTracer(g));
|
|
289
|
-
}
|
|
290
|
-
plugin({ store: t, options: e }) {
|
|
291
|
-
if (this._subscribers.length) {
|
|
292
|
-
this.trace("plugin", {
|
|
293
|
-
subscribers: this._subscribers,
|
|
294
|
-
store: t,
|
|
295
|
-
options: e
|
|
296
|
-
}, t.$id);
|
|
297
|
-
try {
|
|
298
|
-
this._subscribers.forEach(
|
|
299
|
-
(i) => {
|
|
300
|
-
const s = this.defineSubscriberKey(i, t), n = this.definePluginExecution(i);
|
|
301
|
-
if (!(this._subscribersDelivered.has(s) || this._subscribersScheduled.has(s)) && this.shouldInvokePlugin(n)) {
|
|
302
|
-
if (this.shouldScheduleAfterHydration(n)) {
|
|
303
|
-
this.queueSubscriberExecution({ store: t, options: e }, i, s);
|
|
304
|
-
return;
|
|
305
|
-
}
|
|
306
|
-
this.executeSubscriber({ store: t, options: e }, i);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
), this.rewriteResetStore({ store: t }, t.$state), this._storeRollbackSnapshotsHandler.initFromPluginContext({ store: t, options: e }), this.executeStoreOnActionSubscription(t), this.registerStoreCleanup(t);
|
|
310
|
-
} catch (i) {
|
|
311
|
-
this.traceError("plugin:invoke", i, () => ({ store: t, options: e }), t.$id);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
queueSubscriberExecution(t, e, i) {
|
|
316
|
-
const s = this.defineHydrationScheduler(e);
|
|
317
|
-
this._subscribersScheduled.add(i), s(() => {
|
|
318
|
-
this._disposedStores.has(t.store) || (this._subscribersScheduled.delete(i), this.executeSubscriberSafely(t, e));
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
registerStoreCleanup(t) {
|
|
322
|
-
if (typeof t.$dispose != "function")
|
|
323
|
-
return;
|
|
324
|
-
const e = t.$dispose.bind(t), i = t;
|
|
325
|
-
i.__piniaPluginSubscriptionDisposed || (i.__piniaPluginSubscriptionDisposed = !0, t.$dispose = () => (this._disposedStores.add(t), this.clearStoreTracking(t), this._storeRollbackSnapshotsHandler.clearStoreTracking(t), e()));
|
|
326
|
-
}
|
|
327
|
-
rewriteResetStore({ store: t }, e) {
|
|
328
|
-
const i = h(e);
|
|
329
|
-
t.$reset = () => {
|
|
330
|
-
this.trace("store:reset", {
|
|
331
|
-
initState: i,
|
|
332
|
-
store: t
|
|
333
|
-
}, t.$id), this.executeResetStoreCallbacks(t), t.$patch(h(i));
|
|
334
|
-
};
|
|
335
|
-
}
|
|
336
|
-
shouldInvokePlugin(t) {
|
|
337
|
-
const e = this.defineCurrentEnvironment();
|
|
338
|
-
return t.environment === "both" || t.environment === e;
|
|
339
|
-
}
|
|
340
|
-
shouldScheduleAfterHydration(t) {
|
|
341
|
-
return this.defineCurrentEnvironment() === "client" && t.hydration === "defer";
|
|
342
|
-
}
|
|
343
|
-
storeOnActionSubscription(t) {
|
|
344
|
-
const { store: e, callback: i } = t?.storeOnActionSubscription?.() ?? {};
|
|
345
|
-
!e || !i || (this._onActionSubscriptions.has(e) || this._onActionSubscriptions.set(e, /* @__PURE__ */ new Set()), this._onActionSubscriptions.get(e).add(i), this.executeStoreOnActionSubscription(e));
|
|
346
|
-
}
|
|
347
|
-
storeMutationSubscription(t) {
|
|
348
|
-
const { store: e, callback: i } = t();
|
|
349
|
-
e.$subscribe((s) => {
|
|
350
|
-
this.trace("store:mutation", { mutation: s, store: e }, e.$id), i(s);
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
subscriptionDelivery(t, e) {
|
|
354
|
-
Object.entries(e).forEach(([i, s]) => {
|
|
355
|
-
const n = this.defineSubscriptionKey(i, t.store), o = this.definePluginExecution(s.subscription);
|
|
356
|
-
if (!(this._subscriptionsDelivered.has(n) || this._subscriptionsScheduled.has(n)) && this.shouldInvokePlugin(o)) {
|
|
357
|
-
if (this.shouldScheduleAfterHydration(o)) {
|
|
358
|
-
const a = this.defineHydrationScheduler(s.subscription);
|
|
359
|
-
this._subscriptionsScheduled.add(n), a(() => {
|
|
360
|
-
this._disposedStores.has(t.store) || (this._subscriptionsScheduled.delete(n), this.subscriptionDeliverySafely(t, i, s));
|
|
361
|
-
});
|
|
362
|
-
return;
|
|
363
|
-
}
|
|
364
|
-
this.subscriptionDeliverySafely(t, i, s);
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
subscriptionDeliverySafely({ store: t, options: e }, i, s) {
|
|
369
|
-
try {
|
|
370
|
-
if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(i, t)))
|
|
371
|
-
return;
|
|
372
|
-
const { subscription: n, subscriptionOptions: o, stores: a } = s;
|
|
373
|
-
this.trace("subscription:delivery", {
|
|
374
|
-
pluginName: i,
|
|
375
|
-
subscription: n,
|
|
376
|
-
options: o,
|
|
377
|
-
stores: a
|
|
378
|
-
}, t.$id);
|
|
379
|
-
const u = {
|
|
380
|
-
storeOptions: { ...e?.storeOptions ?? {}, ...o }
|
|
381
|
-
};
|
|
382
|
-
n.invoke(
|
|
383
|
-
{ store: t, options: { ...e, ...u } }
|
|
384
|
-
), this._subscriptionsDelivered.add(this.defineSubscriptionKey(i, t)), _(a) || a?.forEach(
|
|
385
|
-
(l) => n.invoke({
|
|
386
|
-
store: l,
|
|
387
|
-
options: {
|
|
388
|
-
...e,
|
|
389
|
-
...u,
|
|
390
|
-
...l?.storeOptions ?? {}
|
|
391
|
-
}
|
|
392
|
-
})
|
|
393
|
-
);
|
|
394
|
-
} catch (n) {
|
|
395
|
-
this.traceError("subscription:delivery", n, () => ({ store: t, options: e }), t.$id);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
trace(t, e, i) {
|
|
399
|
-
this._tracer && (t = this._className + ":" + t, this._tracer.debug(t, t.toLowerCase(), e, i));
|
|
400
|
-
}
|
|
401
|
-
traceError(t, e, i, s) {
|
|
402
|
-
this._tracer ? (t = this._className + ":" + t, this._tracer.error(t, t.toLowerCase(), e, i, s)) : console.error(e);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
function D(r, t) {
|
|
406
|
-
const e = new A(r, t);
|
|
407
|
-
return e.plugin.bind(e);
|
|
408
|
-
}
|
|
409
|
-
function j(r, t) {
|
|
410
|
-
const e = new A(r, {
|
|
411
|
-
execution: t?.execution,
|
|
412
|
-
hydrationScheduler: t?.hydrationScheduler,
|
|
413
|
-
runtimeEnvironment: t?.runtimeEnvironment,
|
|
414
|
-
subscriberExecution: t?.subscriberExecution
|
|
415
|
-
});
|
|
416
|
-
return e.plugin.bind(e);
|
|
28
|
+
function p(r, e, t) {
|
|
29
|
+
return t && (e = { ...e, ...t ?? {} }), c(r, e);
|
|
417
30
|
}
|
|
418
31
|
export {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
D as createPlugin,
|
|
425
|
-
F as defineAStore,
|
|
426
|
-
W as defineAStoreCtx,
|
|
427
|
-
I as getDefineAStoreSetupContext,
|
|
428
|
-
q as getEnhancedStore,
|
|
429
|
-
z as getExtendingStore,
|
|
430
|
-
L as setEnhancedStore
|
|
32
|
+
A as PLUGIN_NAME,
|
|
33
|
+
i as createPlugin,
|
|
34
|
+
S as defineAStore,
|
|
35
|
+
a as defineAStoreCtx,
|
|
36
|
+
g as getEnhancedStore
|
|
431
37
|
};
|
|
432
|
-
//# sourceMappingURL=pinia-plugin-subscription.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { PiniaPlugin } from "pinia";
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { StoreSubscriberInterface, PluginOrchestratorOptions } from "../types/plugin";
|
|
3
|
+
import type { StoreOptions } from "../types/store";
|
|
4
4
|
/**
|
|
5
5
|
* Creates a Pinia plugin that manages the specified plugin subscribers.
|
|
6
6
|
*
|
|
@@ -8,18 +8,8 @@ import type { RollbackActionParams, StoreOptions } from "../types/store";
|
|
|
8
8
|
* @param options The options for configuring the plugin subscription.
|
|
9
9
|
* @returns A Pinia plugin that handles the provided subscribers and options.
|
|
10
10
|
*/
|
|
11
|
-
export declare function createPlugin(subscribers:
|
|
11
|
+
export declare function createPlugin(subscribers: StoreSubscriberInterface[], options?: PluginOrchestratorOptions): PiniaPlugin;
|
|
12
12
|
declare module 'pinia' {
|
|
13
|
-
interface PiniaCustomProperties {
|
|
14
|
-
/**
|
|
15
|
-
* Executes an action and restores the previous state snapshot if it fails.
|
|
16
|
-
* Added to every store when the plugin is active.
|
|
17
|
-
*
|
|
18
|
-
* @param params The action name and the state keys to snapshot.
|
|
19
|
-
* @param args The action arguments, passed as a single array (forwarded via `apply`).
|
|
20
|
-
*/
|
|
21
|
-
$rollbackAfterFailure: (params: RollbackActionParams, args?: any[]) => Promise<void>;
|
|
22
|
-
}
|
|
23
13
|
interface DefineStoreOptionsBase<S, Store> {
|
|
24
14
|
storeOptions?: StoreOptions;
|
|
25
15
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { MessengerSubscriber } from '../types/messenger';
|
|
2
|
+
export default class Messenger {
|
|
3
|
+
/**
|
|
4
|
+
* List of subscribers.
|
|
5
|
+
*/
|
|
6
|
+
private _subscribers;
|
|
7
|
+
get subscribers(): Map<string, MessengerSubscriber[]>;
|
|
8
|
+
/**
|
|
9
|
+
* Adds a subscriber.
|
|
10
|
+
* @param subscriber The subscriber to add.
|
|
11
|
+
*/
|
|
12
|
+
addSubscriber(chanel: string, subscriber: MessengerSubscriber): void;
|
|
13
|
+
/**
|
|
14
|
+
* Clears all subscribers.
|
|
15
|
+
*/
|
|
16
|
+
clearSubscribers(): void;
|
|
17
|
+
/**
|
|
18
|
+
* Sends a payload to all subscribers.
|
|
19
|
+
* @param payload The payload to send.
|
|
20
|
+
*/
|
|
21
|
+
send(chanel: string, payload: unknown): void;
|
|
22
|
+
/**
|
|
23
|
+
* Removes a subscriber.
|
|
24
|
+
* @param subscriber The subscriber to remove.
|
|
25
|
+
*/
|
|
26
|
+
removeSubscriber(chanel: string, subscriber: MessengerSubscriber): void;
|
|
27
|
+
}
|
|
28
|
+
export declare const PluginMessenger: Messenger;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import Messenger from './messenger';
|
|
2
|
+
import type { MessengerEmitter as MessengerEmitterInterface, MessengerProvider as MessengerProviderInterface, MessengerSubscriber as MessengerSubscriberInterface } from '../types/messenger';
|
|
3
|
+
/**
|
|
4
|
+
* Abstract class providing access to the events messenger.
|
|
5
|
+
* Manages the messenger instance and provides a getter for it.
|
|
6
|
+
*/
|
|
7
|
+
export default abstract class MessengerProvider implements MessengerProviderInterface {
|
|
8
|
+
private readonly _messenger;
|
|
9
|
+
protected _messengerChannel: string;
|
|
10
|
+
get messenger(): Messenger;
|
|
11
|
+
get messengerChannel(): string;
|
|
12
|
+
constructor(_messenger: Messenger, _messengerChannel: string);
|
|
13
|
+
}
|
|
14
|
+
export declare class MessengerEmitter extends MessengerProvider implements MessengerEmitterInterface {
|
|
15
|
+
/**
|
|
16
|
+
* Emits an event to all subscribers.
|
|
17
|
+
* @param payload The payload to emit.
|
|
18
|
+
*/
|
|
19
|
+
emit(payload: unknown): void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Abstract class for subscribing to messenger events.
|
|
23
|
+
* Manages subscriptions and provides a method to receive payloads.
|
|
24
|
+
*/
|
|
25
|
+
export declare abstract class MessengerSubscriber extends MessengerProvider implements MessengerSubscriberInterface {
|
|
26
|
+
private readonly _id;
|
|
27
|
+
get id(): string;
|
|
28
|
+
constructor(messenger: Messenger, messengerChannel: string, _id: string);
|
|
29
|
+
/**
|
|
30
|
+
* overide this method to clear subscriptions
|
|
31
|
+
*/
|
|
32
|
+
clear(): void;
|
|
33
|
+
protected abstract consume(payload: unknown): void;
|
|
34
|
+
protected abstract isCustomer(payload: unknown): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Receives a payload and processes it if there are matching subscriptions.
|
|
37
|
+
* @param payload The payload to receive.
|
|
38
|
+
*/
|
|
39
|
+
receive(payload: unknown): void;
|
|
40
|
+
private subscribe;
|
|
41
|
+
/**
|
|
42
|
+
* Unsubscribes from the messenger channel.
|
|
43
|
+
*/
|
|
44
|
+
unsubscribe(): void;
|
|
45
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Store } from "pinia";
|
|
2
|
+
import type { StoreEventsGateway, TraceReporter } from "../types/core";
|
|
3
|
+
import type StoreHooksRegistry from "./StoreHooksRegistry";
|
|
4
|
+
import type TraceGroupHelper from "../tracing/TraceGroupHelper";
|
|
5
|
+
/** Hooks the stores `$onAction` / `$subscribe` and runs the registered callbacks inside trace groups. */
|
|
6
|
+
export default class StoreHooksBinder {
|
|
7
|
+
private _events;
|
|
8
|
+
private _groups;
|
|
9
|
+
private _registry;
|
|
10
|
+
private _reporter;
|
|
11
|
+
constructor(registry: StoreHooksRegistry, groups: TraceGroupHelper, events: StoreEventsGateway, reporter: TraceReporter);
|
|
12
|
+
/** Subscribes once to the store `$subscribe` hook and dispatches mutations to the registered callbacks. */
|
|
13
|
+
hookStoreMutations(store: Store): void;
|
|
14
|
+
/**
|
|
15
|
+
* Hooks the store `$onAction` once, when a callback, an event subscription or a deferred subscriber needs it.
|
|
16
|
+
*/
|
|
17
|
+
executeStoreOnActionSubscription(store: Store, hasDeferredSubscribers?: boolean): void;
|
|
18
|
+
private executeStoreOnActionAfterCallbacks;
|
|
19
|
+
private executeStoreOnActionOnErrorCallbacks;
|
|
20
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Store } from "pinia";
|
|
2
|
+
import type { StoreMutationSubscriptionCallback, StoreOnActionAfterCallbackParameter, StoreOnActionSubscriptionCallback } from "../types/plugin";
|
|
3
|
+
export interface StoreCleanupHandlers {
|
|
4
|
+
onDispose: (store: Store) => void;
|
|
5
|
+
onError: (error: unknown, store: Store) => void;
|
|
6
|
+
}
|
|
7
|
+
/** Holds the per-store onAction/onMutation subscriptions and the per-action after/onError callbacks. */
|
|
8
|
+
export default class StoreHooksRegistry {
|
|
9
|
+
private _disposedStores;
|
|
10
|
+
private _storesRegistry;
|
|
11
|
+
addOnActionAfterCallback(actionName: string, store: Store, callback: StoreOnActionAfterCallbackParameter): void;
|
|
12
|
+
addOnActionOnErrorCallback(actionName: string, store: Store, callback: (error: unknown) => void): void;
|
|
13
|
+
addOnActionSubscription(store: Store, callback: StoreOnActionSubscriptionCallback, subscriberName: string): void;
|
|
14
|
+
addMutationSubscription(store: Store, callback: StoreMutationSubscriptionCallback, subscriberName: string): void;
|
|
15
|
+
clearOnActionCallbacks(storageKey: string, store: Store): void;
|
|
16
|
+
clearStore(store: Store): void;
|
|
17
|
+
getMutationSubscriptions(store: Store): Set<StoreMutationSubscriptionCallback> | undefined;
|
|
18
|
+
getOnActionAfterCallbacks(storageKey: string, store: Store): StoreOnActionAfterCallbackParameter[] | undefined;
|
|
19
|
+
getOnActionOnErrorCallbacks(storageKey: string, store: Store): Array<(error: unknown) => void> | undefined;
|
|
20
|
+
getOnActionSubscriptions(store: Store): Set<StoreOnActionSubscriptionCallback> | undefined;
|
|
21
|
+
hasMutationUnsubscribe(store: Store): boolean;
|
|
22
|
+
initStoreRegistry(store: Store): void;
|
|
23
|
+
isDisposed(store: Store): boolean;
|
|
24
|
+
isOnActionHooked(store: Store): boolean;
|
|
25
|
+
markOnActionHooked(store: Store): void;
|
|
26
|
+
/** Registers the names of the subscribers that subscribed to the store hooks, for tracing. */
|
|
27
|
+
setTraces(store: Store, traces: {
|
|
28
|
+
onAction: string[];
|
|
29
|
+
onMutation: string[];
|
|
30
|
+
}): void;
|
|
31
|
+
setMutationUnsubscribe(store: Store, unsubscribe: () => void): void;
|
|
32
|
+
/** Wraps `store.$dispose` (once per store) to release the registry entries of the store. */
|
|
33
|
+
registerStoreCleanup(store: Store, { onDispose, onError }: StoreCleanupHandlers): void;
|
|
34
|
+
}
|