pinia-plugin-subscription 0.1.8 → 0.2.0
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 +30 -0
- package/README.md +47 -289
- package/dist/chunks/{index-B-zO0yrr.js → index-VL_PgzVK.js} +53 -54
- package/dist/chunks/index-VL_PgzVK.js.map +1 -0
- package/dist/core/PluginSubscriber.d.ts +1 -1
- package/dist/core/PluginSubscription.d.ts +39 -4
- package/dist/core/RollbackStateSnapshotsHandler.d.ts +65 -0
- package/dist/core/Store.d.ts +4 -4
- package/dist/factories/state-snapshot.d.ts +3 -0
- package/dist/helpers.js +2 -1
- package/dist/helpers.js.map +1 -0
- package/dist/lib/main.d.ts +2 -1
- package/dist/pinia-plugin-subscription.js +344 -199
- package/dist/pinia-plugin-subscription.js.map +1 -0
- package/dist/plugins/createPlugin.d.ts +9 -1
- package/dist/tests/defineAStore.test-d.d.ts +1 -0
- package/dist/tests/storeOptionsExtensions.test-d.d.ts +12 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/plugin.d.ts +5 -2
- package/dist/types/store.d.ts +43 -2
- package/dist/utils/deep-clone.d.ts +7 -0
- package/dist/utils/store.d.ts +84 -8
- package/docs/authoring-plugins.md +408 -0
- package/docs/using-plugins.md +374 -0
- package/package.json +2 -1
|
@@ -1,288 +1,433 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import {
|
|
5
|
-
import { C as
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
hydration: "immediate"
|
|
9
|
-
};
|
|
10
|
-
function E(r) {
|
|
11
|
-
if (typeof window < "u" && typeof window.requestAnimationFrame == "function") {
|
|
12
|
-
window.requestAnimationFrame(() => globalThis.setTimeout(r, 0));
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
globalThis.setTimeout(r, 0);
|
|
16
|
-
}
|
|
17
|
-
function h(r) {
|
|
1
|
+
var g = Object.defineProperty;
|
|
2
|
+
var E = (n, s, t) => s in n ? g(n, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[s] = t;
|
|
3
|
+
var a = (n, s, t) => E(n, typeof s != "symbol" ? s + "" : s, t);
|
|
4
|
+
import { h as O, g as f, D as m, i as y, P as C } from "./chunks/index-VL_PgzVK.js";
|
|
5
|
+
import { C as T, p as W, a as L, S as I, d as q, b as G, c as U, e as z, f as B, s as J } from "./chunks/index-VL_PgzVK.js";
|
|
6
|
+
import { toRaw as _ } from "vue";
|
|
7
|
+
function u(n) {
|
|
18
8
|
if (typeof structuredClone == "function")
|
|
19
9
|
try {
|
|
20
|
-
return structuredClone(
|
|
10
|
+
return structuredClone(n);
|
|
21
11
|
} catch {
|
|
22
12
|
}
|
|
23
|
-
if (
|
|
24
|
-
return
|
|
25
|
-
if (
|
|
13
|
+
if (n === null || typeof n != "object")
|
|
14
|
+
return n;
|
|
15
|
+
if (n instanceof Map)
|
|
26
16
|
return new Map(
|
|
27
|
-
Array.from(
|
|
17
|
+
Array.from(n.entries(), ([t, e]) => [u(t), u(e)])
|
|
28
18
|
);
|
|
29
|
-
if (
|
|
30
|
-
return new Set(Array.from(
|
|
31
|
-
if (Array.isArray(
|
|
32
|
-
return
|
|
33
|
-
if (Object.getPrototypeOf(
|
|
19
|
+
if (n instanceof Set)
|
|
20
|
+
return new Set(Array.from(n.values(), (t) => u(t)));
|
|
21
|
+
if (Array.isArray(n))
|
|
22
|
+
return n.map((t) => u(t));
|
|
23
|
+
if (Object.getPrototypeOf(n) === Object.prototype || Object.getPrototypeOf(n) === null)
|
|
34
24
|
return Object.fromEntries(
|
|
35
|
-
Object.entries(
|
|
25
|
+
Object.entries(n).map(([t, e]) => [t, u(e)])
|
|
26
|
+
);
|
|
27
|
+
const s = Object.create(Object.getPrototypeOf(n));
|
|
28
|
+
for (const [t, e] of Object.entries(n))
|
|
29
|
+
s[t] = u(e);
|
|
30
|
+
return s;
|
|
31
|
+
}
|
|
32
|
+
function $(n, s = "all") {
|
|
33
|
+
if (Array.isArray(s)) {
|
|
34
|
+
const t = {};
|
|
35
|
+
for (const e of s)
|
|
36
|
+
e in n && (t[e] = u(_(n[e])));
|
|
37
|
+
return t;
|
|
38
|
+
}
|
|
39
|
+
if (s !== "all")
|
|
40
|
+
throw new Error(`Invalid keys parameter: ${s}`);
|
|
41
|
+
return u(_(n));
|
|
42
|
+
}
|
|
43
|
+
class v {
|
|
44
|
+
constructor() {
|
|
45
|
+
a(this, "_stateSnapshots", /* @__PURE__ */ new Map());
|
|
46
|
+
a(this, "_storeHasRollbackSnapshots", /* @__PURE__ */ new Set());
|
|
47
|
+
a(this, "_storeRollbackSnapshotsParams", /* @__PURE__ */ new Map());
|
|
48
|
+
}
|
|
49
|
+
addRollbackAction(s) {
|
|
50
|
+
typeof s.$rollbackAfterFailure != "function" && (s.$rollbackAfterFailure = async ({ action: t, stateKeys: e }, i = []) => {
|
|
51
|
+
const r = s[t];
|
|
52
|
+
if (typeof r != "function" || O(t))
|
|
53
|
+
return;
|
|
54
|
+
const o = !e || e.length === 0 ? "all" : e, h = this.saveStateSnapshot(s, t, o);
|
|
55
|
+
try {
|
|
56
|
+
await r.apply(s, i);
|
|
57
|
+
} catch (c) {
|
|
58
|
+
throw this.patchStore(h, s), c;
|
|
59
|
+
} finally {
|
|
60
|
+
this.removeStateSnapshot(h);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
createOnActionCallbacks(s, t) {
|
|
65
|
+
const e = this._storeRollbackSnapshotsParams.get(
|
|
66
|
+
f(s, t)
|
|
36
67
|
);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
68
|
+
if (!e || e.length === 0)
|
|
69
|
+
return;
|
|
70
|
+
const i = this.saveStateSnapshot(t, s, e);
|
|
71
|
+
return {
|
|
72
|
+
after: () => {
|
|
73
|
+
this.removeStateSnapshot(i);
|
|
74
|
+
},
|
|
75
|
+
onError: () => {
|
|
76
|
+
this.patchStore(i, t), this.removeStateSnapshot(i);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
initFromPluginContext({ options: s, store: t }) {
|
|
81
|
+
const e = s?.storeOptions?.rollbackAfterFailure;
|
|
82
|
+
e && !this._storeHasRollbackSnapshots.has(t.$id) && (Object.keys(e).forEach((i) => {
|
|
83
|
+
this._storeRollbackSnapshotsParams.set(
|
|
84
|
+
f(i, t),
|
|
85
|
+
e[i]
|
|
86
|
+
);
|
|
87
|
+
}), this._storeHasRollbackSnapshots.add(t.$id)), this.addRollbackAction(t);
|
|
88
|
+
}
|
|
89
|
+
clearStoreTracking(s) {
|
|
90
|
+
const t = `-${s.$id}`;
|
|
91
|
+
for (const e of Array.from(this._storeRollbackSnapshotsParams.keys()))
|
|
92
|
+
e.endsWith(t) && this._storeRollbackSnapshotsParams.delete(e);
|
|
93
|
+
this._storeHasRollbackSnapshots.delete(s.$id);
|
|
94
|
+
for (const e of Array.from(this._stateSnapshots.keys()))
|
|
95
|
+
e.description?.startsWith(`${s.$id}:`) && this._stateSnapshots.delete(e);
|
|
96
|
+
}
|
|
97
|
+
patchStore(s, t) {
|
|
98
|
+
const e = this._stateSnapshots.get(s);
|
|
99
|
+
if (e)
|
|
100
|
+
try {
|
|
101
|
+
t.$patch(e);
|
|
102
|
+
} catch (i) {
|
|
103
|
+
console.error(`Failed to patch store with snapshot ${String(s)}`, i);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
removeStateSnapshot(s) {
|
|
107
|
+
s && this._stateSnapshots.delete(s);
|
|
108
|
+
}
|
|
109
|
+
saveStateSnapshot(s, t, e) {
|
|
110
|
+
const i = $(
|
|
111
|
+
s.$state,
|
|
112
|
+
e
|
|
113
|
+
), r = /* @__PURE__ */ Symbol(`${s.$id}:${t}`);
|
|
114
|
+
return this._stateSnapshots.set(r, i), r;
|
|
115
|
+
}
|
|
116
|
+
storeHasRollbackSnapshots(s) {
|
|
117
|
+
return this._storeHasRollbackSnapshots.has(s.$id);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const A = "PluginSubscription", P = {
|
|
121
|
+
environment: "both",
|
|
122
|
+
hydration: "immediate"
|
|
123
|
+
};
|
|
124
|
+
function R(n) {
|
|
125
|
+
if (typeof window < "u" && typeof window.requestAnimationFrame == "function") {
|
|
126
|
+
window.requestAnimationFrame(() => globalThis.setTimeout(n, 0));
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
globalThis.setTimeout(n, 0);
|
|
41
130
|
}
|
|
42
|
-
function
|
|
43
|
-
return
|
|
131
|
+
function D(n) {
|
|
132
|
+
return n !== void 0 && !Array.isArray(n);
|
|
44
133
|
}
|
|
45
|
-
class
|
|
46
|
-
constructor(e, i
|
|
47
|
-
const
|
|
48
|
-
super(Array.isArray(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
this
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
this
|
|
134
|
+
class k extends m {
|
|
135
|
+
constructor(t, e, i) {
|
|
136
|
+
const r = Array.isArray(e) ? e : void 0, o = D(e) ? e : i;
|
|
137
|
+
super(Array.isArray(r) && !!r?.includes(A), C);
|
|
138
|
+
a(this, "_className", A);
|
|
139
|
+
a(this, "_hydrationScheduler");
|
|
140
|
+
a(this, "_onActionSubscriptions", []);
|
|
141
|
+
a(this, "_onActionAfterSubscriptions", {});
|
|
142
|
+
a(this, "_onActionOnErrorSubscriptions", {});
|
|
143
|
+
a(this, "_options");
|
|
144
|
+
a(this, "_pluginDebug");
|
|
145
|
+
a(this, "_resetStoreCallback", []);
|
|
146
|
+
a(this, "_storeRollbackSnapshotsHandler", new v());
|
|
147
|
+
a(this, "_subscribers", []);
|
|
148
|
+
a(this, "_subscribersDelivered", /* @__PURE__ */ new Set());
|
|
149
|
+
a(this, "_subscribersScheduled", /* @__PURE__ */ new Set());
|
|
150
|
+
a(this, "_subscriptions", []);
|
|
151
|
+
a(this, "_subscriptionsDelivered", /* @__PURE__ */ new Set());
|
|
152
|
+
a(this, "_subscriptionsScheduled", /* @__PURE__ */ new Set());
|
|
153
|
+
this._hydrationScheduler = o?.hydrationScheduler ?? R, this._options = o, this._pluginDebug = Array.isArray(r) ? r : void 0, this._subscribers = t;
|
|
154
|
+
}
|
|
155
|
+
set subscribers(t) {
|
|
156
|
+
this._subscribers = t;
|
|
64
157
|
}
|
|
65
158
|
get subscriptions() {
|
|
66
|
-
if (!
|
|
159
|
+
if (!y(this._subscriptions))
|
|
67
160
|
return this._subscriptions;
|
|
68
161
|
}
|
|
69
|
-
|
|
70
|
-
|
|
162
|
+
addOnActionAfterCallback(t, e, i) {
|
|
163
|
+
const r = f(t, e);
|
|
164
|
+
this._onActionAfterSubscriptions[r] || (this._onActionAfterSubscriptions[r] = []), this._onActionAfterSubscriptions[r]?.push(i);
|
|
165
|
+
}
|
|
166
|
+
addOnActionOnErrorCallback(t, e, i) {
|
|
167
|
+
const r = f(t, e);
|
|
168
|
+
this._onActionOnErrorSubscriptions[r] || (this._onActionOnErrorSubscriptions[r] = []), this._onActionOnErrorSubscriptions[r]?.push(i);
|
|
169
|
+
}
|
|
170
|
+
addResetStoreCallback(t) {
|
|
171
|
+
this._resetStoreCallback.push(t);
|
|
172
|
+
}
|
|
173
|
+
clearOnActionCallbacksSubscriptions(t) {
|
|
174
|
+
delete this._onActionAfterSubscriptions[t], delete this._onActionOnErrorSubscriptions[t];
|
|
175
|
+
}
|
|
176
|
+
clearStoreTracking(t) {
|
|
177
|
+
const e = `-${t.$id}`;
|
|
178
|
+
for (const i of Array.from(this._subscribersDelivered))
|
|
179
|
+
i.endsWith(e) && this._subscribersDelivered.delete(i);
|
|
180
|
+
for (const i of Array.from(this._subscribersScheduled))
|
|
181
|
+
i.endsWith(e) && this._subscribersScheduled.delete(i);
|
|
182
|
+
for (const i of Array.from(this._subscriptionsDelivered))
|
|
183
|
+
i.endsWith(e) && this._subscriptionsDelivered.delete(i);
|
|
184
|
+
for (const i of Array.from(this._subscriptionsScheduled))
|
|
185
|
+
i.endsWith(e) && this._subscriptionsScheduled.delete(i);
|
|
186
|
+
for (const i of Object.keys(this._onActionAfterSubscriptions))
|
|
187
|
+
i.endsWith(e) && delete this._onActionAfterSubscriptions[i];
|
|
188
|
+
for (const i of Object.keys(this._onActionOnErrorSubscriptions))
|
|
189
|
+
i.endsWith(e) && delete this._onActionOnErrorSubscriptions[i];
|
|
190
|
+
}
|
|
191
|
+
createOnActionAfterCallbackCollector(t, e) {
|
|
192
|
+
return (i) => {
|
|
193
|
+
this.addOnActionAfterCallback(t, e, i);
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
createOnActionOnErrorCallbackCollector(t, e) {
|
|
197
|
+
return (i) => {
|
|
198
|
+
this.addOnActionOnErrorCallback(t, e, i);
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
createRollbackSnapshot(t, e) {
|
|
202
|
+
const {
|
|
203
|
+
after: i,
|
|
204
|
+
onError: r
|
|
205
|
+
} = this._storeRollbackSnapshotsHandler.createOnActionCallbacks(
|
|
206
|
+
t,
|
|
207
|
+
e
|
|
208
|
+
) ?? {};
|
|
209
|
+
!i || !r || (this.addOnActionAfterCallback(t, e, i), this.addOnActionOnErrorCallback(t, e, r));
|
|
71
210
|
}
|
|
72
211
|
defineCurrentEnvironment() {
|
|
73
212
|
return this._options?.runtimeEnvironment ?? (typeof window > "u" ? "server" : "client");
|
|
74
213
|
}
|
|
75
|
-
definePluginDebug(
|
|
76
|
-
return !!this._pluginDebug?.includes(
|
|
214
|
+
definePluginDebug(t) {
|
|
215
|
+
return !!this._pluginDebug?.includes(t.name);
|
|
77
216
|
}
|
|
78
|
-
definePluginExecution(
|
|
217
|
+
definePluginExecution(t) {
|
|
79
218
|
return {
|
|
80
|
-
...
|
|
219
|
+
...P,
|
|
81
220
|
...this._options?.execution ?? {},
|
|
82
|
-
...
|
|
83
|
-
...this._options?.subscriberExecution?.[
|
|
221
|
+
...t.execution ?? {},
|
|
222
|
+
...this._options?.subscriberExecution?.[t.name] ?? {}
|
|
84
223
|
};
|
|
85
224
|
}
|
|
86
|
-
defineHydrationScheduler(
|
|
87
|
-
return
|
|
225
|
+
defineHydrationScheduler(t) {
|
|
226
|
+
return t.hydrationScheduler ?? this._hydrationScheduler;
|
|
88
227
|
}
|
|
89
|
-
defineSubscriberKey(
|
|
90
|
-
return `${
|
|
228
|
+
defineSubscriberKey(t, e) {
|
|
229
|
+
return `${t.name}-${e.$id}`;
|
|
91
230
|
}
|
|
92
|
-
defineSubscriptionKey(
|
|
93
|
-
return `${
|
|
231
|
+
defineSubscriptionKey(t, e) {
|
|
232
|
+
return `${t}-${e.$id}`;
|
|
94
233
|
}
|
|
95
|
-
executeResetStoreCallbacks(
|
|
96
|
-
this._resetStoreCallback.forEach((
|
|
234
|
+
executeResetStoreCallbacks(t) {
|
|
235
|
+
this._resetStoreCallback.forEach((e) => e(t));
|
|
97
236
|
}
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
237
|
+
executeStoreOnActionSubscription(t) {
|
|
238
|
+
const e = this._onActionSubscriptions, i = this._storeRollbackSnapshotsHandler.storeHasRollbackSnapshots(t);
|
|
239
|
+
!e?.length && !i || t.$onAction(({ after: r, args: o, name: h, onError: c }) => {
|
|
240
|
+
const l = f(h, t);
|
|
241
|
+
this.debugLog(`storeOnActionSubscription ${l}`, { args: o, name: h, store: t }), i && this.createRollbackSnapshot(h, t), e?.length && e.forEach((b) => b({
|
|
242
|
+
after: this.createOnActionAfterCallbackCollector(h, t),
|
|
243
|
+
args: o,
|
|
244
|
+
name: h,
|
|
245
|
+
onError: this.createOnActionOnErrorCallbackCollector(h, t)
|
|
246
|
+
}));
|
|
247
|
+
const d = this._onActionAfterSubscriptions[l], S = this._onActionOnErrorSubscriptions[l];
|
|
248
|
+
this.clearOnActionCallbacksSubscriptions(l), d?.length && r((b) => {
|
|
249
|
+
d.forEach((p) => p(b));
|
|
250
|
+
}), S?.length && c((b) => {
|
|
251
|
+
S.forEach((p) => p(b));
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
executeSubscriber(t, e) {
|
|
256
|
+
const i = this.defineSubscriberKey(e, t.store), r = this.definePluginDebug(e);
|
|
257
|
+
if (this._subscribersDelivered.has(i) || !e.invoke(
|
|
258
|
+
t,
|
|
259
|
+
r
|
|
103
260
|
))
|
|
104
261
|
return;
|
|
105
|
-
this._subscribersDelivered.add(
|
|
106
|
-
let
|
|
262
|
+
this._subscribersDelivered.add(i);
|
|
263
|
+
let o;
|
|
107
264
|
try {
|
|
108
|
-
|
|
109
|
-
} catch (
|
|
110
|
-
this.logError(
|
|
265
|
+
o = e.hydrate?.(t, r);
|
|
266
|
+
} catch (c) {
|
|
267
|
+
this.logError(c, t.store, t.options);
|
|
111
268
|
return;
|
|
112
269
|
}
|
|
113
|
-
const
|
|
270
|
+
const h = () => {
|
|
114
271
|
try {
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
} catch (
|
|
118
|
-
this.logError(
|
|
272
|
+
const c = e.afterHydration?.(t, r);
|
|
273
|
+
c && typeof c.then == "function" && c.catch((l) => this.logError(l, t.store, t.options));
|
|
274
|
+
} catch (c) {
|
|
275
|
+
this.logError(c, t.store, t.options);
|
|
119
276
|
}
|
|
120
277
|
};
|
|
121
|
-
|
|
278
|
+
o && typeof o.then == "function" ? o.then(() => h()).catch((c) => this.logError(c, t.store, t.options)) : h(), e.subscriptions && this.subscriptionDelivery(t, e.subscriptions), e.storeMutationSubscription && this.storeMutationSubscription(e.storeMutationSubscription), this.storeOnActionSubscription(e), e.resetStoreCallback && this.addResetStoreCallback(e.resetStoreCallback);
|
|
122
279
|
}
|
|
123
|
-
executeSubscriberSafely(
|
|
280
|
+
executeSubscriberSafely(t, e) {
|
|
124
281
|
try {
|
|
125
|
-
this.executeSubscriber(
|
|
126
|
-
} catch (
|
|
127
|
-
this.logError(
|
|
282
|
+
this.executeSubscriber(t, e);
|
|
283
|
+
} catch (i) {
|
|
284
|
+
this.logError(i, t.store, t.options);
|
|
128
285
|
}
|
|
129
286
|
}
|
|
130
|
-
|
|
131
|
-
const s = this.defineHydrationScheduler(i);
|
|
132
|
-
this._subscribersScheduled.add(t), s(() => {
|
|
133
|
-
this._subscribersScheduled.delete(t), this.executeSubscriberSafely(e, i);
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
shouldInvokePlugin(e) {
|
|
137
|
-
const i = this.defineCurrentEnvironment();
|
|
138
|
-
return e.environment === "both" || e.environment === i;
|
|
139
|
-
}
|
|
140
|
-
shouldScheduleAfterHydration(e) {
|
|
141
|
-
return this.defineCurrentEnvironment() === "client" && e.hydration === "defer";
|
|
142
|
-
}
|
|
143
|
-
plugin({ store: e, options: i }) {
|
|
287
|
+
plugin({ store: t, options: e }) {
|
|
144
288
|
if (this._subscribers.length) {
|
|
145
|
-
this.debugLog(`plugin() - ${
|
|
289
|
+
this.debugLog(`plugin() - ${t.$id}`, [
|
|
146
290
|
"subscriber:",
|
|
147
291
|
this._subscribers,
|
|
148
292
|
"store:",
|
|
149
|
-
|
|
293
|
+
t,
|
|
150
294
|
"options:",
|
|
151
|
-
|
|
295
|
+
e
|
|
152
296
|
]);
|
|
153
297
|
try {
|
|
154
298
|
this._subscribers.forEach(
|
|
155
|
-
(
|
|
156
|
-
const
|
|
157
|
-
if (!(this._subscribersDelivered.has(
|
|
158
|
-
if (this.shouldScheduleAfterHydration(
|
|
159
|
-
this.queueSubscriberExecution({ store:
|
|
299
|
+
(i) => {
|
|
300
|
+
const r = this.defineSubscriberKey(i, t), o = this.definePluginExecution(i);
|
|
301
|
+
if (!(this._subscribersDelivered.has(r) || this._subscribersScheduled.has(r)) && this.shouldInvokePlugin(o)) {
|
|
302
|
+
if (this.shouldScheduleAfterHydration(o)) {
|
|
303
|
+
this.queueSubscriberExecution({ store: t, options: e }, i, r);
|
|
160
304
|
return;
|
|
161
305
|
}
|
|
162
|
-
this.executeSubscriber({ store:
|
|
306
|
+
this.executeSubscriber({ store: t, options: e }, i);
|
|
163
307
|
}
|
|
164
308
|
}
|
|
165
|
-
), this.rewriteResetStore({ store:
|
|
166
|
-
} catch (
|
|
167
|
-
this.logError(t, e
|
|
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.logError(i, t, e);
|
|
168
312
|
}
|
|
169
313
|
}
|
|
170
314
|
}
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
this.
|
|
175
|
-
};
|
|
315
|
+
queueSubscriberExecution(t, e, i) {
|
|
316
|
+
const r = this.defineHydrationScheduler(e);
|
|
317
|
+
this._subscribersScheduled.add(i), r(() => {
|
|
318
|
+
this._subscribersScheduled.delete(i), this.executeSubscriberSafely(t, e);
|
|
319
|
+
});
|
|
176
320
|
}
|
|
177
|
-
registerStoreCleanup(
|
|
178
|
-
if (typeof
|
|
321
|
+
registerStoreCleanup(t) {
|
|
322
|
+
if (this._onActionSubscriptions = [], typeof t.$dispose != "function")
|
|
179
323
|
return;
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const i =
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
t.endsWith(i) && this._subscribersScheduled.delete(t);
|
|
189
|
-
for (const t of Array.from(this._subscriptionsDelivered))
|
|
190
|
-
t.endsWith(i) && this._subscriptionsDelivered.delete(t);
|
|
191
|
-
for (const t of Array.from(this._subscriptionsScheduled))
|
|
192
|
-
t.endsWith(i) && this._subscriptionsScheduled.delete(t);
|
|
193
|
-
}
|
|
194
|
-
storeOnActionSubscription(e) {
|
|
195
|
-
const { store: i, callback: t } = e();
|
|
196
|
-
i.$onAction(({ after: s, args: n, name: d, onError: u }) => {
|
|
197
|
-
this.debugLog(`storeOnActionSubscription ${i.$id}`, { after: s, args: n, name: d, store: i }), t({ after: s, args: n, name: d, onError: u });
|
|
198
|
-
});
|
|
324
|
+
const e = t.$dispose.bind(t), i = t;
|
|
325
|
+
i.__piniaPluginSubscriptionDisposed || (i.__piniaPluginSubscriptionDisposed = !0, t.$dispose = () => (this.clearStoreTracking(t), this._storeRollbackSnapshotsHandler.clearStoreTracking(t), e()));
|
|
326
|
+
}
|
|
327
|
+
rewriteResetStore({ store: t }, e) {
|
|
328
|
+
const i = u(e);
|
|
329
|
+
t.$reset = () => {
|
|
330
|
+
this.debugLog("rewriteResetStore()", { initState: i, store: t }), this.executeResetStoreCallbacks(t), t.$patch(u(i));
|
|
331
|
+
};
|
|
199
332
|
}
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
333
|
+
shouldInvokePlugin(t) {
|
|
334
|
+
const e = this.defineCurrentEnvironment();
|
|
335
|
+
return t.environment === "both" || t.environment === e;
|
|
336
|
+
}
|
|
337
|
+
shouldScheduleAfterHydration(t) {
|
|
338
|
+
return this.defineCurrentEnvironment() === "client" && t.hydration === "defer";
|
|
339
|
+
}
|
|
340
|
+
storeOnActionSubscription(t) {
|
|
341
|
+
const { store: e, callback: i } = t?.storeOnActionSubscription?.() ?? {};
|
|
342
|
+
!e || !i || this._onActionSubscriptions.push(i);
|
|
343
|
+
}
|
|
344
|
+
storeMutationSubscription(t) {
|
|
345
|
+
const { store: e, callback: i } = t();
|
|
346
|
+
e.$subscribe((r) => {
|
|
347
|
+
this.debugLog(`$subscribe ${e.$id}`, { mutation: r, store: e }), i(r);
|
|
204
348
|
});
|
|
205
349
|
}
|
|
206
|
-
subscriptionDelivery(
|
|
207
|
-
Object.entries(
|
|
208
|
-
const
|
|
209
|
-
if (!(this._subscriptionsDelivered.has(
|
|
210
|
-
if (this.shouldScheduleAfterHydration(
|
|
211
|
-
const
|
|
212
|
-
this._subscriptionsScheduled.add(
|
|
213
|
-
this._subscriptionsScheduled.delete(
|
|
350
|
+
subscriptionDelivery(t, e) {
|
|
351
|
+
Object.entries(e).forEach(([i, r]) => {
|
|
352
|
+
const o = this.defineSubscriptionKey(i, t.store), h = this.definePluginExecution(r.subscription);
|
|
353
|
+
if (!(this._subscriptionsDelivered.has(o) || this._subscriptionsScheduled.has(o)) && this.shouldInvokePlugin(h)) {
|
|
354
|
+
if (this.shouldScheduleAfterHydration(h)) {
|
|
355
|
+
const c = this.defineHydrationScheduler(r.subscription);
|
|
356
|
+
this._subscriptionsScheduled.add(o), c(() => {
|
|
357
|
+
this._subscriptionsScheduled.delete(o), this.subscriptionDeliverySafely(t, i, r);
|
|
214
358
|
});
|
|
215
359
|
return;
|
|
216
360
|
}
|
|
217
|
-
this.subscriptionDeliverySafely(
|
|
361
|
+
this.subscriptionDeliverySafely(t, i, r);
|
|
218
362
|
}
|
|
219
363
|
});
|
|
220
364
|
}
|
|
221
|
-
subscriptionDeliverySafely({ store:
|
|
365
|
+
subscriptionDeliverySafely({ store: t, options: e }, i, r) {
|
|
222
366
|
try {
|
|
223
|
-
if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(
|
|
367
|
+
if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(i, t)))
|
|
224
368
|
return;
|
|
225
|
-
const { subscription:
|
|
226
|
-
this.debugLog(`subscriptionDelivery() - store: ${
|
|
369
|
+
const { subscription: o, subscriptionOptions: h, stores: c } = r;
|
|
370
|
+
this.debugLog(`subscriptionDelivery() - store: ${t.$id}`, [
|
|
227
371
|
"pluginName:",
|
|
228
|
-
|
|
372
|
+
i,
|
|
229
373
|
"subscription:",
|
|
230
|
-
|
|
374
|
+
o,
|
|
231
375
|
"options:",
|
|
232
|
-
|
|
376
|
+
r,
|
|
233
377
|
"stores:",
|
|
234
|
-
|
|
378
|
+
c
|
|
235
379
|
]);
|
|
236
|
-
const
|
|
237
|
-
storeOptions: { ...
|
|
380
|
+
const l = {
|
|
381
|
+
storeOptions: { ...e?.storeOptions ?? {}, ...h }
|
|
238
382
|
};
|
|
239
|
-
|
|
240
|
-
{ store:
|
|
383
|
+
o.invoke(
|
|
384
|
+
{ store: t, options: { ...e, ...l } },
|
|
241
385
|
this.debug
|
|
242
|
-
), this._subscriptionsDelivered.add(this.defineSubscriptionKey(
|
|
243
|
-
(
|
|
386
|
+
), this._subscriptionsDelivered.add(this.defineSubscriptionKey(i, t)), y(c) || c?.forEach(
|
|
387
|
+
(d) => o.invoke(
|
|
244
388
|
{
|
|
245
|
-
store:
|
|
389
|
+
store: d,
|
|
246
390
|
options: {
|
|
247
|
-
...
|
|
248
|
-
...
|
|
249
|
-
...
|
|
391
|
+
...e,
|
|
392
|
+
...l,
|
|
393
|
+
...d?.storeOptions ?? {}
|
|
250
394
|
}
|
|
251
395
|
},
|
|
252
396
|
this.debug
|
|
253
397
|
)
|
|
254
398
|
);
|
|
255
|
-
} catch (
|
|
256
|
-
this.logError("subscriptionDelivery()",
|
|
399
|
+
} catch (o) {
|
|
400
|
+
this.logError("subscriptionDelivery()", o, t, e);
|
|
257
401
|
}
|
|
258
402
|
}
|
|
259
403
|
}
|
|
260
|
-
function
|
|
261
|
-
const
|
|
262
|
-
return
|
|
404
|
+
function j(n, s) {
|
|
405
|
+
const t = new k(n, s);
|
|
406
|
+
return t.plugin.bind(t);
|
|
263
407
|
}
|
|
264
|
-
function
|
|
265
|
-
const
|
|
266
|
-
execution:
|
|
267
|
-
hydrationScheduler:
|
|
268
|
-
runtimeEnvironment:
|
|
269
|
-
subscriberExecution:
|
|
408
|
+
function K(n, s) {
|
|
409
|
+
const t = new k(n, s?.debug, {
|
|
410
|
+
execution: s?.execution,
|
|
411
|
+
hydrationScheduler: s?.hydrationScheduler,
|
|
412
|
+
runtimeEnvironment: s?.runtimeEnvironment,
|
|
413
|
+
subscriberExecution: s?.subscriberExecution
|
|
270
414
|
});
|
|
271
|
-
return
|
|
415
|
+
return t.plugin.bind(t);
|
|
272
416
|
}
|
|
273
417
|
export {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
418
|
+
T as CustomConsole,
|
|
419
|
+
W as PLUGIN_NAME,
|
|
420
|
+
L as PluginSubscriber,
|
|
421
|
+
k as PluginSubscription,
|
|
422
|
+
I as Store,
|
|
423
|
+
K as createHydrationPlugin,
|
|
424
|
+
j as createPlugin,
|
|
425
|
+
q as defineAStore,
|
|
426
|
+
G as defineAStoreCtx,
|
|
427
|
+
U as getDefineAStoreSetupContext,
|
|
428
|
+
z as getEnhancedStore,
|
|
429
|
+
B as getExtendingStore,
|
|
430
|
+
y as isEmpty,
|
|
431
|
+
J as setEnhancedStore
|
|
288
432
|
};
|
|
433
|
+
//# sourceMappingURL=pinia-plugin-subscription.js.map
|