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.
@@ -1,288 +1,433 @@
1
- var S = Object.defineProperty;
2
- var y = (r, o, e) => o in r ? S(r, o, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[o] = e;
3
- var c = (r, o, e) => y(r, typeof o != "symbol" ? o + "" : o, e);
4
- import { D as g, i as l, P as _ } from "./chunks/index-B-zO0yrr.js";
5
- import { C as w, p as O, a as j, S as x, d as H, b as K, g as R, c as M, e as L, s as T } from "./chunks/index-B-zO0yrr.js";
6
- const f = "PluginSubscription", m = {
7
- environment: "both",
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(r);
10
+ return structuredClone(n);
21
11
  } catch {
22
12
  }
23
- if (r === null || typeof r != "object")
24
- return r;
25
- if (r instanceof Map)
13
+ if (n === null || typeof n != "object")
14
+ return n;
15
+ if (n instanceof Map)
26
16
  return new Map(
27
- Array.from(r.entries(), ([e, i]) => [h(e), h(i)])
17
+ Array.from(n.entries(), ([t, e]) => [u(t), u(e)])
28
18
  );
29
- if (r instanceof Set)
30
- return new Set(Array.from(r.values(), (e) => h(e)));
31
- if (Array.isArray(r))
32
- return r.map((e) => h(e));
33
- if (Object.getPrototypeOf(r) === Object.prototype || Object.getPrototypeOf(r) === null)
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(r).map(([e, i]) => [e, h(i)])
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
- const o = Object.create(Object.getPrototypeOf(r));
38
- for (const [e, i] of Object.entries(r))
39
- o[e] = h(i);
40
- return o;
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 A(r) {
43
- return r !== void 0 && !Array.isArray(r);
131
+ function D(n) {
132
+ return n !== void 0 && !Array.isArray(n);
44
133
  }
45
- class p extends g {
46
- constructor(e, i, t) {
47
- const s = Array.isArray(i) ? i : void 0, n = A(i) ? i : t;
48
- super(Array.isArray(s) && !!s?.includes(f), _);
49
- c(this, "_className", f);
50
- c(this, "_hydrationScheduler");
51
- c(this, "_options");
52
- c(this, "_pluginDebug");
53
- c(this, "_resetStoreCallback", []);
54
- c(this, "_subscribers", []);
55
- c(this, "_subscribersDelivered", /* @__PURE__ */ new Set());
56
- c(this, "_subscribersScheduled", /* @__PURE__ */ new Set());
57
- c(this, "_subscriptions", []);
58
- c(this, "_subscriptionsDelivered", /* @__PURE__ */ new Set());
59
- c(this, "_subscriptionsScheduled", /* @__PURE__ */ new Set());
60
- this._hydrationScheduler = n?.hydrationScheduler ?? E, this._options = n, this._pluginDebug = Array.isArray(s) ? s : void 0, this._subscribers = e;
61
- }
62
- set subscribers(e) {
63
- this._subscribers = e;
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 (!l(this._subscriptions))
159
+ if (!y(this._subscriptions))
67
160
  return this._subscriptions;
68
161
  }
69
- addResetStoreCallback(e) {
70
- this._resetStoreCallback.push(e);
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(e) {
76
- return !!this._pluginDebug?.includes(e.name);
214
+ definePluginDebug(t) {
215
+ return !!this._pluginDebug?.includes(t.name);
77
216
  }
78
- definePluginExecution(e) {
217
+ definePluginExecution(t) {
79
218
  return {
80
- ...m,
219
+ ...P,
81
220
  ...this._options?.execution ?? {},
82
- ...e.execution ?? {},
83
- ...this._options?.subscriberExecution?.[e.name] ?? {}
221
+ ...t.execution ?? {},
222
+ ...this._options?.subscriberExecution?.[t.name] ?? {}
84
223
  };
85
224
  }
86
- defineHydrationScheduler(e) {
87
- return e.hydrationScheduler ?? this._hydrationScheduler;
225
+ defineHydrationScheduler(t) {
226
+ return t.hydrationScheduler ?? this._hydrationScheduler;
88
227
  }
89
- defineSubscriberKey(e, i) {
90
- return `${e.name}-${i.$id}`;
228
+ defineSubscriberKey(t, e) {
229
+ return `${t.name}-${e.$id}`;
91
230
  }
92
- defineSubscriptionKey(e, i) {
93
- return `${e}-${i.$id}`;
231
+ defineSubscriptionKey(t, e) {
232
+ return `${t}-${e.$id}`;
94
233
  }
95
- executeResetStoreCallbacks(e) {
96
- this._resetStoreCallback.forEach((i) => i(e));
234
+ executeResetStoreCallbacks(t) {
235
+ this._resetStoreCallback.forEach((e) => e(t));
97
236
  }
98
- executeSubscriber(e, i) {
99
- const t = this.defineSubscriberKey(i, e.store), s = this.definePluginDebug(i);
100
- if (this._subscribersDelivered.has(t) || !i.invoke(
101
- e,
102
- s
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(t);
106
- let n;
262
+ this._subscribersDelivered.add(i);
263
+ let o;
107
264
  try {
108
- n = i.hydrate?.(e, s);
109
- } catch (u) {
110
- this.logError(u, e.store, e.options);
265
+ o = e.hydrate?.(t, r);
266
+ } catch (c) {
267
+ this.logError(c, t.store, t.options);
111
268
  return;
112
269
  }
113
- const d = () => {
270
+ const h = () => {
114
271
  try {
115
- const u = i.afterHydration?.(e, s);
116
- u && typeof u.then == "function" && u.catch((a) => this.logError(a, e.store, e.options));
117
- } catch (u) {
118
- this.logError(u, e.store, e.options);
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
- n && typeof n.then == "function" ? n.then(() => d()).catch((u) => this.logError(u, e.store, e.options)) : d(), i.subscriptions && this.subscriptionDelivery(e, i.subscriptions), i.storeMutationSubscription && this.storeMutationSubscription(i.storeMutationSubscription), i.storeOnActionSubscription && this.storeOnActionSubscription(i.storeOnActionSubscription), i.resetStoreCallback && this.addResetStoreCallback(i.resetStoreCallback);
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(e, i) {
280
+ executeSubscriberSafely(t, e) {
124
281
  try {
125
- this.executeSubscriber(e, i);
126
- } catch (t) {
127
- this.logError(t, e.store, e.options);
282
+ this.executeSubscriber(t, e);
283
+ } catch (i) {
284
+ this.logError(i, t.store, t.options);
128
285
  }
129
286
  }
130
- queueSubscriberExecution(e, i, t) {
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() - ${e.$id}`, [
289
+ this.debugLog(`plugin() - ${t.$id}`, [
146
290
  "subscriber:",
147
291
  this._subscribers,
148
292
  "store:",
149
- e,
293
+ t,
150
294
  "options:",
151
- i
295
+ e
152
296
  ]);
153
297
  try {
154
298
  this._subscribers.forEach(
155
- (t) => {
156
- const s = this.defineSubscriberKey(t, e), n = this.definePluginExecution(t);
157
- if (!(this._subscribersDelivered.has(s) || this._subscribersScheduled.has(s)) && this.shouldInvokePlugin(n)) {
158
- if (this.shouldScheduleAfterHydration(n)) {
159
- this.queueSubscriberExecution({ store: e, options: i }, t, s);
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: e, options: i }, t);
306
+ this.executeSubscriber({ store: t, options: e }, i);
163
307
  }
164
308
  }
165
- ), this.rewriteResetStore({ store: e }, h(e.$state), Object.assign({}, e)), this.registerStoreCleanup(e);
166
- } catch (t) {
167
- this.logError(t, e, i);
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
- rewriteResetStore({ store: e }, i, t) {
172
- const s = h(i);
173
- e.$reset = () => {
174
- this.debugLog("rewriteResetStore()", { initState: s, store: e, customStore: t }), this.executeResetStoreCallbacks(e), e.$patch(h(s));
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(e) {
178
- if (typeof e.$dispose != "function")
321
+ registerStoreCleanup(t) {
322
+ if (this._onActionSubscriptions = [], typeof t.$dispose != "function")
179
323
  return;
180
- const i = e.$dispose.bind(e), t = e;
181
- t.__piniaPluginSubscriptionDisposed || (t.__piniaPluginSubscriptionDisposed = !0, e.$dispose = () => (this.clearStoreTracking(e), i()));
182
- }
183
- clearStoreTracking(e) {
184
- const i = `-${e.$id}`;
185
- for (const t of Array.from(this._subscribersDelivered))
186
- t.endsWith(i) && this._subscribersDelivered.delete(t);
187
- for (const t of Array.from(this._subscribersScheduled))
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
- storeMutationSubscription(e) {
201
- const { store: i, callback: t } = e();
202
- i.$subscribe((s) => {
203
- this.debugLog(`$subscribe ${i.$id}`, { mutation: s, store: i }), t(s);
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(e, i) {
207
- Object.entries(i).forEach(([t, s]) => {
208
- const n = this.defineSubscriptionKey(t, e.store), d = this.definePluginExecution(s.subscription);
209
- if (!(this._subscriptionsDelivered.has(n) || this._subscriptionsScheduled.has(n)) && this.shouldInvokePlugin(d)) {
210
- if (this.shouldScheduleAfterHydration(d)) {
211
- const u = this.defineHydrationScheduler(s.subscription);
212
- this._subscriptionsScheduled.add(n), u(() => {
213
- this._subscriptionsScheduled.delete(n), this.subscriptionDeliverySafely(e, t, s);
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(e, t, s);
361
+ this.subscriptionDeliverySafely(t, i, r);
218
362
  }
219
363
  });
220
364
  }
221
- subscriptionDeliverySafely({ store: e, options: i }, t, s) {
365
+ subscriptionDeliverySafely({ store: t, options: e }, i, r) {
222
366
  try {
223
- if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(t, e)))
367
+ if (this._subscriptionsDelivered.has(this.defineSubscriptionKey(i, t)))
224
368
  return;
225
- const { subscription: n, subscriptionOptions: d, stores: u } = s;
226
- this.debugLog(`subscriptionDelivery() - store: ${e.$id}`, [
369
+ const { subscription: o, subscriptionOptions: h, stores: c } = r;
370
+ this.debugLog(`subscriptionDelivery() - store: ${t.$id}`, [
227
371
  "pluginName:",
228
- t,
372
+ i,
229
373
  "subscription:",
230
- n,
374
+ o,
231
375
  "options:",
232
- s,
376
+ r,
233
377
  "stores:",
234
- u
378
+ c
235
379
  ]);
236
- const a = {
237
- storeOptions: { ...i?.storeOptions ?? {}, ...d }
380
+ const l = {
381
+ storeOptions: { ...e?.storeOptions ?? {}, ...h }
238
382
  };
239
- n.invoke(
240
- { store: e, options: { ...i, ...a } },
383
+ o.invoke(
384
+ { store: t, options: { ...e, ...l } },
241
385
  this.debug
242
- ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(t, e)), l(u) || u?.forEach(
243
- (b) => n.invoke(
386
+ ), this._subscriptionsDelivered.add(this.defineSubscriptionKey(i, t)), y(c) || c?.forEach(
387
+ (d) => o.invoke(
244
388
  {
245
- store: b,
389
+ store: d,
246
390
  options: {
247
- ...i,
248
- ...a,
249
- ...b?.storeOptions ?? {}
391
+ ...e,
392
+ ...l,
393
+ ...d?.storeOptions ?? {}
250
394
  }
251
395
  },
252
396
  this.debug
253
397
  )
254
398
  );
255
- } catch (n) {
256
- this.logError("subscriptionDelivery()", n, e, i);
399
+ } catch (o) {
400
+ this.logError("subscriptionDelivery()", o, t, e);
257
401
  }
258
402
  }
259
403
  }
260
- function $(r, o) {
261
- const e = new p(r, o);
262
- return e.plugin.bind(e);
404
+ function j(n, s) {
405
+ const t = new k(n, s);
406
+ return t.plugin.bind(t);
263
407
  }
264
- function P(r, o) {
265
- const e = new p(r, o?.debug, {
266
- execution: o?.execution,
267
- hydrationScheduler: o?.hydrationScheduler,
268
- runtimeEnvironment: o?.runtimeEnvironment,
269
- subscriberExecution: o?.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 e.plugin.bind(e);
415
+ return t.plugin.bind(t);
272
416
  }
273
417
  export {
274
- w as CustomConsole,
275
- O as PLUGIN_NAME,
276
- j as PluginSubscriber,
277
- p as PluginSubscription,
278
- x as Store,
279
- P as createHydrationPlugin,
280
- $ as createPlugin,
281
- H as defineAStore,
282
- K as defineAStoreCtx,
283
- R as getDefineAStoreSetupContext,
284
- M as getEnhancedStore,
285
- L as getExtendingStore,
286
- l as isEmpty,
287
- T as setEnhancedStore
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