@yoltra/core 0.4.0 → 0.6.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/README.es.md +214 -7
- package/README.md +315 -13
- package/dist/types/eventBus/EventBus.d.ts +1 -1
- package/dist/types/eventBus/index.d.ts +2 -2
- package/dist/types/index.d.ts +21 -16
- package/dist/types/persistence/adapters.d.ts +1 -1
- package/dist/types/persistence/persist.d.ts +3 -6
- package/dist/types/reducer/Reducer.d.ts +4 -3
- package/dist/types/store/Store.d.ts +252 -17
- package/dist/types/store/call.d.ts +149 -0
- package/dist/types/store/callQueue.d.ts +79 -0
- package/dist/types/store/rejection.d.ts +58 -0
- package/dist/types/types.d.ts +279 -14
- package/dist/types/utils/detectChangedProps.d.ts +6 -0
- package/dist/types/utils/immutability.d.ts +1 -1
- package/dist/types/utils/index.d.ts +2 -2
- package/dist/yoltra.cjs +11 -0
- package/dist/yoltra.cjs.map +1 -0
- package/dist/yoltra.mjs +2902 -0
- package/dist/yoltra.mjs.map +1 -0
- package/dist/yoltra.umd.js +2 -2
- package/dist/yoltra.umd.js.map +1 -0
- package/package.json +21 -21
- package/dist/yoltra.cjs.js +0 -11
- package/dist/yoltra.esm.js +0 -2374
package/dist/yoltra.esm.js
DELETED
|
@@ -1,2374 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @yoltra/core v0.4.0
|
|
3
|
-
* (c) 2026 Manu Ramirez <@pixerael>
|
|
4
|
-
* License: MIT
|
|
5
|
-
* Homepage: https://yoltra.dev
|
|
6
|
-
*
|
|
7
|
-
* This source code is licensed under the MIT license found in the
|
|
8
|
-
* LICENSE file in the root directory of this source tree
|
|
9
|
-
*/
|
|
10
|
-
var T = Object.defineProperty;
|
|
11
|
-
var C = (o, e, t) => e in o ? T(o, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[e] = t;
|
|
12
|
-
var p = (o, e, t) => C(o, typeof e != "symbol" ? e + "" : e, t);
|
|
13
|
-
class B {
|
|
14
|
-
constructor() {
|
|
15
|
-
/**
|
|
16
|
-
* Internal registry: `channel → type → Set<handler>`.
|
|
17
|
-
* @internal
|
|
18
|
-
*/
|
|
19
|
-
p(this, "handlers", /* @__PURE__ */ new Map());
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Subscribes a handler to an exact `(channel, type)`.
|
|
23
|
-
*
|
|
24
|
-
* @typeParam C - Channel key (must be a string key of `EM`).
|
|
25
|
-
* @typeParam T - Type key within channel `C` (must be a string key of `EM[C]`).
|
|
26
|
-
* @param channel - Channel name to subscribe to.
|
|
27
|
-
* @param type - Event type within the channel.
|
|
28
|
-
* @param handler - Function invoked with the payload type `EM[C][T]`. It optionally
|
|
29
|
-
* receives the **source event** as a second argument when the emitter supplies one, so
|
|
30
|
-
* subscribers can read the true `id` (and any `meta`) instead of reconstructing an event
|
|
31
|
-
* from the payload alone. Handlers that declare only `payload` remain valid.
|
|
32
|
-
* @returns An **unsubscribe** function that removes this handler.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* const off = bus.on('data', 'loaded', ({ items }) => {
|
|
37
|
-
* console.log('Loaded', items.length, 'items');
|
|
38
|
-
* });
|
|
39
|
-
*
|
|
40
|
-
* // Later, stop listening:
|
|
41
|
-
* off();
|
|
42
|
-
* ```
|
|
43
|
-
*
|
|
44
|
-
* @example Reading the source event
|
|
45
|
-
* ```ts
|
|
46
|
-
* bus.on('data', 'loaded', (payload, event) => {
|
|
47
|
-
* console.log('event id:', event?.id);
|
|
48
|
-
* });
|
|
49
|
-
* ```
|
|
50
|
-
*
|
|
51
|
-
* @public
|
|
52
|
-
*/
|
|
53
|
-
on(e, t, n) {
|
|
54
|
-
let s = this.handlers.get(e);
|
|
55
|
-
s || (s = /* @__PURE__ */ new Map(), this.handlers.set(e, s));
|
|
56
|
-
let i = s.get(t);
|
|
57
|
-
return i || (i = /* @__PURE__ */ new Set(), s.set(t, i)), i.add(n), () => this.off(e, t, n);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Removes a specific handler previously added with {@link EventBus.on | `on`}.
|
|
61
|
-
*
|
|
62
|
-
* @typeParam C - Channel key (string key of `EM`).
|
|
63
|
-
* @typeParam T - Type key within channel `C` (string key of `EM[C]`).
|
|
64
|
-
* @param channel - Channel name of the subscription to remove.
|
|
65
|
-
* @param type - Event type of the subscription to remove.
|
|
66
|
-
* @param handler - The same handler reference that was passed to `on`.
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```ts
|
|
70
|
-
* const h = (n: number) => console.log('inc', n);
|
|
71
|
-
* bus.on('math', 'inc', h);
|
|
72
|
-
*
|
|
73
|
-
* // Explicitly remove this handler:
|
|
74
|
-
* bus.off('math', 'inc', h);
|
|
75
|
-
* ```
|
|
76
|
-
*
|
|
77
|
-
* @public
|
|
78
|
-
*/
|
|
79
|
-
off(e, t, n) {
|
|
80
|
-
const s = this.handlers.get(e);
|
|
81
|
-
if (!s) return;
|
|
82
|
-
const i = s.get(t);
|
|
83
|
-
i && (i.delete(n), i.size === 0 && s.delete(t), s.size === 0 && this.handlers.delete(e));
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Emits an event to all subscribers of the exact `(channel, type)`.
|
|
87
|
-
*
|
|
88
|
-
* Handlers are invoked **synchronously**. Any exception thrown by a handler is
|
|
89
|
-
* caught and logged, and other handlers still run.
|
|
90
|
-
*
|
|
91
|
-
* @typeParam C - Channel key (string key of `EM`).
|
|
92
|
-
* @typeParam T - Type key within channel `C` (string key of `EM[C]`).
|
|
93
|
-
* @param channel - Channel name to emit on.
|
|
94
|
-
* @param type - Event type to emit.
|
|
95
|
-
* @param payload - Payload matching `EM[C][T]`.
|
|
96
|
-
* @param event - Optional **source event**, forwarded to handlers as a second argument.
|
|
97
|
-
* Supply it whenever the caller already holds the real event so subscribers observe its
|
|
98
|
-
* true `id` rather than reconstructing one; omitting it keeps the original behaviour.
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```ts
|
|
102
|
-
* bus.emit('ui', 'toggle', false);
|
|
103
|
-
* ```
|
|
104
|
-
*
|
|
105
|
-
* @public
|
|
106
|
-
*/
|
|
107
|
-
emit(e, t, n, s) {
|
|
108
|
-
const i = this.handlers.get(e);
|
|
109
|
-
if (!i) return;
|
|
110
|
-
const d = i.get(t);
|
|
111
|
-
if (!(!d || d.size === 0))
|
|
112
|
-
for (const a of [...d])
|
|
113
|
-
try {
|
|
114
|
-
a(n, s);
|
|
115
|
-
} catch (r) {
|
|
116
|
-
console.error("EventBus handler error:", r);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Clears **all** listeners across all channels/types.
|
|
121
|
-
*
|
|
122
|
-
* Useful for tests or during HMR teardown to avoid duplicate handlers.
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* ```ts
|
|
126
|
-
* // In a test teardown:
|
|
127
|
-
* afterEach(() => bus.clear());
|
|
128
|
-
* ```
|
|
129
|
-
*
|
|
130
|
-
* @public
|
|
131
|
-
*/
|
|
132
|
-
clear() {
|
|
133
|
-
this.handlers.clear();
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
class H {
|
|
137
|
-
constructor() {
|
|
138
|
-
/**
|
|
139
|
-
* Exact handlers: `channel → type → [handlers]`.
|
|
140
|
-
* @internal
|
|
141
|
-
*/
|
|
142
|
-
p(this, "handlers", /* @__PURE__ */ new Map());
|
|
143
|
-
/**
|
|
144
|
-
* Pattern handlers with `*` and `**`: `channel → pattern(string) → [handlers]`.
|
|
145
|
-
* @internal
|
|
146
|
-
*/
|
|
147
|
-
p(this, "patternHandlers", /* @__PURE__ */ new Map());
|
|
148
|
-
/**
|
|
149
|
-
* Patterns bucketed by their first segment, so an emit tests only what could match.
|
|
150
|
-
*
|
|
151
|
-
* @remarks
|
|
152
|
-
* Delivery used to walk every pattern registered on the channel and run the full segment
|
|
153
|
-
* matcher against each. That is linear in the number of patterns rather than in the number
|
|
154
|
-
* that match, and it re-split both the pattern and the subject on every test — for a thousand
|
|
155
|
-
* patterns, two thousand string splits to deliver one event.
|
|
156
|
-
*
|
|
157
|
-
* A subject's first segment can only be matched by a pattern whose first segment is that same
|
|
158
|
-
* literal, or is `*` or `**`. Bucketing on that turns the common shape — distinct event
|
|
159
|
-
* families like `panel.*` and `order.**` — from a scan of everything into a map lookup plus
|
|
160
|
-
* the handful that begin with a wildcard.
|
|
161
|
-
*
|
|
162
|
-
* It buys nothing for a channel where every pattern starts with `**`, since all of those must
|
|
163
|
-
* still be tested. That is the honest worst case, and it is unchanged rather than worsened.
|
|
164
|
-
*/
|
|
165
|
-
p(this, "patternIndex", /* @__PURE__ */ new Map());
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Subscribes a handler to either an **exact** type or a **pattern**.
|
|
169
|
-
*
|
|
170
|
-
* @param channel - Channel to subscribe on.
|
|
171
|
-
* @param type - Exact event type (e.g. `"a.b"`) or pattern (contains `*`/`**`).
|
|
172
|
-
* @param handler - Function invoked with the emitted payload.
|
|
173
|
-
* @returns An **unsubscribe** function that removes this handler.
|
|
174
|
-
*
|
|
175
|
-
* @remarks
|
|
176
|
-
* - Exact subscriptions are stored under a **normalized** key (leading `.` removed).
|
|
177
|
-
* - Pattern subscriptions are stored **as provided**; matching normalizes the subject.
|
|
178
|
-
*
|
|
179
|
-
* @example Exact subscription
|
|
180
|
-
* ```ts
|
|
181
|
-
* const off = bus.on('data', 'items.loaded', ({ count }) => {
|
|
182
|
-
* console.log('Loaded', count);
|
|
183
|
-
* });
|
|
184
|
-
* // Later
|
|
185
|
-
* off();
|
|
186
|
-
* ```
|
|
187
|
-
*
|
|
188
|
-
* @example Pattern subscription
|
|
189
|
-
* ```ts
|
|
190
|
-
* // Match any single sub-event: 'panel.open', 'panel.close', etc.
|
|
191
|
-
* const offStar = bus.on('ui', 'panel.*', () => {});
|
|
192
|
-
*
|
|
193
|
-
* // Match any depth: 'panel.open', 'panel.items.add', 'panel', etc.
|
|
194
|
-
* const offGlob = bus.on('ui', 'panel.**', () => {});
|
|
195
|
-
* ```
|
|
196
|
-
*
|
|
197
|
-
* @public
|
|
198
|
-
*/
|
|
199
|
-
on(e, t, n) {
|
|
200
|
-
const s = String(t);
|
|
201
|
-
if (this.isPattern(s)) {
|
|
202
|
-
const i = s;
|
|
203
|
-
this.patternHandlers.has(e) || this.patternHandlers.set(e, /* @__PURE__ */ new Map());
|
|
204
|
-
const d = this.patternHandlers.get(e);
|
|
205
|
-
return d.has(i) || (d.set(i, []), this.indexPattern(e, i)), d.get(i).push(n), () => this.offPattern(e, i, n);
|
|
206
|
-
} else {
|
|
207
|
-
const i = this.normalizeTypeKey(s);
|
|
208
|
-
this.handlers.has(e) || this.handlers.set(e, /* @__PURE__ */ new Map());
|
|
209
|
-
const d = this.handlers.get(e);
|
|
210
|
-
return d.has(i) || d.set(i, []), d.get(i).push(n), () => this.offExactNormalized(e, i, n);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Unsubscribes an **exact** handler. The `type` key is normalized internally,
|
|
215
|
-
* so callers can pass `"foo"` or `".foo"` interchangeably.
|
|
216
|
-
*
|
|
217
|
-
* @param channel - Channel name.
|
|
218
|
-
* @param type - Exact event type key to remove (normalization applied).
|
|
219
|
-
* @param handler - The same handler reference previously passed to {@link LooseEventBus.on | `on`}.
|
|
220
|
-
*
|
|
221
|
-
* @example
|
|
222
|
-
* ```ts
|
|
223
|
-
* const h = () => {};
|
|
224
|
-
* bus.on('ui', 'panel.open', h);
|
|
225
|
-
* // Remove it (with or without leading dot)
|
|
226
|
-
* bus.off('ui', '.panel.open', h);
|
|
227
|
-
* ```
|
|
228
|
-
*
|
|
229
|
-
* @public
|
|
230
|
-
*/
|
|
231
|
-
off(e, t, n) {
|
|
232
|
-
const s = this.normalizeTypeKey(String(t));
|
|
233
|
-
this.offExactNormalized(e, s, n);
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Internal exact unsubscription using an already **normalized** type key.
|
|
237
|
-
*
|
|
238
|
-
* @param channel - Channel name.
|
|
239
|
-
* @param normalizedType - Event type key with leading dot removed.
|
|
240
|
-
* @param handler - Handler to remove.
|
|
241
|
-
* @internal
|
|
242
|
-
*/
|
|
243
|
-
offExactNormalized(e, t, n) {
|
|
244
|
-
const s = this.handlers.get(e);
|
|
245
|
-
if (!s) return;
|
|
246
|
-
const i = s.get(t);
|
|
247
|
-
if (!i) return;
|
|
248
|
-
const d = i.indexOf(n);
|
|
249
|
-
d !== -1 && i.splice(d, 1), i.length === 0 && s.delete(t), s.size === 0 && this.handlers.delete(e);
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Internal removal for a **pattern** subscription. No-ops if missing.
|
|
253
|
-
*
|
|
254
|
-
* @param channel - Channel name.
|
|
255
|
-
* @param pattern - Pattern string as originally subscribed.
|
|
256
|
-
* @param handler - Handler to remove.
|
|
257
|
-
* @internal
|
|
258
|
-
*/
|
|
259
|
-
offPattern(e, t, n) {
|
|
260
|
-
const s = this.patternHandlers.get(e);
|
|
261
|
-
if (!s) return;
|
|
262
|
-
const i = s.get(t);
|
|
263
|
-
if (!i) return;
|
|
264
|
-
const d = i.indexOf(n);
|
|
265
|
-
d !== -1 && i.splice(d, 1), i.length === 0 && (s.delete(t), this.unindexPattern(e, t)), s.size === 0 && (this.patternHandlers.delete(e), this.patternIndex.delete(e));
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Emits an event to all exact subscribers first, then to **matching pattern** subscribers.
|
|
269
|
-
* Duplicate handler references are called **once** (de-duped).
|
|
270
|
-
*
|
|
271
|
-
* @param channel - Channel to emit on.
|
|
272
|
-
* @param type - Event type (subject). A leading dot is ignored for matching.
|
|
273
|
-
* @param payload - Payload delivered to handlers.
|
|
274
|
-
*
|
|
275
|
-
* @example
|
|
276
|
-
* ```ts
|
|
277
|
-
* // Suppose:
|
|
278
|
-
* // - on('ui', 'panel.open', h)
|
|
279
|
-
* // - on('ui', 'panel.*', h) // same handler ref!
|
|
280
|
-
* // - on('ui', 'panel.**', other)
|
|
281
|
-
* bus.emit('ui', 'panel.open', { id: 1 });
|
|
282
|
-
* // => 'h' runs once (de-duped), then 'other'
|
|
283
|
-
* ```
|
|
284
|
-
*
|
|
285
|
-
* @public
|
|
286
|
-
*/
|
|
287
|
-
emit(e, t, n) {
|
|
288
|
-
const s = String(t), i = this.normalizeTypeKey(s), d = this.handlers.get(e)?.get(i) ?? [], a = this.matchingPatternHandlers(e, s), r = /* @__PURE__ */ new Set(), f = (c) => {
|
|
289
|
-
for (const u of [...c])
|
|
290
|
-
if (!r.has(u)) {
|
|
291
|
-
r.add(u);
|
|
292
|
-
try {
|
|
293
|
-
u(n);
|
|
294
|
-
} catch (l) {
|
|
295
|
-
console.error(l);
|
|
296
|
-
continue;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
f(d);
|
|
301
|
-
for (const c of a) f(c);
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Emits a payload that is only built if somebody is listening.
|
|
305
|
-
*
|
|
306
|
-
* @param channel - Channel to emit on.
|
|
307
|
-
* @param type - Concrete event type.
|
|
308
|
-
* @param make - Builds the payload. Called at most once, and only when a handler matched.
|
|
309
|
-
*
|
|
310
|
-
* @remarks
|
|
311
|
-
* Same matching as {@link LooseEventBus.emit}; the difference is *when* the payload exists.
|
|
312
|
-
* The store's change notification carries the old and new value at a path, and reading those
|
|
313
|
-
* means walking the state tree twice per path. Doing that eagerly meant a slice nobody had
|
|
314
|
-
* subscribed to paid the full cost of describing changes to an audience of nobody — the
|
|
315
|
-
* matching work was already being done to discover there were no handlers.
|
|
316
|
-
*
|
|
317
|
-
* @public
|
|
318
|
-
*/
|
|
319
|
-
emitWith(e, t, n) {
|
|
320
|
-
const s = String(t), i = this.normalizeTypeKey(s), d = this.handlers.get(e)?.get(i) ?? [], a = this.matchingPatternHandlers(e, s);
|
|
321
|
-
if (d.length === 0 && a.length === 0) return;
|
|
322
|
-
const r = n(), f = /* @__PURE__ */ new Set(), c = (u) => {
|
|
323
|
-
for (const l of [...u])
|
|
324
|
-
if (!f.has(l)) {
|
|
325
|
-
f.add(l);
|
|
326
|
-
try {
|
|
327
|
-
l(r);
|
|
328
|
-
} catch (y) {
|
|
329
|
-
console.error(y);
|
|
330
|
-
continue;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
c(d);
|
|
335
|
-
for (const u of a) c(u);
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Determines if a string is a **pattern** (contains `*`).
|
|
339
|
-
* @param s - Event type or pattern string.
|
|
340
|
-
* @returns `true` if it contains at least one `*`, else `false`.
|
|
341
|
-
* @internal
|
|
342
|
-
*/
|
|
343
|
-
isPattern(e) {
|
|
344
|
-
return e.includes("*");
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Normalizes event type keys for exact matching by stripping a **single** leading dot.
|
|
348
|
-
*
|
|
349
|
-
* @param s - Event type key.
|
|
350
|
-
* @returns Normalized key without a leading dot.
|
|
351
|
-
* @example
|
|
352
|
-
* ```ts
|
|
353
|
-
* normalizeTypeKey('.a.b') // 'a.b'
|
|
354
|
-
* normalizeTypeKey('a.b') // 'a.b'
|
|
355
|
-
* ```
|
|
356
|
-
* @internal
|
|
357
|
-
*/
|
|
358
|
-
normalizeTypeKey(e) {
|
|
359
|
-
return e.replace(/^\./, "");
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Splits a path into dot-separated segments after normalization and removes empties.
|
|
363
|
-
* @param p - Event type or pattern string.
|
|
364
|
-
* @internal
|
|
365
|
-
*/
|
|
366
|
-
splitPath(e) {
|
|
367
|
-
return this.normalizeTypeKey(e).split(".").filter(Boolean);
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Files a pattern under the first segment that could select it.
|
|
371
|
-
* @internal
|
|
372
|
-
*/
|
|
373
|
-
indexPattern(e, t) {
|
|
374
|
-
let n = this.patternIndex.get(e);
|
|
375
|
-
n === void 0 && (n = { byHead: /* @__PURE__ */ new Map(), anyHead: [] }, this.patternIndex.set(e, n));
|
|
376
|
-
const s = this.splitPath(t), i = { pattern: t, segments: s }, d = s[0];
|
|
377
|
-
if (d === void 0 || d === "*" || d === "**") {
|
|
378
|
-
n.anyHead.push(i);
|
|
379
|
-
return;
|
|
380
|
-
}
|
|
381
|
-
const a = n.byHead.get(d);
|
|
382
|
-
a === void 0 ? n.byHead.set(d, [i]) : a.push(i);
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* Removes a pattern from the index. Paired with {@link LooseEventBus.offPattern}.
|
|
386
|
-
* @internal
|
|
387
|
-
*/
|
|
388
|
-
unindexPattern(e, t) {
|
|
389
|
-
const n = this.patternIndex.get(e);
|
|
390
|
-
if (n === void 0) return;
|
|
391
|
-
const s = this.splitPath(t)[0], i = s === void 0 || s === "*" || s === "**" ? n.anyHead : n.byHead.get(s);
|
|
392
|
-
if (i === void 0) return;
|
|
393
|
-
const d = i.findIndex((a) => a.pattern === t);
|
|
394
|
-
d !== -1 && i.splice(d, 1), i.length === 0 && i !== n.anyHead && s !== void 0 && n.byHead.delete(s);
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* The handler lists of every pattern matching this subject.
|
|
398
|
-
*
|
|
399
|
-
* @remarks
|
|
400
|
-
* Shared by `emit` and `emitWith` so the two cannot drift on what "matching" means — which
|
|
401
|
-
* they could, being two copies of the same walk before.
|
|
402
|
-
*
|
|
403
|
-
* The subject is split once here rather than once per pattern tested.
|
|
404
|
-
*
|
|
405
|
-
* @internal
|
|
406
|
-
*/
|
|
407
|
-
matchingPatternHandlers(e, t) {
|
|
408
|
-
const n = this.patternHandlers.get(e), s = this.patternIndex.get(e);
|
|
409
|
-
if (n === void 0 || n.size === 0 || s === void 0) return [];
|
|
410
|
-
const i = this.splitPath(t), d = [], a = (f) => {
|
|
411
|
-
for (const c of f) {
|
|
412
|
-
if (!this.matchSegments(c.segments, i)) continue;
|
|
413
|
-
const u = n.get(c.pattern);
|
|
414
|
-
u !== void 0 && d.push(u);
|
|
415
|
-
}
|
|
416
|
-
}, r = i[0];
|
|
417
|
-
if (r !== void 0) {
|
|
418
|
-
const f = s.byHead.get(r);
|
|
419
|
-
f !== void 0 && a(f);
|
|
420
|
-
}
|
|
421
|
-
return a(s.anyHead), d;
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Pattern matcher over dot-separated segments, which arrive already split.
|
|
425
|
-
*
|
|
426
|
-
* Rules:
|
|
427
|
-
* - **literal**: exact match.
|
|
428
|
-
* - `*` : matches exactly **one** segment.
|
|
429
|
-
* - `**` : matches **zero or more** remaining segments (including empty).
|
|
430
|
-
*
|
|
431
|
-
* @remarks
|
|
432
|
-
* Takes segments rather than strings so delivery can split each pattern once at registration
|
|
433
|
-
* and the subject once per emit, instead of both once per test. Re-splitting per test was most
|
|
434
|
-
* of what made wildcard delivery expensive: a thousand patterns meant two thousand string
|
|
435
|
-
* splits to deliver one event.
|
|
436
|
-
*
|
|
437
|
-
* @param pSegs - Pattern segments (may include `*`/`**`).
|
|
438
|
-
* @param sSegs - Subject segments to test.
|
|
439
|
-
* @returns `true` if the pattern matches; otherwise `false`.
|
|
440
|
-
*
|
|
441
|
-
* @example
|
|
442
|
-
* ```ts
|
|
443
|
-
* matchSegments(['a', '*'], ['a', 'b']) // true
|
|
444
|
-
* matchSegments(['a', '*'], ['a', 'b', 'c']) // false
|
|
445
|
-
* matchSegments(['a', '**'], ['a']) // true
|
|
446
|
-
* matchSegments(['**', 'end'], ['x', 'y', 'end']) // true
|
|
447
|
-
* ```
|
|
448
|
-
*
|
|
449
|
-
* @internal
|
|
450
|
-
*/
|
|
451
|
-
matchSegments(e, t) {
|
|
452
|
-
let n = 0, s = 0, i = -1, d = 0;
|
|
453
|
-
for (; s < t.length; )
|
|
454
|
-
if (n < e.length && (e[n] === "*" || e[n] === t[s]))
|
|
455
|
-
n++, s++;
|
|
456
|
-
else if (n < e.length && e[n] === "**")
|
|
457
|
-
i = n, d = s, n++;
|
|
458
|
-
else if (i !== -1)
|
|
459
|
-
n = i + 1, s = ++d;
|
|
460
|
-
else
|
|
461
|
-
return !1;
|
|
462
|
-
for (; n < e.length && e[n] === "**"; ) n++;
|
|
463
|
-
return n === e.length;
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Removes **all** listeners (exact and pattern). Useful for tests/HMR teardown.
|
|
467
|
-
*
|
|
468
|
-
* @example
|
|
469
|
-
* ```ts
|
|
470
|
-
* afterEach(() => bus.clear());
|
|
471
|
-
* ```
|
|
472
|
-
*
|
|
473
|
-
* @public
|
|
474
|
-
*/
|
|
475
|
-
clear() {
|
|
476
|
-
this.handlers.clear(), this.patternHandlers.clear(), this.patternIndex.clear();
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Returns a snapshot of all registered subscriptions for DevTools introspection.
|
|
480
|
-
*
|
|
481
|
-
* @returns An array of `{ channel, type, count }` entries for each distinct
|
|
482
|
-
* (channel, type/pattern) pair with at least one handler.
|
|
483
|
-
*
|
|
484
|
-
* @internal
|
|
485
|
-
*/
|
|
486
|
-
__introspect() {
|
|
487
|
-
const e = [];
|
|
488
|
-
for (const [t, n] of this.handlers)
|
|
489
|
-
for (const [s, i] of n)
|
|
490
|
-
i.length > 0 && e.push({ channel: t, type: s, count: i.length });
|
|
491
|
-
for (const [t, n] of this.patternHandlers)
|
|
492
|
-
for (const [s, i] of n)
|
|
493
|
-
i.length > 0 && e.push({ channel: t, type: s, count: i.length });
|
|
494
|
-
return e;
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
class K {
|
|
498
|
-
/**
|
|
499
|
-
* Creates a new {@link Reducer} from a pure reducer function.
|
|
500
|
-
*
|
|
501
|
-
* @param reduce - A function `(state, event) => nextState` that implements your update logic.
|
|
502
|
-
*
|
|
503
|
-
* @example
|
|
504
|
-
* ```ts
|
|
505
|
-
* const reducer = new Reducer<MyState, MyEM>((state, event) => {
|
|
506
|
-
* // implement your transitions here
|
|
507
|
-
* return state;
|
|
508
|
-
* });
|
|
509
|
-
* ```
|
|
510
|
-
*
|
|
511
|
-
* @public
|
|
512
|
-
*/
|
|
513
|
-
constructor(e) {
|
|
514
|
-
/**
|
|
515
|
-
* The underlying pure reducer function.
|
|
516
|
-
* @internal
|
|
517
|
-
*/
|
|
518
|
-
p(this, "_reduce");
|
|
519
|
-
this._reduce = e;
|
|
520
|
-
}
|
|
521
|
-
/**
|
|
522
|
-
* Applies the reducer to produce the next state.
|
|
523
|
-
*
|
|
524
|
-
* @param state - Current state.
|
|
525
|
-
* @param event - An event drawn from {@link EventUnion | `EventUnion<EM>`}.
|
|
526
|
-
* @returns The next state produced by the underlying reducer function.
|
|
527
|
-
*
|
|
528
|
-
* @example
|
|
529
|
-
* ```ts
|
|
530
|
-
* const next = reducer.reduce(curr, someEvent as EventUnion<MyEM>);
|
|
531
|
-
* ```
|
|
532
|
-
*
|
|
533
|
-
* @public
|
|
534
|
-
*/
|
|
535
|
-
reduce(e, t) {
|
|
536
|
-
return this._reduce(e, t);
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
const P = /* @__PURE__ */ new Set();
|
|
540
|
-
function M(o, e) {
|
|
541
|
-
const t = o ? `${o}.${e}` : e;
|
|
542
|
-
P.has(t) || (P.add(t), console.warn(
|
|
543
|
-
`[yoltra] State key "${e}"${o ? ` under "${o}"` : ""} contains a dot. Paths are dotted, so this key is indistinguishable from nested objects of the same name: a subscription to "${t}" may match the wrong value, and DevTools patches for it will address the wrong node. Rename the key, or nest it.`
|
|
544
|
-
));
|
|
545
|
-
}
|
|
546
|
-
function O(o, e, t = "", n = /* @__PURE__ */ new Map()) {
|
|
547
|
-
const s = [];
|
|
548
|
-
return v(o, e, t, n, s), s;
|
|
549
|
-
}
|
|
550
|
-
function v(o, e, t, n, s) {
|
|
551
|
-
if (o === e) return;
|
|
552
|
-
if (typeof o != "object" || typeof e != "object" || o === null || e === null) {
|
|
553
|
-
if (typeof o == "number" && Number.isNaN(o) && Number.isNaN(e))
|
|
554
|
-
return;
|
|
555
|
-
s.push(t);
|
|
556
|
-
return;
|
|
557
|
-
}
|
|
558
|
-
if (o instanceof Date && e instanceof Date) {
|
|
559
|
-
o.getTime() !== e.getTime() && s.push(t);
|
|
560
|
-
return;
|
|
561
|
-
}
|
|
562
|
-
if (o instanceof RegExp && e instanceof RegExp) {
|
|
563
|
-
(o.source !== e.source || e.flags !== o.flags) && s.push(t);
|
|
564
|
-
return;
|
|
565
|
-
}
|
|
566
|
-
if (o instanceof Map || e instanceof Map) {
|
|
567
|
-
s.push(t);
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
if (o instanceof Set || e instanceof Set) {
|
|
571
|
-
s.push(t);
|
|
572
|
-
return;
|
|
573
|
-
}
|
|
574
|
-
const i = o, d = e, a = n.get(i);
|
|
575
|
-
if (a?.has(d)) return;
|
|
576
|
-
const r = a ?? /* @__PURE__ */ new Set();
|
|
577
|
-
r.add(d), a || n.set(i, r);
|
|
578
|
-
try {
|
|
579
|
-
const f = Array.isArray(o), c = Array.isArray(e);
|
|
580
|
-
if (f !== c) {
|
|
581
|
-
s.push(t);
|
|
582
|
-
return;
|
|
583
|
-
}
|
|
584
|
-
if (f) {
|
|
585
|
-
const h = o, m = e;
|
|
586
|
-
h.length !== m.length && t && s.push(t);
|
|
587
|
-
const g = Math.min(h.length, m.length);
|
|
588
|
-
for (let w = 0; w < g; w++)
|
|
589
|
-
h[w] !== m[w] && v(h[w], m[w], t ? `${t}.${w}` : `${w}`, n, s);
|
|
590
|
-
for (let w = g; w < Math.max(h.length, m.length); w++)
|
|
591
|
-
s.push(t ? `${t}.${w}` : `${w}`);
|
|
592
|
-
return;
|
|
593
|
-
}
|
|
594
|
-
const u = Object.keys(o), l = Object.keys(e);
|
|
595
|
-
if (u.length === 0 && l.length === 0) {
|
|
596
|
-
s.push(t);
|
|
597
|
-
return;
|
|
598
|
-
}
|
|
599
|
-
let y = u.length === l.length;
|
|
600
|
-
if (y) {
|
|
601
|
-
for (let h = 0; h < l.length; h++)
|
|
602
|
-
if (!Object.prototype.hasOwnProperty.call(o, l[h])) {
|
|
603
|
-
y = !1;
|
|
604
|
-
break;
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
if (y) {
|
|
608
|
-
for (const h of l)
|
|
609
|
-
o[h] !== e[h] && (process.env.NODE_ENV !== "production" && h.includes(".") && M(t, h), v(o[h], e[h], t ? `${t}.${h}` : h, n, s));
|
|
610
|
-
return;
|
|
611
|
-
}
|
|
612
|
-
for (const h of l) {
|
|
613
|
-
const m = Object.prototype.hasOwnProperty.call(o, h);
|
|
614
|
-
if (m && o[h] === e[h]) continue;
|
|
615
|
-
process.env.NODE_ENV !== "production" && h.includes(".") && M(t, h);
|
|
616
|
-
const g = t ? `${t}.${h}` : h;
|
|
617
|
-
if (!m) {
|
|
618
|
-
s.push(g);
|
|
619
|
-
continue;
|
|
620
|
-
}
|
|
621
|
-
v(o[h], e[h], g, n, s);
|
|
622
|
-
}
|
|
623
|
-
for (const h of u)
|
|
624
|
-
Object.prototype.hasOwnProperty.call(e, h) || (process.env.NODE_ENV !== "production" && h.includes(".") && M(t, h), s.push(t ? `${t}.${h}` : h));
|
|
625
|
-
} finally {
|
|
626
|
-
r.delete(d), r.size === 0 && n.delete(i);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
function S(o, e = /* @__PURE__ */ new WeakSet(), t) {
|
|
630
|
-
if (o === null || typeof o != "object" || e.has(o) || (t !== void 0 && o === t.watch && t.onFound(), Object.isFrozen(o))) return o;
|
|
631
|
-
if (e.add(o), Array.isArray(o)) {
|
|
632
|
-
const n = o;
|
|
633
|
-
for (let s = 0; s < n.length; s++)
|
|
634
|
-
n[s] = S(n[s], e, t);
|
|
635
|
-
return Object.freeze(n);
|
|
636
|
-
}
|
|
637
|
-
for (const n of Object.getOwnPropertyNames(o)) {
|
|
638
|
-
const s = Object.getOwnPropertyDescriptor(o, n);
|
|
639
|
-
!s || !("value" in s) || (o[n] = S(o[n], e, t));
|
|
640
|
-
}
|
|
641
|
-
for (const n of Object.getOwnPropertySymbols(o)) {
|
|
642
|
-
const s = Object.getOwnPropertyDescriptor(o, n);
|
|
643
|
-
!s || !("value" in s) || (o[n] = S(o[n], e, t));
|
|
644
|
-
}
|
|
645
|
-
return Object.freeze(o);
|
|
646
|
-
}
|
|
647
|
-
function W(o, e) {
|
|
648
|
-
try {
|
|
649
|
-
return structuredClone(e);
|
|
650
|
-
} catch (t) {
|
|
651
|
-
throw new Error(
|
|
652
|
-
`[yoltra] Initial state for slice "${String(o)}" could not be copied: ${t instanceof Error ? t.message : String(t)}. State must be structured-cloneable — functions, class instances and DOM nodes are not. Keep behaviour out of state and store plain data.`
|
|
653
|
-
);
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
function k(o, e) {
|
|
657
|
-
return process.env.NODE_ENV === "production" ? o : S(o, /* @__PURE__ */ new WeakSet(), e);
|
|
658
|
-
}
|
|
659
|
-
const x = 100, z = () => typeof performance < "u" && typeof performance.now == "function" ? performance.now() : Date.now();
|
|
660
|
-
class $ {
|
|
661
|
-
/**
|
|
662
|
-
* Creates a store from a {@link StoreSpec}.
|
|
663
|
-
*
|
|
664
|
-
* @param spec - Store configuration (name, reducers, middleware, optional effects).
|
|
665
|
-
*
|
|
666
|
-
* @public
|
|
667
|
-
*/
|
|
668
|
-
constructor(e) {
|
|
669
|
-
/**
|
|
670
|
-
* Store name (used by DevTools & diagnostics).
|
|
671
|
-
*
|
|
672
|
-
* @public
|
|
673
|
-
*/
|
|
674
|
-
p(this, "name");
|
|
675
|
-
/**
|
|
676
|
-
* Registered middleware pipeline (run **before** reducers).
|
|
677
|
-
* Stores either raw functions (legacy) or MiddlewareSpec objects.
|
|
678
|
-
* Return `false` from the middleware function to stop propagation.
|
|
679
|
-
*
|
|
680
|
-
* @internal
|
|
681
|
-
*/
|
|
682
|
-
p(this, "middleware");
|
|
683
|
-
/**
|
|
684
|
-
* Installed slice reducers keyed by slice name.
|
|
685
|
-
*
|
|
686
|
-
* @internal
|
|
687
|
-
*/
|
|
688
|
-
p(this, "reducers");
|
|
689
|
-
/**
|
|
690
|
-
* Current immutable snapshot of the store state.
|
|
691
|
-
* This reference changes whenever any slice changes (shallow immutability).
|
|
692
|
-
*
|
|
693
|
-
* @internal
|
|
694
|
-
*/
|
|
695
|
-
p(this, "state");
|
|
696
|
-
/**
|
|
697
|
-
* Bus for reducer wiring (emit by `(channel, type)`).
|
|
698
|
-
*
|
|
699
|
-
* @internal
|
|
700
|
-
*/
|
|
701
|
-
p(this, "reducerBus");
|
|
702
|
-
/**
|
|
703
|
-
* Bus for **granular** connector events (emit by **dotted path** inside a slice).
|
|
704
|
-
*
|
|
705
|
-
* @internal
|
|
706
|
-
*/
|
|
707
|
-
p(this, "connectorBus");
|
|
708
|
-
/**
|
|
709
|
-
* Coarse-grained listeners (called once per committed event, only if state changed).
|
|
710
|
-
*
|
|
711
|
-
* @internal
|
|
712
|
-
*/
|
|
713
|
-
p(this, "listeners", /* @__PURE__ */ new Set());
|
|
714
|
-
/**
|
|
715
|
-
* Registered effect handlers keyed by `"channel::type"` for O(1) lookup.
|
|
716
|
-
* Used for effects with explicit `keys` targeting.
|
|
717
|
-
*
|
|
718
|
-
* @internal
|
|
719
|
-
*/
|
|
720
|
-
p(this, "effects", /* @__PURE__ */ new Map());
|
|
721
|
-
/**
|
|
722
|
-
* Pattern-based effects that need runtime matching.
|
|
723
|
-
* Used for effects with `when: { any }`, `{ channel }`, or `{ channels }`.
|
|
724
|
-
* Stores tuples of [effect function, when matcher].
|
|
725
|
-
*
|
|
726
|
-
* @internal
|
|
727
|
-
*/
|
|
728
|
-
p(this, "patternEffects", /* @__PURE__ */ new Set());
|
|
729
|
-
/**
|
|
730
|
-
* Committed event subscribers keyed by `"channel::type"` for O(1) lookup.
|
|
731
|
-
* Notified after reducers, before effects, for events that passed middleware.
|
|
732
|
-
*
|
|
733
|
-
* @internal
|
|
734
|
-
*/
|
|
735
|
-
p(this, "committedEventSubscribers", /* @__PURE__ */ new Map());
|
|
736
|
-
/**
|
|
737
|
-
* Uncommitted event subscribers keyed by `"channel::type"` for O(1) lookup.
|
|
738
|
-
* Notified when middleware rejects an event.
|
|
739
|
-
*
|
|
740
|
-
* @internal
|
|
741
|
-
*/
|
|
742
|
-
p(this, "uncommittedEventSubscribers", /* @__PURE__ */ new Map());
|
|
743
|
-
/**
|
|
744
|
-
* All-events subscribers keyed by `"channel::type"` for O(1) lookup.
|
|
745
|
-
* Notified for both committed and uncommitted events with phase parameter.
|
|
746
|
-
*
|
|
747
|
-
* @internal
|
|
748
|
-
*/
|
|
749
|
-
p(this, "allEventSubscribers", /* @__PURE__ */ new Map());
|
|
750
|
-
/**
|
|
751
|
-
* Track reducerBus unsubs per slice for HMR/register/unregister.
|
|
752
|
-
*
|
|
753
|
-
* @internal
|
|
754
|
-
*/
|
|
755
|
-
p(this, "sliceUnsubs", /* @__PURE__ */ new Map());
|
|
756
|
-
/**
|
|
757
|
-
* Pattern-based reducers that need runtime matching.
|
|
758
|
-
* Used for reducers with `when: { any }`, `{ channel }`, or `{ channels }`.
|
|
759
|
-
* Maps slice name to the `when` matcher.
|
|
760
|
-
*
|
|
761
|
-
* @internal
|
|
762
|
-
*/
|
|
763
|
-
p(this, "patternReducers", /* @__PURE__ */ new Map());
|
|
764
|
-
/**
|
|
765
|
-
* Whether `__replayEvents()` is allowed.
|
|
766
|
-
* Set from `spec.devtools.allowReplay`.
|
|
767
|
-
*
|
|
768
|
-
* @internal
|
|
769
|
-
*/
|
|
770
|
-
p(this, "replayEnabled");
|
|
771
|
-
/**
|
|
772
|
-
* Produces the `id` for each emitted event. Defaults to `crypto.randomUUID()`; overridable
|
|
773
|
-
* via {@link StoreSpec.idFactory} for runtimes lacking it or for deterministic tests.
|
|
774
|
-
*
|
|
775
|
-
* @internal
|
|
776
|
-
*/
|
|
777
|
-
p(this, "idFactory");
|
|
778
|
-
/**
|
|
779
|
-
* Optional hook invoked when an effect throws/rejects. See
|
|
780
|
-
* {@link StoreSpec.onEffectError}. `await emit()` never rejects on effect
|
|
781
|
-
* failure — this is how callers observe effect errors.
|
|
782
|
-
*/
|
|
783
|
-
p(this, "onEffectError");
|
|
784
|
-
/**
|
|
785
|
-
* Optional hook invoked when a reducer throws. See {@link StoreSpec.onReducerError}. The
|
|
786
|
-
* failing slice is isolated rather than the event being rolled back, so this is the only
|
|
787
|
-
* signal that a reducer misbehaved.
|
|
788
|
-
*/
|
|
789
|
-
p(this, "onReducerError");
|
|
790
|
-
/**
|
|
791
|
-
* `slice:channel:type` combinations already warned about for payload aliasing.
|
|
792
|
-
*
|
|
793
|
-
* @remarks
|
|
794
|
-
* Development-only diagnostics have to stay quiet enough to be read. One warning names the
|
|
795
|
-
* pattern; repeating it once per event would bury it.
|
|
796
|
-
*/
|
|
797
|
-
p(this, "warnedPayloadAliases", /* @__PURE__ */ new Set());
|
|
798
|
-
/**
|
|
799
|
-
* Pending events awaiting the **synchronous** reduce phase (middleware +
|
|
800
|
-
* reducers + subscribers + coarse listeners). Drained by {@link drainReduce}.
|
|
801
|
-
*
|
|
802
|
-
* @internal
|
|
803
|
-
*/
|
|
804
|
-
p(this, "reduceQueue", []);
|
|
805
|
-
/**
|
|
806
|
-
* Re-entrancy guard for the synchronous reduce phase.
|
|
807
|
-
*
|
|
808
|
-
* @internal
|
|
809
|
-
*/
|
|
810
|
-
p(this, "isReducing", !1);
|
|
811
|
-
/**
|
|
812
|
-
* Registered instrumentation observers (DevTools seam). See {@link instrument}.
|
|
813
|
-
*
|
|
814
|
-
* @internal
|
|
815
|
-
*/
|
|
816
|
-
p(this, "instrumentObservers", /* @__PURE__ */ new Set());
|
|
817
|
-
/**
|
|
818
|
-
* Scratch array collecting slice-prefixed changed leaf paths during an
|
|
819
|
-
* instrumented reduce. Set by {@link drainReduce} while observers are active;
|
|
820
|
-
* appended to by {@link forwardEvent}. `null` when not instrumenting.
|
|
821
|
-
*
|
|
822
|
-
* @internal
|
|
823
|
-
*/
|
|
824
|
-
p(this, "changedPathSink", null);
|
|
825
|
-
/**
|
|
826
|
-
* Count of effect tasks currently in flight; surfaced as queue depth by
|
|
827
|
-
* {@link __devtoolsIntrospect}.
|
|
828
|
-
*
|
|
829
|
-
* @internal
|
|
830
|
-
*/
|
|
831
|
-
p(this, "inFlightEffects", 0);
|
|
832
|
-
/**
|
|
833
|
-
* Tracks processed events by fingerprint with timestamps for TTL-based deduplication.
|
|
834
|
-
*
|
|
835
|
-
* **Deduplication Behavior:**
|
|
836
|
-
* - Events are fingerprinted using `channel::type::JSON(payload)`
|
|
837
|
-
* - If an identical fingerprint is seen within the dedup window, it's skipped
|
|
838
|
-
* - The window is 50ms in development, 100ms in production
|
|
839
|
-
*
|
|
840
|
-
* **Limitations:**
|
|
841
|
-
* - Non-serializable payloads (functions, symbols, circular refs) get unique
|
|
842
|
-
* fingerprints and won't be deduplicated
|
|
843
|
-
* - Legitimate rapid-fire identical events may be incorrectly deduplicated
|
|
844
|
-
* - The cache is bounded to 1000 entries with lazy pruning
|
|
845
|
-
*
|
|
846
|
-
* @internal
|
|
847
|
-
*/
|
|
848
|
-
p(this, "processedEvents", /* @__PURE__ */ new Map());
|
|
849
|
-
/**
|
|
850
|
-
* Lifetime count of events suppressed by the deduplication cache.
|
|
851
|
-
* Exposed via {@link __devtoolsIntrospect} so the DevTools agent can
|
|
852
|
-
* surface it in the STORE_METRICS response without further core changes.
|
|
853
|
-
*
|
|
854
|
-
* @internal
|
|
855
|
-
*/
|
|
856
|
-
p(this, "dedupCount", 0);
|
|
857
|
-
/**
|
|
858
|
-
* Store-owned metadata for registered effects, keyed by the effect function.
|
|
859
|
-
* Kept **off** the caller's function object: mutating a user-owned function
|
|
860
|
-
* (the old `fn.__quoMeta`) bled metadata across stores that share a handler
|
|
861
|
-
* and left it attached after unregister. Cleared on {@link dispose}.
|
|
862
|
-
*
|
|
863
|
-
* @internal
|
|
864
|
-
*/
|
|
865
|
-
p(this, "effectMeta", /* @__PURE__ */ new WeakMap());
|
|
866
|
-
/**
|
|
867
|
-
* Configuration for event deduplication.
|
|
868
|
-
* @internal
|
|
869
|
-
*/
|
|
870
|
-
p(this, "dedupConfig");
|
|
871
|
-
/**
|
|
872
|
-
* Timer for periodic cleanup of processed events.
|
|
873
|
-
*
|
|
874
|
-
* @internal
|
|
875
|
-
*/
|
|
876
|
-
p(this, "eventCleanupTimer", null);
|
|
877
|
-
if (this.name = e.name ?? "yoltra Store", this.reducerBus = new B(), this.connectorBus = new H(), this.middleware = [...e.middleware ?? []], this.reducers = {}, this.state = {}, this.replayEnabled = e.devtools?.allowReplay ?? !1, this.idFactory = e.idFactory ?? (() => crypto.randomUUID()), this.onEffectError = e.onEffectError, this.onReducerError = e.onReducerError, this.dedupConfig = {
|
|
878
|
-
windowMs: e.dedupWindowMs ?? 0,
|
|
879
|
-
maxCacheSize: 1e3
|
|
880
|
-
}, Object.entries(e.reducer).forEach(([t, n]) => {
|
|
881
|
-
this.mountSlice(t, n, { preserveState: !1 });
|
|
882
|
-
}), e.effects?.length)
|
|
883
|
-
for (const t of e.effects)
|
|
884
|
-
this.registerEffect(t);
|
|
885
|
-
this.dispose = this.dispose.bind(this), this.notifyEffects = this.notifyEffects.bind(this), this.forwardEvent = this.forwardEvent.bind(this), this.__applyExternalState = this.__applyExternalState.bind(this), this.__replayEvents = this.__replayEvents.bind(this), this.__devtoolsIntrospect = this.__devtoolsIntrospect.bind(this), this.mountSlice = this.mountSlice.bind(this), this.unmountSlice = this.unmountSlice.bind(this), this.getAtPath = this.getAtPath.bind(this), this.emit = this.emit.bind(this), this.subscribe = this.subscribe.bind(this), this.connect = this.connect.bind(this), this.onEffect = this.onEffect.bind(this), this.onEvent = this.onEvent.bind(this), this.getState = this.getState.bind(this), this.registerEffect = this.registerEffect.bind(this), this.registerMiddleware = this.registerMiddleware.bind(this), this.registerReducer = this.registerReducer.bind(this), this.replaceMiddleware = this.replaceMiddleware.bind(this), this.replaceEffects = this.replaceEffects.bind(this), this.replaceReducers = this.replaceReducers.bind(this), this.hotReplace = this.hotReplace.bind(this);
|
|
886
|
-
}
|
|
887
|
-
/**
|
|
888
|
-
* Cleanup resources (timers, etc.) when disposing the store.
|
|
889
|
-
* Call this if you're dynamically creating/destroying stores.
|
|
890
|
-
*
|
|
891
|
-
* @example
|
|
892
|
-
* ```ts
|
|
893
|
-
* const store = createStore({ ... });
|
|
894
|
-
* // later
|
|
895
|
-
* store.dispose();
|
|
896
|
-
* ```
|
|
897
|
-
*
|
|
898
|
-
* @public
|
|
899
|
-
*/
|
|
900
|
-
dispose() {
|
|
901
|
-
this.eventCleanupTimer && (clearInterval(this.eventCleanupTimer), this.eventCleanupTimer = null), this.processedEvents.clear(), this.effects.clear(), this.patternEffects.clear(), this.effectMeta = /* @__PURE__ */ new WeakMap(), this.listeners.clear(), this.committedEventSubscribers.clear(), this.uncommittedEventSubscribers.clear(), this.allEventSubscribers.clear(), this.instrumentObservers.clear(), this.connectorBus.clear(), this.reducerBus.clear(), this.patternReducers.clear(), this.sliceUnsubs.clear(), this.changedPathSink = null;
|
|
902
|
-
}
|
|
903
|
-
/**
|
|
904
|
-
* Generates a fingerprint for an event for deduplication purposes.
|
|
905
|
-
* Falls back gracefully for non-serializable payloads.
|
|
906
|
-
*
|
|
907
|
-
* @param channel - Event channel.
|
|
908
|
-
* @param type - Event type.
|
|
909
|
-
* @param payload - Event payload.
|
|
910
|
-
* @returns A string fingerprint for the event.
|
|
911
|
-
*
|
|
912
|
-
* @internal
|
|
913
|
-
*/
|
|
914
|
-
fingerprint(e, t, n) {
|
|
915
|
-
const s = `${e}::${t}`;
|
|
916
|
-
try {
|
|
917
|
-
if (n == null)
|
|
918
|
-
return `${s}::null`;
|
|
919
|
-
if (typeof n != "object")
|
|
920
|
-
return `${s}::${String(n)}`;
|
|
921
|
-
const i = JSON.stringify(n);
|
|
922
|
-
return `${s}::${i}`;
|
|
923
|
-
} catch {
|
|
924
|
-
return `${s}::${Date.now()}::${Math.random()}`;
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
/**
|
|
928
|
-
* Checks if an event should be deduplicated.
|
|
929
|
-
* Returns true if this is a duplicate that should be skipped.
|
|
930
|
-
*
|
|
931
|
-
* @param fp - Event fingerprint.
|
|
932
|
-
* @returns `true` if duplicate (should skip), `false` otherwise.
|
|
933
|
-
*
|
|
934
|
-
* @internal
|
|
935
|
-
*/
|
|
936
|
-
shouldDedupe(e, t) {
|
|
937
|
-
const n = Date.now(), s = this.processedEvents.get(e);
|
|
938
|
-
return s !== void 0 && n - s < t ? (this.dedupCount++, !0) : (this.processedEvents.set(e, n), this.ensureCleanupTimer(), this.processedEvents.size > this.dedupConfig.maxCacheSize && this.pruneProcessedEvents(n), !1);
|
|
939
|
-
}
|
|
940
|
-
/**
|
|
941
|
-
* Starts the periodic prune interval if it isn't already running. Called when
|
|
942
|
-
* the first entry is cached so the timer's lifetime tracks actual dedup use
|
|
943
|
-
* (content window or identity `dedupKey`), independent of `dedupWindowMs`.
|
|
944
|
-
*
|
|
945
|
-
* @internal
|
|
946
|
-
*/
|
|
947
|
-
ensureCleanupTimer() {
|
|
948
|
-
this.eventCleanupTimer === null && (this.eventCleanupTimer = setInterval(() => {
|
|
949
|
-
this.pruneProcessedEvents(Date.now());
|
|
950
|
-
}, 5e3), this.eventCleanupTimer.unref?.());
|
|
951
|
-
}
|
|
952
|
-
/**
|
|
953
|
-
* Removes expired entries from the processed events cache.
|
|
954
|
-
*
|
|
955
|
-
* @param now - Current timestamp.
|
|
956
|
-
*
|
|
957
|
-
* @internal
|
|
958
|
-
*/
|
|
959
|
-
pruneProcessedEvents(e) {
|
|
960
|
-
const t = Math.max(this.dedupConfig.windowMs, x), n = e - t * 2;
|
|
961
|
-
for (const [s, i] of this.processedEvents)
|
|
962
|
-
i < n && this.processedEvents.delete(s);
|
|
963
|
-
this.processedEvents.size === 0 && this.eventCleanupTimer !== null && (clearInterval(this.eventCleanupTimer), this.eventCleanupTimer = null);
|
|
964
|
-
}
|
|
965
|
-
/**
|
|
966
|
-
* Checks if an event matches a `When` matcher.
|
|
967
|
-
*
|
|
968
|
-
* @param when - The When matcher (or undefined for "all events").
|
|
969
|
-
* @param event - The event to check.
|
|
970
|
-
* @returns `true` if the event matches, `false` otherwise.
|
|
971
|
-
*
|
|
972
|
-
* @remarks
|
|
973
|
-
* - `undefined` or missing `when` matches ALL events.
|
|
974
|
-
* - `{ any: true }` matches ALL events.
|
|
975
|
-
* - `{ keys: [...] }` matches if event's `[channel, type]` is in the array.
|
|
976
|
-
* - `{ channel: 'x' }` matches if event's channel equals 'x'.
|
|
977
|
-
* - `{ channels: ['x', 'y'] }` matches if event's channel is in the array.
|
|
978
|
-
*
|
|
979
|
-
* @internal
|
|
980
|
-
*/
|
|
981
|
-
matchesWhen(e, t) {
|
|
982
|
-
return !e || "any" in e && e.any === !0 ? !0 : "keys" in e ? e.keys.some(
|
|
983
|
-
([n, s]) => t.channel === n && t.type === s
|
|
984
|
-
) : "channel" in e ? t.channel === e.channel : "channels" in e ? e.channels.includes(t.channel) : !1;
|
|
985
|
-
}
|
|
986
|
-
/**
|
|
987
|
-
* Extracts the middleware function from a MiddlewareInput.
|
|
988
|
-
* Handles both raw functions (legacy) and MiddlewareSpec objects.
|
|
989
|
-
*
|
|
990
|
-
* @param input - MiddlewareInput (function or spec).
|
|
991
|
-
* @returns The middleware function.
|
|
992
|
-
*
|
|
993
|
-
* @internal
|
|
994
|
-
*/
|
|
995
|
-
getMiddlewareFunction(e) {
|
|
996
|
-
return typeof e == "function" ? e : e.middleware;
|
|
997
|
-
}
|
|
998
|
-
/**
|
|
999
|
-
* Gets the `when` matcher from a MiddlewareInput.
|
|
1000
|
-
*
|
|
1001
|
-
* @param input - MiddlewareInput (function or spec).
|
|
1002
|
-
* @returns The `when` matcher, or `undefined` for raw functions (match all).
|
|
1003
|
-
*
|
|
1004
|
-
* @internal
|
|
1005
|
-
*/
|
|
1006
|
-
getMiddlewareWhen(e) {
|
|
1007
|
-
if (typeof e != "function")
|
|
1008
|
-
return e.when;
|
|
1009
|
-
}
|
|
1010
|
-
/**
|
|
1011
|
-
* Invokes all registered **effects** for a given event.
|
|
1012
|
-
* Handles both key-based effects (O(1) lookup) and pattern-based effects (runtime matching).
|
|
1013
|
-
* Errors are caught and logged.
|
|
1014
|
-
*
|
|
1015
|
-
* @param event - The event that was reduced.
|
|
1016
|
-
* @internal
|
|
1017
|
-
*/
|
|
1018
|
-
async notifyEffects(e) {
|
|
1019
|
-
const t = `${String(e.channel)}::${String(e.type)}`, n = this.effects.get(t);
|
|
1020
|
-
if (n && n.size > 0)
|
|
1021
|
-
for (const s of [...n])
|
|
1022
|
-
try {
|
|
1023
|
-
await s(e, this.getState, this.emit);
|
|
1024
|
-
} catch (i) {
|
|
1025
|
-
console.error("Effect error:", i), this.onEffectError?.(i, e);
|
|
1026
|
-
}
|
|
1027
|
-
for (const { effect: s, when: i } of this.patternEffects)
|
|
1028
|
-
if (this.matchesWhen(i, e))
|
|
1029
|
-
try {
|
|
1030
|
-
await s(e, this.getState, this.emit);
|
|
1031
|
-
} catch (d) {
|
|
1032
|
-
console.error("Effect error:", d), this.onEffectError?.(d, e);
|
|
1033
|
-
}
|
|
1034
|
-
}
|
|
1035
|
-
/**
|
|
1036
|
-
* Notifies event subscribers for a specific phase.
|
|
1037
|
-
*
|
|
1038
|
-
* Calls both phase-specific subscribers and 'all' subscribers.
|
|
1039
|
-
* Errors are caught and logged, allowing other subscribers to continue.
|
|
1040
|
-
*
|
|
1041
|
-
* @param event - The event to notify about.
|
|
1042
|
-
* @param phase - The phase ('committed' or 'uncommitted').
|
|
1043
|
-
* @internal
|
|
1044
|
-
*/
|
|
1045
|
-
notifyEventSubscribers(e, t) {
|
|
1046
|
-
const n = `${String(e.channel)}::${String(e.type)}`, i = (t === "committed" ? this.committedEventSubscribers : this.uncommittedEventSubscribers).get(n);
|
|
1047
|
-
if (i?.size)
|
|
1048
|
-
for (const a of [...i]) this.invokeEventSubscriber(a, e, t);
|
|
1049
|
-
const d = this.allEventSubscribers.get(n);
|
|
1050
|
-
if (d?.size)
|
|
1051
|
-
for (const a of [...d]) this.invokeEventSubscriber(a, e, t);
|
|
1052
|
-
}
|
|
1053
|
-
/**
|
|
1054
|
-
* Invokes a single event-subscription handler **fire-and-forget**: synchronous
|
|
1055
|
-
* throws and async rejections are logged but never block the emit pipeline.
|
|
1056
|
-
* Event subscribers are notifications, not part of the committed reduce result.
|
|
1057
|
-
*
|
|
1058
|
-
* @internal
|
|
1059
|
-
*/
|
|
1060
|
-
invokeEventSubscriber(e, t, n) {
|
|
1061
|
-
try {
|
|
1062
|
-
const s = e(t, this.getState, this.emit, n);
|
|
1063
|
-
s && typeof s.then == "function" && s.catch((i) => console.error("Event subscription error:", i));
|
|
1064
|
-
} catch (s) {
|
|
1065
|
-
console.error("Event subscription error:", s);
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
/**
|
|
1069
|
-
* Applies a reduced event to a slice and emits **precise** connector events.
|
|
1070
|
-
*
|
|
1071
|
-
* For each changed **leaf path** (via {@link detectChangedProps}), emits that leaf and
|
|
1072
|
-
* all of its **ancestors** once (e.g., `"data"`, `"data.123"`, `"data.123.title"`).
|
|
1073
|
-
*
|
|
1074
|
-
* **State Immutability**: When a slice changes, a new state object is created via
|
|
1075
|
-
* shallow spread: `{ ...this.state, [sliceName]: newSlice }`. This ensures that
|
|
1076
|
-
* `this.state` reference changes, enabling efficient change detection via `===`.
|
|
1077
|
-
*
|
|
1078
|
-
* @param rName - Slice name being updated.
|
|
1079
|
-
* @param event - Reduced event with typed payload.
|
|
1080
|
-
* @returns `true` if the slice actually changed, `false` otherwise.
|
|
1081
|
-
*
|
|
1082
|
-
* @internal
|
|
1083
|
-
*/
|
|
1084
|
-
/**
|
|
1085
|
-
* Reduces one slice and contains any error it raises.
|
|
1086
|
-
*
|
|
1087
|
-
* @returns `true` when the slice changed.
|
|
1088
|
-
*
|
|
1089
|
-
* @remarks
|
|
1090
|
-
* The single funnel both dispatch paths go through, which is the point. Keyed reducers run
|
|
1091
|
-
* through `reducerBus`, whose handler loop caught and logged; pattern reducers were called
|
|
1092
|
-
* straight from the drain, so their errors escaped to the caller instead. The same bug in the
|
|
1093
|
-
* same reducer therefore produced two different outcomes depending on how the slice happened
|
|
1094
|
-
* to be targeted — a keyed reducer's throw let the event commit and its effects run, while a
|
|
1095
|
-
* pattern reducer's throw aborted the commit and notified nobody, not even the uncommitted
|
|
1096
|
-
* subscribers a veto would have reached.
|
|
1097
|
-
*
|
|
1098
|
-
* The semantics are now the same either way: **the failing slice is isolated.** Its state is
|
|
1099
|
-
* unchanged, every other slice still reduces, and the event still commits if anything else
|
|
1100
|
-
* changed. Rolling the whole event back would be tidier in principle, but fine-grained
|
|
1101
|
-
* subscribers are notified inside `forwardEvent` as each slice commits, so an event that
|
|
1102
|
-
* reverted afterwards would have already told components about a value that no longer exists.
|
|
1103
|
-
* Isolation keeps every notification truthful.
|
|
1104
|
-
*
|
|
1105
|
-
* @internal
|
|
1106
|
-
*/
|
|
1107
|
-
forwardEventGuarded(e, t) {
|
|
1108
|
-
try {
|
|
1109
|
-
return this.forwardEvent(e, t);
|
|
1110
|
-
} catch (n) {
|
|
1111
|
-
return console.error(`Reducer error in slice "${e}":`, n), this.onReducerError?.(n, t, e), !1;
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1114
|
-
forwardEvent(e, t) {
|
|
1115
|
-
const n = this.state[e], s = this.reducers[e].reduce(n, t);
|
|
1116
|
-
if (n === s) return !1;
|
|
1117
|
-
const i = O(n, s).filter(Boolean);
|
|
1118
|
-
if (i.length === 0) return !1;
|
|
1119
|
-
const d = t.payload, a = process.env.NODE_ENV !== "production" && d !== null && typeof d == "object" ? {
|
|
1120
|
-
watch: d,
|
|
1121
|
-
onFound: () => {
|
|
1122
|
-
const c = `${e}:${t.channel}:${t.type}`;
|
|
1123
|
-
this.warnedPayloadAliases.has(c) || (this.warnedPayloadAliases.add(c), console.warn(
|
|
1124
|
-
`[yoltra] Slice "${e}" stored the payload of "${t.channel}/${t.type}" by reference. It is now frozen along with the rest of the state, so the emitter mutating it later will throw in development and silently corrupt state in production. Copy the payload in the reducer instead.`
|
|
1125
|
-
));
|
|
1126
|
-
}
|
|
1127
|
-
} : void 0, r = k(s, a);
|
|
1128
|
-
if (this.state = { ...this.state, [e]: r }, this.changedPathSink)
|
|
1129
|
-
for (const c of i)
|
|
1130
|
-
this.changedPathSink.push(c ? `${e}.${c}` : e);
|
|
1131
|
-
const f = /* @__PURE__ */ new Set();
|
|
1132
|
-
for (const c of i)
|
|
1133
|
-
for (const u of $.buildAncestorPaths(c)) f.add(u);
|
|
1134
|
-
for (const c of f)
|
|
1135
|
-
this.connectorBus.emitWith(e, c, () => ({
|
|
1136
|
-
oldValue: this.getAtPath(n, c),
|
|
1137
|
-
newValue: this.getAtPath(r, c),
|
|
1138
|
-
path: c
|
|
1139
|
-
}));
|
|
1140
|
-
return !0;
|
|
1141
|
-
}
|
|
1142
|
-
/**
|
|
1143
|
-
* Returns a structured introspection snapshot for DevTools UIs.
|
|
1144
|
-
*
|
|
1145
|
-
* @remarks
|
|
1146
|
-
* Reads the internal middleware, effects, reducers, and subscriber
|
|
1147
|
-
* registries and returns a plain-object summary matching the
|
|
1148
|
-
* `STORE_SUBSCRIPTIONS` protocol message shape.
|
|
1149
|
-
*
|
|
1150
|
-
* @public
|
|
1151
|
-
*/
|
|
1152
|
-
__devtoolsIntrospect() {
|
|
1153
|
-
const e = Object.keys(this.reducers).map((a) => {
|
|
1154
|
-
const r = this.patternReducers.get(a);
|
|
1155
|
-
return { name: a, when: r };
|
|
1156
|
-
}), t = [];
|
|
1157
|
-
for (const [a, r] of this.effects) {
|
|
1158
|
-
if (r.size === 0) continue;
|
|
1159
|
-
const [f, c] = a.split("::");
|
|
1160
|
-
for (const u of r) {
|
|
1161
|
-
const l = this.effectMeta.get(u);
|
|
1162
|
-
t.push({ channel: f, type: c, name: l?.name, description: l?.description });
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
1165
|
-
for (const a of this.patternEffects) {
|
|
1166
|
-
const r = this.effectMeta.get(a.effect);
|
|
1167
|
-
t.push({
|
|
1168
|
-
channel: "*",
|
|
1169
|
-
type: "*",
|
|
1170
|
-
name: r?.name,
|
|
1171
|
-
description: r?.description
|
|
1172
|
-
});
|
|
1173
|
-
}
|
|
1174
|
-
const n = [];
|
|
1175
|
-
for (const a of this.middleware)
|
|
1176
|
-
typeof a == "function" ? n.push({ name: a.name || void 0 }) : n.push({
|
|
1177
|
-
name: a.meta?.name,
|
|
1178
|
-
description: a.meta?.description,
|
|
1179
|
-
when: a.when
|
|
1180
|
-
});
|
|
1181
|
-
const s = [];
|
|
1182
|
-
for (const a of this.connectorBus.__introspect())
|
|
1183
|
-
for (let r = 0; r < a.count; r++)
|
|
1184
|
-
s.push({ reducer: a.channel, property: a.type });
|
|
1185
|
-
const i = [];
|
|
1186
|
-
for (const [a, r] of this.committedEventSubscribers) {
|
|
1187
|
-
if (r.size === 0) continue;
|
|
1188
|
-
const [f, c] = a.split("::");
|
|
1189
|
-
for (let u = 0; u < r.size; u++)
|
|
1190
|
-
i.push({ channel: f, type: c, phase: "committed" });
|
|
1191
|
-
}
|
|
1192
|
-
for (const [a, r] of this.uncommittedEventSubscribers) {
|
|
1193
|
-
if (r.size === 0) continue;
|
|
1194
|
-
const [f, c] = a.split("::");
|
|
1195
|
-
for (let u = 0; u < r.size; u++)
|
|
1196
|
-
i.push({ channel: f, type: c, phase: "uncommitted" });
|
|
1197
|
-
}
|
|
1198
|
-
for (const [a, r] of this.allEventSubscribers) {
|
|
1199
|
-
if (r.size === 0) continue;
|
|
1200
|
-
const [f, c] = a.split("::");
|
|
1201
|
-
for (let u = 0; u < r.size; u++)
|
|
1202
|
-
i.push({ channel: f, type: c, phase: "all" });
|
|
1203
|
-
}
|
|
1204
|
-
const d = this.listeners.size;
|
|
1205
|
-
return {
|
|
1206
|
-
reducers: e,
|
|
1207
|
-
effects: t,
|
|
1208
|
-
middleware: n,
|
|
1209
|
-
atomic: s,
|
|
1210
|
-
event: i,
|
|
1211
|
-
coarse: d,
|
|
1212
|
-
dedupHits: this.dedupCount,
|
|
1213
|
-
queueDepth: this.reduceQueue.length + this.inFlightEffects
|
|
1214
|
-
};
|
|
1215
|
-
}
|
|
1216
|
-
/**
|
|
1217
|
-
* Applies an externally provided **whole-state** (e.g., DevTools time travel) and emits
|
|
1218
|
-
* fine-grained path changes for each slice.
|
|
1219
|
-
*
|
|
1220
|
-
* **State Immutability**: If any slices change, a new state object is created via
|
|
1221
|
-
* shallow spread. This ensures consistent immutability with {@link forwardEvent}.
|
|
1222
|
-
*
|
|
1223
|
-
* **Missing slices**: the snapshot should contain every slice. A slice absent
|
|
1224
|
-
* from `nextPlain` is **retained at its current value** (not blanked to
|
|
1225
|
-
* `undefined`, which would make `getState().<slice>` throw on next access).
|
|
1226
|
-
*
|
|
1227
|
-
* @param nextPlain - Plain JS object to become the new state.
|
|
1228
|
-
*
|
|
1229
|
-
* @internal
|
|
1230
|
-
*/
|
|
1231
|
-
__applyExternalState(e) {
|
|
1232
|
-
if (!this.replayEnabled)
|
|
1233
|
-
throw new Error(
|
|
1234
|
-
"[yoltra] External state apply (time-travel) is disabled. Enable it with createStore({ devtools: { allowReplay: true } })"
|
|
1235
|
-
);
|
|
1236
|
-
const t = this.state, n = e, s = { ...this.state };
|
|
1237
|
-
let i = !1;
|
|
1238
|
-
Object.keys(this.reducers).forEach((d) => {
|
|
1239
|
-
const a = t?.[d], r = n?.[d];
|
|
1240
|
-
if (r === void 0) {
|
|
1241
|
-
process.env.NODE_ENV !== "production" && console.warn(
|
|
1242
|
-
`[yoltra] External state is missing slice "${String(
|
|
1243
|
-
d
|
|
1244
|
-
)}"; retaining its current value. Time-travel snapshots should contain all slices.`
|
|
1245
|
-
);
|
|
1246
|
-
return;
|
|
1247
|
-
}
|
|
1248
|
-
if (a === r) return;
|
|
1249
|
-
const f = k(r);
|
|
1250
|
-
s[d] = f, i = !0;
|
|
1251
|
-
const c = O(a, r).filter(Boolean);
|
|
1252
|
-
if (c.length === 0) return;
|
|
1253
|
-
const u = /* @__PURE__ */ new Set();
|
|
1254
|
-
for (const l of c) for (const y of $.buildAncestorPaths(l)) u.add(y);
|
|
1255
|
-
for (const l of u) {
|
|
1256
|
-
const y = this.getAtPath(a, l), h = this.getAtPath(f, l);
|
|
1257
|
-
this.connectorBus.emit(d, l, { oldValue: y, newValue: h, path: l });
|
|
1258
|
-
}
|
|
1259
|
-
}), i && (this.state = s), i && this.listeners.forEach((d) => d());
|
|
1260
|
-
}
|
|
1261
|
-
/**
|
|
1262
|
-
* Replays a sequence of events from a snapshot through reducers and event
|
|
1263
|
-
* subscribers ONLY. Skips dedup, middleware, and effects.
|
|
1264
|
-
*
|
|
1265
|
-
* This method is gated by the `devtools.allowReplay` runtime config.
|
|
1266
|
-
* If replay is not enabled, this method throws.
|
|
1267
|
-
*
|
|
1268
|
-
* @param snapshot - The state snapshot to restore before replaying.
|
|
1269
|
-
* @param events - Array of events to replay (in order).
|
|
1270
|
-
*
|
|
1271
|
-
* @internal
|
|
1272
|
-
*/
|
|
1273
|
-
__replayEvents(e, t) {
|
|
1274
|
-
if (!this.replayEnabled)
|
|
1275
|
-
throw new Error(
|
|
1276
|
-
"[yoltra] Event replay is disabled. Enable it with createStore({ devtools: { allowReplay: true } })"
|
|
1277
|
-
);
|
|
1278
|
-
this.__applyExternalState(e);
|
|
1279
|
-
for (const n of t) {
|
|
1280
|
-
const s = n, i = this.state;
|
|
1281
|
-
this.reducerBus.emit(s.channel, s.type, s.payload, s);
|
|
1282
|
-
for (const [r, f] of this.patternReducers)
|
|
1283
|
-
this.matchesWhen(f, s) && this.forwardEventGuarded(r, s);
|
|
1284
|
-
const d = this.state, a = i !== d;
|
|
1285
|
-
this.notifyEventSubscribers(s, "committed"), a && this.listeners.forEach((r) => r());
|
|
1286
|
-
}
|
|
1287
|
-
}
|
|
1288
|
-
/**
|
|
1289
|
-
* Emits a typed event `(channel, type, payload)`.
|
|
1290
|
-
* Events are queued and processed **sequentially** (FIFO).
|
|
1291
|
-
*
|
|
1292
|
-
* **Pipeline per event:** the *reduce phase* (steps 1-4) runs **synchronously**,
|
|
1293
|
-
* so `getState()` reflects the change as soon as `emit()` returns; the *effect
|
|
1294
|
-
* phase* (step 5) runs afterwards, asynchronously.
|
|
1295
|
-
* 1. **Deduplication** (opt-in) - Skip when content-dedup is enabled (`dedupWindowMs > 0`) or a matching `dedupKey` recurs; off by default
|
|
1296
|
-
* 2. **Middleware** (sync) - Pre-reducer hooks; may cancel by returning `false`
|
|
1297
|
-
* 3. **Reducers** (sync) - state updates + fine-grained path notifications
|
|
1298
|
-
* 4. **Subscribers + coarse** (sync) - event subscribers (fire-and-forget) then coarse listeners (only if state changed)
|
|
1299
|
-
* 5. **Effects** (async) - side-effects keyed by `(channel, type)`; the returned promise resolves once they complete
|
|
1300
|
-
*
|
|
1301
|
-
* **Change Detection**: Uses reference equality (`===`) on `this.state` to determine
|
|
1302
|
-
* if any slice changed. Works because {@link forwardEvent} creates a new state reference
|
|
1303
|
-
* via shallow spread when any slice changes.
|
|
1304
|
-
*
|
|
1305
|
-
* @typeParam C - Channel key in `EM`.
|
|
1306
|
-
* @typeParam T - Type key within channel `C`.
|
|
1307
|
-
* @param channel - Channel name.
|
|
1308
|
-
* @param type - Event type name.
|
|
1309
|
-
* @param payload - Payload typed as `EM[C][T]`.
|
|
1310
|
-
* @param opts - Optional per-emit options (e.g. `dedupKey` for identity-based dedup).
|
|
1311
|
-
* @returns A promise that resolves once this event's effects have finished.
|
|
1312
|
-
* State is already updated synchronously before `emit()` returns.
|
|
1313
|
-
*
|
|
1314
|
-
* @example Basic usage
|
|
1315
|
-
* ```ts
|
|
1316
|
-
* await store.emit('ui', 'increment', 1);
|
|
1317
|
-
* ```
|
|
1318
|
-
*
|
|
1319
|
-
* @example With middleware cancellation
|
|
1320
|
-
* ```ts
|
|
1321
|
-
* store.registerMiddleware((state, event) => {
|
|
1322
|
-
* if (event.type === 'dangerous') return false; // cancel
|
|
1323
|
-
* return true; // allow
|
|
1324
|
-
* });
|
|
1325
|
-
*
|
|
1326
|
-
* await store.emit('ui', 'dangerous', null); // cancelled, no state change
|
|
1327
|
-
* ```
|
|
1328
|
-
*
|
|
1329
|
-
* @public
|
|
1330
|
-
*/
|
|
1331
|
-
async emit(e, t, n, s) {
|
|
1332
|
-
const i = s?.dedupKey, d = this.dedupConfig.windowMs;
|
|
1333
|
-
if (s?.skipDedup !== !0 && (d > 0 || i !== void 0)) {
|
|
1334
|
-
const c = i !== void 0 && d <= 0 ? x : d, u = i !== void 0 ? `${e}::${t}::#${i}` : this.fingerprint(e, t, n);
|
|
1335
|
-
if (this.shouldDedupe(u, c))
|
|
1336
|
-
return;
|
|
1337
|
-
}
|
|
1338
|
-
const a = s?.id ?? this.idFactory();
|
|
1339
|
-
let r;
|
|
1340
|
-
const f = new Promise((c) => {
|
|
1341
|
-
r = c;
|
|
1342
|
-
});
|
|
1343
|
-
return this.reduceQueue.push({
|
|
1344
|
-
channel: e,
|
|
1345
|
-
type: t,
|
|
1346
|
-
payload: n,
|
|
1347
|
-
id: a,
|
|
1348
|
-
meta: s?.meta,
|
|
1349
|
-
resolve: r
|
|
1350
|
-
}), this.drainReduce(), f;
|
|
1351
|
-
}
|
|
1352
|
-
/**
|
|
1353
|
-
* Drains the reduce queue **synchronously**. For each event it runs middleware,
|
|
1354
|
-
* reducers, event subscribers, and coarse listeners in the same tick, so
|
|
1355
|
-
* `getState()` reflects the change the moment {@link emit} returns. Re-entrant
|
|
1356
|
-
* emits (from middleware or subscribers) are appended and drained in the same
|
|
1357
|
-
* pass — preserving FIFO order without interleaving reducers. Each committed
|
|
1358
|
-
* event's effects then run in an independent task (see {@link runEventEffects}).
|
|
1359
|
-
*
|
|
1360
|
-
* @internal
|
|
1361
|
-
*/
|
|
1362
|
-
drainReduce() {
|
|
1363
|
-
if (!this.isReducing) {
|
|
1364
|
-
this.isReducing = !0;
|
|
1365
|
-
try {
|
|
1366
|
-
for (; this.reduceQueue.length > 0; ) {
|
|
1367
|
-
const { channel: e, type: t, payload: n, id: s, meta: i, resolve: d } = this.reduceQueue.shift(), a = {
|
|
1368
|
-
channel: e,
|
|
1369
|
-
type: t,
|
|
1370
|
-
payload: n,
|
|
1371
|
-
id: s,
|
|
1372
|
-
...i !== void 0 ? { meta: i } : {}
|
|
1373
|
-
}, r = this.instrumentObservers.size > 0, f = r ? this.state : void 0, c = r ? [] : void 0;
|
|
1374
|
-
c !== void 0 && (this.changedPathSink = c);
|
|
1375
|
-
const u = r ? z() : 0;
|
|
1376
|
-
let l = !1;
|
|
1377
|
-
try {
|
|
1378
|
-
l = this.applyEventSync(a);
|
|
1379
|
-
} catch (y) {
|
|
1380
|
-
console.error("Emit reduce error:", y);
|
|
1381
|
-
} finally {
|
|
1382
|
-
r && (this.changedPathSink = null);
|
|
1383
|
-
}
|
|
1384
|
-
r && this.emitInstrumentation(
|
|
1385
|
-
a,
|
|
1386
|
-
l,
|
|
1387
|
-
c ?? [],
|
|
1388
|
-
f,
|
|
1389
|
-
z() - u
|
|
1390
|
-
), this.runEventEffects(a, l, d);
|
|
1391
|
-
}
|
|
1392
|
-
} finally {
|
|
1393
|
-
this.isReducing = !1;
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
/**
|
|
1398
|
-
* Runs the **synchronous** part of the pipeline for a single event: middleware
|
|
1399
|
-
* (may veto), key- and pattern-based reducers, committed/uncommitted event
|
|
1400
|
-
* subscribers (fire-and-forget), and coarse listeners.
|
|
1401
|
-
*
|
|
1402
|
-
* @returns `true` if the event was committed (passed middleware), `false` if a
|
|
1403
|
-
* middleware vetoed it.
|
|
1404
|
-
*
|
|
1405
|
-
* @internal
|
|
1406
|
-
*/
|
|
1407
|
-
applyEventSync(e) {
|
|
1408
|
-
for (const s of this.middleware) {
|
|
1409
|
-
const i = this.getMiddlewareWhen(s);
|
|
1410
|
-
if (!this.matchesWhen(i, e)) continue;
|
|
1411
|
-
const d = this.getMiddlewareFunction(s);
|
|
1412
|
-
let a;
|
|
1413
|
-
try {
|
|
1414
|
-
a = d(this.state, e, this.emit), process.env.NODE_ENV !== "production" && typeof a?.then == "function" && console.error(
|
|
1415
|
-
`[yoltra] Middleware for "${e.channel}/${e.type}" returned a Promise. Middleware is synchronous: a Promise is truthy, so this event was allowed without waiting and a "return false" inside it can never veto. Do the check synchronously, and put anything that must await in an effect.`
|
|
1416
|
-
);
|
|
1417
|
-
} catch (r) {
|
|
1418
|
-
console.error("Middleware error:", r), a = !1;
|
|
1419
|
-
}
|
|
1420
|
-
if (!a)
|
|
1421
|
-
return this.notifyEventSubscribers(e, "uncommitted"), !1;
|
|
1422
|
-
}
|
|
1423
|
-
const t = this.state;
|
|
1424
|
-
this.reducerBus.emit(
|
|
1425
|
-
e.channel,
|
|
1426
|
-
e.type,
|
|
1427
|
-
e.payload,
|
|
1428
|
-
e
|
|
1429
|
-
);
|
|
1430
|
-
for (const [s, i] of this.patternReducers)
|
|
1431
|
-
this.matchesWhen(i, e) && this.forwardEventGuarded(s, e);
|
|
1432
|
-
const n = t !== this.state;
|
|
1433
|
-
return this.notifyEventSubscribers(e, "committed"), n && this.listeners.forEach((s) => s()), !0;
|
|
1434
|
-
}
|
|
1435
|
-
/**
|
|
1436
|
-
* Runs a single committed event's effects as an **independent async task**,
|
|
1437
|
-
* then resolves that event's completion deferred so `await emit(...)` settles
|
|
1438
|
-
* once its effects finish. Per-event tasks (rather than one shared serialized
|
|
1439
|
-
* loop) let an effect `await` a re-entrant emit without deadlocking.
|
|
1440
|
-
*
|
|
1441
|
-
* @internal
|
|
1442
|
-
*/
|
|
1443
|
-
async runEventEffects(e, t, n) {
|
|
1444
|
-
this.inFlightEffects++;
|
|
1445
|
-
try {
|
|
1446
|
-
t && await this.notifyEffects(e);
|
|
1447
|
-
} catch (s) {
|
|
1448
|
-
console.error("Effect error:", s);
|
|
1449
|
-
} finally {
|
|
1450
|
-
this.inFlightEffects--, n();
|
|
1451
|
-
}
|
|
1452
|
-
}
|
|
1453
|
-
/**
|
|
1454
|
-
* Registers an instrumentation observer. See {@link StoreInstance.instrument}.
|
|
1455
|
-
*
|
|
1456
|
-
* @public
|
|
1457
|
-
*/
|
|
1458
|
-
instrument(e) {
|
|
1459
|
-
return this.instrumentObservers.add(e), () => {
|
|
1460
|
-
this.instrumentObservers.delete(e);
|
|
1461
|
-
};
|
|
1462
|
-
}
|
|
1463
|
-
/**
|
|
1464
|
-
* Builds an {@link InstrumentedEvent} from the reduce result and notifies
|
|
1465
|
-
* observers. `changedPaths` are the exact slice-prefixed leaf paths recorded
|
|
1466
|
-
* by {@link forwardEvent} during this reduce, so DevTools patches need no
|
|
1467
|
-
* re-diff.
|
|
1468
|
-
*
|
|
1469
|
-
* @internal
|
|
1470
|
-
*/
|
|
1471
|
-
emitInstrumentation(e, t, n, s, i) {
|
|
1472
|
-
const d = {}, a = {};
|
|
1473
|
-
for (const f of n)
|
|
1474
|
-
d[f] = this.getAtPath(s, f), a[f] = this.getAtPath(this.state, f);
|
|
1475
|
-
const r = {
|
|
1476
|
-
event: {
|
|
1477
|
-
id: e.id,
|
|
1478
|
-
channel: e.channel,
|
|
1479
|
-
type: e.type,
|
|
1480
|
-
payload: e.payload,
|
|
1481
|
-
// Conditional, so an event without metadata produces an observer payload
|
|
1482
|
-
// byte-identical to the pre-`meta` shape.
|
|
1483
|
-
...e.meta !== void 0 ? { meta: e.meta } : {}
|
|
1484
|
-
},
|
|
1485
|
-
committed: t,
|
|
1486
|
-
changedPaths: n,
|
|
1487
|
-
prevValues: d,
|
|
1488
|
-
nextValues: a,
|
|
1489
|
-
reduceTimeMs: i
|
|
1490
|
-
};
|
|
1491
|
-
for (const f of [...this.instrumentObservers])
|
|
1492
|
-
try {
|
|
1493
|
-
f(r);
|
|
1494
|
-
} catch (c) {
|
|
1495
|
-
console.error("Instrumentation observer error:", c);
|
|
1496
|
-
}
|
|
1497
|
-
}
|
|
1498
|
-
/**
|
|
1499
|
-
* Connects a **fine-grained** listener to a dotted path under a slice.
|
|
1500
|
-
*
|
|
1501
|
-
* @param spec - `{ reducer, property }` where `property` is a dotted path (e.g., `"items.0.title"`).
|
|
1502
|
-
* Supports wildcards: `*` (one segment) and `**` (zero or more segments).
|
|
1503
|
-
* @param h - Handler receiving a {@link Change} with `{ oldValue, newValue, path }`.
|
|
1504
|
-
* @returns Unsubscribe function.
|
|
1505
|
-
*
|
|
1506
|
-
* @example Exact path
|
|
1507
|
-
* ```ts
|
|
1508
|
-
* const off = store.connect(
|
|
1509
|
-
* { reducer: 'todos', property: 'items.0.title' },
|
|
1510
|
-
* (chg) => console.log('title changed:', chg.newValue)
|
|
1511
|
-
* );
|
|
1512
|
-
* off();
|
|
1513
|
-
* ```
|
|
1514
|
-
*
|
|
1515
|
-
* @example Wildcard pattern
|
|
1516
|
-
* ```ts
|
|
1517
|
-
* // Listen to any item title change
|
|
1518
|
-
* const off = store.connect(
|
|
1519
|
-
* { reducer: 'todos', property: 'items.*.title' },
|
|
1520
|
-
* (chg) => console.log('some title changed')
|
|
1521
|
-
* );
|
|
1522
|
-
* ```
|
|
1523
|
-
*
|
|
1524
|
-
* @public
|
|
1525
|
-
*/
|
|
1526
|
-
connect(e, t) {
|
|
1527
|
-
return this.connectorBus.on(e.reducer, e.property, t);
|
|
1528
|
-
}
|
|
1529
|
-
/**
|
|
1530
|
-
* Subscribe to events by channel and type.
|
|
1531
|
-
*
|
|
1532
|
-
* Event subscriptions are intended for the View layer (e.g., React components)
|
|
1533
|
-
* to react to events without affecting the event flow. They are fire-and-forget
|
|
1534
|
-
* and cannot cancel event propagation.
|
|
1535
|
-
*
|
|
1536
|
-
* **Phases:**
|
|
1537
|
-
* - `'committed'` (default): Events that passed middleware and reached reducers.
|
|
1538
|
-
* Notified after reducers, before effects.
|
|
1539
|
-
* - `'uncommitted'`: Events rejected by middleware. Notified immediately after rejection.
|
|
1540
|
-
* - `'all'`: Both committed and uncommitted events. Handler receives the phase parameter
|
|
1541
|
-
* to distinguish between the two.
|
|
1542
|
-
*
|
|
1543
|
-
* @typeParam C - Channel key within `EM`.
|
|
1544
|
-
* @typeParam T - Event type key within channel `C`.
|
|
1545
|
-
* @param channel - Channel to subscribe to.
|
|
1546
|
-
* @param type - Event type to subscribe to.
|
|
1547
|
-
* @param handler - Handler function `(event, getState, emit, phase)`.
|
|
1548
|
-
* @param phase - Event phase to subscribe to (default: `'committed'`).
|
|
1549
|
-
* @returns Unsubscribe function.
|
|
1550
|
-
*
|
|
1551
|
-
* @example Committed events (default)
|
|
1552
|
-
* ```ts
|
|
1553
|
-
* const off = store.onEvent('ui', 'save', (event, getState, emit, phase) => {
|
|
1554
|
-
* console.log('Save committed:', event.payload);
|
|
1555
|
-
* });
|
|
1556
|
-
* off();
|
|
1557
|
-
* ```
|
|
1558
|
-
*
|
|
1559
|
-
* @example Uncommitted (rejected) events
|
|
1560
|
-
* ```ts
|
|
1561
|
-
* store.onEvent('ui', 'delete', (event, getState, emit, phase) => {
|
|
1562
|
-
* console.log('Delete was rejected by middleware');
|
|
1563
|
-
* }, 'uncommitted');
|
|
1564
|
-
* ```
|
|
1565
|
-
*
|
|
1566
|
-
* @example All events
|
|
1567
|
-
* ```ts
|
|
1568
|
-
* store.onEvent('ui', 'action', (event, getState, emit, phase) => {
|
|
1569
|
-
* console.log('Action:', phase); // 'committed' or 'uncommitted'
|
|
1570
|
-
* }, 'all');
|
|
1571
|
-
* ```
|
|
1572
|
-
*
|
|
1573
|
-
* @public
|
|
1574
|
-
*/
|
|
1575
|
-
onEvent(e, t, n, s = "committed") {
|
|
1576
|
-
const i = `${e}::${String(t)}`, d = s === "committed" ? this.committedEventSubscribers : s === "uncommitted" ? this.uncommittedEventSubscribers : this.allEventSubscribers;
|
|
1577
|
-
return d.has(i) || d.set(i, /* @__PURE__ */ new Set()), d.get(i).add(n), () => {
|
|
1578
|
-
const a = d.get(i);
|
|
1579
|
-
a && (a.delete(n), a.size === 0 && d.delete(i));
|
|
1580
|
-
};
|
|
1581
|
-
}
|
|
1582
|
-
/**
|
|
1583
|
-
* Subscribes to **coarse-grained** commits (called once per successful event, only if state changed).
|
|
1584
|
-
*
|
|
1585
|
-
* **Use Case**: React's `useSyncExternalStore` or similar external store integrations.
|
|
1586
|
-
*
|
|
1587
|
-
* @param fn - Listener invoked after reducers/effects have run and state has changed.
|
|
1588
|
-
* @returns Unsubscribe function.
|
|
1589
|
-
*
|
|
1590
|
-
* @example
|
|
1591
|
-
* ```ts
|
|
1592
|
-
* const off = store.subscribe(() => console.log('state committed'));
|
|
1593
|
-
* // Later:
|
|
1594
|
-
* off();
|
|
1595
|
-
* ```
|
|
1596
|
-
*
|
|
1597
|
-
* @public
|
|
1598
|
-
*/
|
|
1599
|
-
subscribe(e) {
|
|
1600
|
-
return this.listeners.add(e), () => this.listeners.delete(e);
|
|
1601
|
-
}
|
|
1602
|
-
/**
|
|
1603
|
-
* Returns the current immutable state snapshot.
|
|
1604
|
-
*
|
|
1605
|
-
* @returns Deep-readonly state object.
|
|
1606
|
-
*
|
|
1607
|
-
* @example
|
|
1608
|
-
* ```ts
|
|
1609
|
-
* const state = store.getState();
|
|
1610
|
-
* console.log(state.counter.value);
|
|
1611
|
-
* ```
|
|
1612
|
-
*
|
|
1613
|
-
* @public
|
|
1614
|
-
*/
|
|
1615
|
-
getState() {
|
|
1616
|
-
return this.state;
|
|
1617
|
-
}
|
|
1618
|
-
/**
|
|
1619
|
-
* Registers a middleware (runs **before** reducers).
|
|
1620
|
-
*
|
|
1621
|
-
* @param mw - Middleware `(state, event, emit) => boolean`. Return `false` to cancel event
|
|
1622
|
-
* propagation.
|
|
1623
|
-
* @returns Unsubscribe function that removes this middleware.
|
|
1624
|
-
*
|
|
1625
|
-
* @remarks
|
|
1626
|
-
* **Synchronous, and that is the contract.** The reduce phase completes before `emit()`
|
|
1627
|
-
* returns, so the commit decision has to be available in the same tick. An `async` middleware
|
|
1628
|
-
* returns a Promise, every Promise is truthy, and the veto would therefore never fire — the
|
|
1629
|
-
* event would commit while the middleware was still deciding. The type rejects it; this note
|
|
1630
|
-
* exists because the examples here used to teach it. Do authorization and validation here, and
|
|
1631
|
-
* anything that needs to await in an effect.
|
|
1632
|
-
*
|
|
1633
|
-
* @example Logging middleware
|
|
1634
|
-
* ```ts
|
|
1635
|
-
* const off = store.registerMiddleware((state, event) => {
|
|
1636
|
-
* console.log('Event:', event.channel, event.type, event.payload);
|
|
1637
|
-
* return true; // allow
|
|
1638
|
-
* });
|
|
1639
|
-
* off();
|
|
1640
|
-
* ```
|
|
1641
|
-
*
|
|
1642
|
-
* @example Cancellation middleware
|
|
1643
|
-
* ```ts
|
|
1644
|
-
* store.registerMiddleware((state, event) => {
|
|
1645
|
-
* if (event.type === 'forbidden') return false; // cancel
|
|
1646
|
-
* return true;
|
|
1647
|
-
* });
|
|
1648
|
-
* ```
|
|
1649
|
-
*
|
|
1650
|
-
* @public
|
|
1651
|
-
*/
|
|
1652
|
-
registerMiddleware(e) {
|
|
1653
|
-
return this.middleware.push(e), () => {
|
|
1654
|
-
const t = this.middleware.indexOf(e);
|
|
1655
|
-
t !== -1 && this.middleware.splice(t, 1);
|
|
1656
|
-
};
|
|
1657
|
-
}
|
|
1658
|
-
/**
|
|
1659
|
-
* Dynamically **adds** a named slice reducer at runtime.
|
|
1660
|
-
*
|
|
1661
|
-
* @param name - New slice name (must not already exist).
|
|
1662
|
-
* @param spec - Reducer spec (state, when, reducer).
|
|
1663
|
-
* @returns Disposer function that **removes** the slice (and its state).
|
|
1664
|
-
*
|
|
1665
|
-
* @example
|
|
1666
|
-
* ```ts
|
|
1667
|
-
* const dispose = store.registerReducer('filters', {
|
|
1668
|
-
* state: { q: '' },
|
|
1669
|
-
* events: [['ui', 'setQuery']],
|
|
1670
|
-
* reducer(s, evt) {
|
|
1671
|
-
* return evt.type === 'setQuery' ? { q: evt.payload } : s;
|
|
1672
|
-
* }
|
|
1673
|
-
* });
|
|
1674
|
-
* // Later:
|
|
1675
|
-
* dispose();
|
|
1676
|
-
* ```
|
|
1677
|
-
*
|
|
1678
|
-
* @public
|
|
1679
|
-
*/
|
|
1680
|
-
registerReducer(e, t) {
|
|
1681
|
-
if (e in this.reducers) throw new Error(`Reducer ${e} already exists`);
|
|
1682
|
-
return this.mountSlice(e, t, {
|
|
1683
|
-
preserveState: !1
|
|
1684
|
-
}), this.listeners.forEach((n) => n()), () => {
|
|
1685
|
-
this.unmountSlice(e, { deleteState: !0 }), this.listeners.forEach((n) => n());
|
|
1686
|
-
};
|
|
1687
|
-
}
|
|
1688
|
-
/**
|
|
1689
|
-
* Registers an **effect** (stateless async event consumer) that runs after reducers.
|
|
1690
|
-
*
|
|
1691
|
-
* Effects are **keyed** by `(channel, type)` for O(1) lookup (no scanning all effects).
|
|
1692
|
-
*
|
|
1693
|
-
* @param spec - Effect specification with `when` targeting and `effect` (handler).
|
|
1694
|
-
* @returns Unsubscribe function.
|
|
1695
|
-
*
|
|
1696
|
-
* @example Logging effect
|
|
1697
|
-
* ```ts
|
|
1698
|
-
* const off = store.registerEffect({
|
|
1699
|
-
* events: [['ui', 'increment']],
|
|
1700
|
-
* effect: async (evt, getState, emit) => {
|
|
1701
|
-
* console.log('increment', evt.payload, getState().counter.value);
|
|
1702
|
-
* }
|
|
1703
|
-
* });
|
|
1704
|
-
* off();
|
|
1705
|
-
* ```
|
|
1706
|
-
*
|
|
1707
|
-
* @example Multi-event effect
|
|
1708
|
-
* ```ts
|
|
1709
|
-
* store.registerEffect({
|
|
1710
|
-
* events: [['ui', 'increment'], ['ui', 'decrement']],
|
|
1711
|
-
* effect: async (evt, getState, emit) => {
|
|
1712
|
-
* // Runs for both increment and decrement
|
|
1713
|
-
* await saveToServer(getState());
|
|
1714
|
-
* }
|
|
1715
|
-
* });
|
|
1716
|
-
* ```
|
|
1717
|
-
*
|
|
1718
|
-
* @public
|
|
1719
|
-
*/
|
|
1720
|
-
registerEffect(e) {
|
|
1721
|
-
const { effect: t, meta: n, when: s } = e, i = [];
|
|
1722
|
-
if (n && this.effectMeta.set(t, n), s && ("any" in s && s.any === !0 || "channel" in s || "channels" in s)) {
|
|
1723
|
-
const r = { effect: t, when: s };
|
|
1724
|
-
return this.patternEffects.add(r), () => {
|
|
1725
|
-
this.patternEffects.delete(r);
|
|
1726
|
-
};
|
|
1727
|
-
}
|
|
1728
|
-
const a = this.normalizeEventKeys(e);
|
|
1729
|
-
if (a.length === 0 && !s) {
|
|
1730
|
-
const r = { effect: t, when: { any: !0 } };
|
|
1731
|
-
return this.patternEffects.add(r), () => {
|
|
1732
|
-
this.patternEffects.delete(r);
|
|
1733
|
-
};
|
|
1734
|
-
}
|
|
1735
|
-
for (const [r, f] of a) {
|
|
1736
|
-
const c = `${String(r)}::${String(f)}`;
|
|
1737
|
-
this.effects.has(c) || this.effects.set(c, /* @__PURE__ */ new Set()), this.effects.get(c).add(t), i.push(() => {
|
|
1738
|
-
const u = this.effects.get(c);
|
|
1739
|
-
u && (u.delete(t), u.size === 0 && this.effects.delete(c));
|
|
1740
|
-
});
|
|
1741
|
-
}
|
|
1742
|
-
return () => {
|
|
1743
|
-
for (const r of i) r();
|
|
1744
|
-
};
|
|
1745
|
-
}
|
|
1746
|
-
/**
|
|
1747
|
-
* Convenience helper to register an **effect** filtered by a single `(channel, type)` pair.
|
|
1748
|
-
*
|
|
1749
|
-
* @typeParam C - Channel key within `EM`.
|
|
1750
|
-
* @typeParam T - Event type key within channel `C`.
|
|
1751
|
-
* @param channel - Channel to filter.
|
|
1752
|
-
* @param type - Event type to filter.
|
|
1753
|
-
* @param handler - Effect handler `(payload, getState, emit, event)`.
|
|
1754
|
-
* @returns Unsubscribe/teardown function.
|
|
1755
|
-
*
|
|
1756
|
-
* @example
|
|
1757
|
-
* ```ts
|
|
1758
|
-
* const off = store.onEffect('ui', 'increment', async (n, get, emit) => {
|
|
1759
|
-
* if (n > 10) await emit('ui', 'increment', -10);
|
|
1760
|
-
* });
|
|
1761
|
-
* // later
|
|
1762
|
-
* off();
|
|
1763
|
-
* ```
|
|
1764
|
-
*
|
|
1765
|
-
* @public
|
|
1766
|
-
*/
|
|
1767
|
-
onEffect(e, t, n) {
|
|
1768
|
-
const s = async (i, d, a) => {
|
|
1769
|
-
if (i.channel !== e || i.type !== t) return;
|
|
1770
|
-
const r = i;
|
|
1771
|
-
return n(r.payload, d, a, r);
|
|
1772
|
-
};
|
|
1773
|
-
return this.registerEffect({
|
|
1774
|
-
when: { keys: [[e, t]] },
|
|
1775
|
-
effect: s
|
|
1776
|
-
});
|
|
1777
|
-
}
|
|
1778
|
-
/**
|
|
1779
|
-
* Replaces the **entire** middleware pipeline (HMR-friendly).
|
|
1780
|
-
*
|
|
1781
|
-
* @param next - New middleware array.
|
|
1782
|
-
*
|
|
1783
|
-
* @example Hot module replacement
|
|
1784
|
-
* ```ts
|
|
1785
|
-
* if (import.meta.hot) {
|
|
1786
|
-
* import.meta.hot.accept('./middleware', (newModule) => {
|
|
1787
|
-
* store.replaceMiddleware(newModule.middleware);
|
|
1788
|
-
* });
|
|
1789
|
-
* }
|
|
1790
|
-
* ```
|
|
1791
|
-
*
|
|
1792
|
-
* @public
|
|
1793
|
-
*/
|
|
1794
|
-
replaceMiddleware(e) {
|
|
1795
|
-
this.middleware.length = 0;
|
|
1796
|
-
for (const t of e) this.middleware.push(t);
|
|
1797
|
-
}
|
|
1798
|
-
/**
|
|
1799
|
-
* Replaces all registered **effects** (HMR-friendly).
|
|
1800
|
-
*
|
|
1801
|
-
* @param next - New effects array (as EffectSpecs).
|
|
1802
|
-
*
|
|
1803
|
-
* @example Hot module replacement
|
|
1804
|
-
* ```ts
|
|
1805
|
-
* if (import.meta.hot) {
|
|
1806
|
-
* import.meta.hot.accept('./effects', (newModule) => {
|
|
1807
|
-
* store.replaceEffects(newModule.effects);
|
|
1808
|
-
* });
|
|
1809
|
-
* }
|
|
1810
|
-
* ```
|
|
1811
|
-
*
|
|
1812
|
-
* @public
|
|
1813
|
-
*/
|
|
1814
|
-
replaceEffects(e) {
|
|
1815
|
-
this.effects.clear(), this.patternEffects.clear();
|
|
1816
|
-
for (const t of e)
|
|
1817
|
-
this.registerEffect(t);
|
|
1818
|
-
}
|
|
1819
|
-
/**
|
|
1820
|
-
* Replaces the entire **reducer set** (HMR-friendly).
|
|
1821
|
-
*
|
|
1822
|
-
* @param next - Map of slice specs keyed by slice name.
|
|
1823
|
-
* @param opts - `{ preserveState?: boolean }` (default `true`).
|
|
1824
|
-
*
|
|
1825
|
-
* @example Hot module replacement
|
|
1826
|
-
* ```ts
|
|
1827
|
-
* if (import.meta.hot) {
|
|
1828
|
-
* import.meta.hot.accept('./reducers', (newModule) => {
|
|
1829
|
-
* store.replaceReducers(newModule.reducers, { preserveState: true });
|
|
1830
|
-
* });
|
|
1831
|
-
* }
|
|
1832
|
-
* ```
|
|
1833
|
-
*
|
|
1834
|
-
* @public
|
|
1835
|
-
*/
|
|
1836
|
-
replaceReducers(e, t = {}) {
|
|
1837
|
-
const n = t.preserveState !== !1, s = new Set(Object.keys(this.reducers)), i = Object.entries(e), d = new Set(i.map(([a]) => a));
|
|
1838
|
-
for (const a of s)
|
|
1839
|
-
d.has(a) || this.unmountSlice(a, { deleteState: !0 });
|
|
1840
|
-
for (const [a, r] of i)
|
|
1841
|
-
s.has(a) ? (this.unmountSlice(a, { deleteState: !1 }), this.mountSlice(a, r, { preserveState: n })) : this.mountSlice(a, r, { preserveState: !1 });
|
|
1842
|
-
}
|
|
1843
|
-
/**
|
|
1844
|
-
* Convenience API to replace **any subset** of store parts (HMR patterns).
|
|
1845
|
-
*
|
|
1846
|
-
* @param partial - Partial replacement set.
|
|
1847
|
-
*
|
|
1848
|
-
* @example Replace everything
|
|
1849
|
-
* ```ts
|
|
1850
|
-
* store.hotReplace({
|
|
1851
|
-
* reducer: newReducers,
|
|
1852
|
-
* middleware: newMiddleware,
|
|
1853
|
-
* effects: newEffects,
|
|
1854
|
-
* preserveState: true
|
|
1855
|
-
* });
|
|
1856
|
-
* ```
|
|
1857
|
-
*
|
|
1858
|
-
* @public
|
|
1859
|
-
*/
|
|
1860
|
-
hotReplace(e) {
|
|
1861
|
-
e.middleware && this.replaceMiddleware(e.middleware), e.effects && this.replaceEffects(e.effects), e.reducer && this.replaceReducers(e.reducer, { preserveState: e.preserveState });
|
|
1862
|
-
}
|
|
1863
|
-
/**
|
|
1864
|
-
* Mounts a slice: installs reducer, initializes state (unless preserved),
|
|
1865
|
-
* and wires `(channel, type)` listeners on the reducer bus.
|
|
1866
|
-
*
|
|
1867
|
-
* @param name - Slice name.
|
|
1868
|
-
* @param rSpec - Reducer spec (state, when, reducer).
|
|
1869
|
-
* @param opts - `{ preserveState: boolean }` whether to keep existing state.
|
|
1870
|
-
*
|
|
1871
|
-
* @internal
|
|
1872
|
-
*/
|
|
1873
|
-
mountSlice(e, t, n) {
|
|
1874
|
-
const s = e, { reducer: i, state: d, when: a } = t;
|
|
1875
|
-
if (this.reducers[e] = new K(i), (!n.preserveState || this.state[s] === void 0) && (this.state = {
|
|
1876
|
-
...this.state,
|
|
1877
|
-
[s]: k(W(s, d))
|
|
1878
|
-
}), a && ("any" in a && a.any === !0 || "channel" in a || "channels" in a)) {
|
|
1879
|
-
this.patternReducers.set(e, a), this.sliceUnsubs.set(s, []);
|
|
1880
|
-
return;
|
|
1881
|
-
}
|
|
1882
|
-
const f = this.normalizeEventKeys(t);
|
|
1883
|
-
if (f.length === 0 && !a) {
|
|
1884
|
-
this.patternReducers.set(e, { any: !0 }), this.sliceUnsubs.set(s, []);
|
|
1885
|
-
return;
|
|
1886
|
-
}
|
|
1887
|
-
const c = [];
|
|
1888
|
-
for (const [u, l] of f) {
|
|
1889
|
-
const y = this.reducerBus.on(u, l, (h, m) => {
|
|
1890
|
-
const g = m ?? {
|
|
1891
|
-
channel: u,
|
|
1892
|
-
type: l,
|
|
1893
|
-
payload: h,
|
|
1894
|
-
id: this.idFactory()
|
|
1895
|
-
};
|
|
1896
|
-
this.forwardEventGuarded(e, g);
|
|
1897
|
-
});
|
|
1898
|
-
c.push(y);
|
|
1899
|
-
}
|
|
1900
|
-
this.sliceUnsubs.set(s, c);
|
|
1901
|
-
}
|
|
1902
|
-
/**
|
|
1903
|
-
* Unmounts a slice: disposes reducer-bus listeners, removes reducer,
|
|
1904
|
-
* and optionally deletes the slice state.
|
|
1905
|
-
*
|
|
1906
|
-
* @param name - Slice name.
|
|
1907
|
-
* @param opts - `{ deleteState: boolean }`.
|
|
1908
|
-
*
|
|
1909
|
-
* @internal
|
|
1910
|
-
*/
|
|
1911
|
-
unmountSlice(e, t) {
|
|
1912
|
-
const n = e;
|
|
1913
|
-
this.patternReducers.delete(e);
|
|
1914
|
-
const s = this.sliceUnsubs.get(n);
|
|
1915
|
-
if (s) {
|
|
1916
|
-
for (const i of s)
|
|
1917
|
-
try {
|
|
1918
|
-
i();
|
|
1919
|
-
} catch (d) {
|
|
1920
|
-
console.error(`[Store error]: ${d}`);
|
|
1921
|
-
}
|
|
1922
|
-
this.sliceUnsubs.delete(n);
|
|
1923
|
-
}
|
|
1924
|
-
if (delete this.reducers[e], t.deleteState) {
|
|
1925
|
-
const { [n]: i, ...d } = this.state;
|
|
1926
|
-
this.state = d;
|
|
1927
|
-
}
|
|
1928
|
-
}
|
|
1929
|
-
/**
|
|
1930
|
-
* Normalizes event targeting from `when` to an array of EventKeys.
|
|
1931
|
-
*
|
|
1932
|
-
* @param spec - Object with an optional `when` matcher.
|
|
1933
|
-
* @returns Array of `[channel, type]` pairs.
|
|
1934
|
-
*
|
|
1935
|
-
* @internal
|
|
1936
|
-
*/
|
|
1937
|
-
normalizeEventKeys(e) {
|
|
1938
|
-
if (e.when) {
|
|
1939
|
-
const t = e.when;
|
|
1940
|
-
if ("keys" in t)
|
|
1941
|
-
return t.keys;
|
|
1942
|
-
}
|
|
1943
|
-
return [];
|
|
1944
|
-
}
|
|
1945
|
-
/**
|
|
1946
|
-
* Reads a dotted path from an object (supports numeric array indices via string keys).
|
|
1947
|
-
*
|
|
1948
|
-
* @param obj - Root object (slice or value).
|
|
1949
|
-
* @param path - Dotted path; leading dot is ignored.
|
|
1950
|
-
* @returns The value at the path, or `undefined`.
|
|
1951
|
-
*
|
|
1952
|
-
* @internal
|
|
1953
|
-
*/
|
|
1954
|
-
getAtPath(e, t) {
|
|
1955
|
-
if (!t) return e;
|
|
1956
|
-
const s = (t[0] === "." ? t.slice(1) : t).split(".");
|
|
1957
|
-
let i = e;
|
|
1958
|
-
for (const d of s) {
|
|
1959
|
-
if (i == null) return;
|
|
1960
|
-
i = i[d];
|
|
1961
|
-
}
|
|
1962
|
-
return i;
|
|
1963
|
-
}
|
|
1964
|
-
/**
|
|
1965
|
-
* Builds ancestor paths for a dotted path.
|
|
1966
|
-
*
|
|
1967
|
-
* For `"a.b.c"`, returns `["a", "a.b", "a.b.c"]`. Leading dots are trimmed.
|
|
1968
|
-
*
|
|
1969
|
-
* @param path - Dotted path string.
|
|
1970
|
-
* @returns Array of ancestor paths.
|
|
1971
|
-
*
|
|
1972
|
-
* @example
|
|
1973
|
-
* ```ts
|
|
1974
|
-
* Store.buildAncestorPaths('x.y.z'); // ['x','x.y','x.y.z']
|
|
1975
|
-
* ```
|
|
1976
|
-
*
|
|
1977
|
-
* @public
|
|
1978
|
-
*/
|
|
1979
|
-
static buildAncestorPaths(e) {
|
|
1980
|
-
if (!e) return [];
|
|
1981
|
-
const n = (e[0] === "." ? e.slice(1) : e).split("."), s = [];
|
|
1982
|
-
for (let i = 0; i < n.length; i++)
|
|
1983
|
-
s.push(n.slice(0, i + 1).join("."));
|
|
1984
|
-
return s;
|
|
1985
|
-
}
|
|
1986
|
-
}
|
|
1987
|
-
function L(o) {
|
|
1988
|
-
return new $({
|
|
1989
|
-
name: o.name,
|
|
1990
|
-
reducer: o.reducer ?? {},
|
|
1991
|
-
middleware: o.middleware ?? [],
|
|
1992
|
-
effects: o.effects ?? [],
|
|
1993
|
-
dedupWindowMs: o.dedupWindowMs,
|
|
1994
|
-
idFactory: o.idFactory,
|
|
1995
|
-
devtools: o.devtools,
|
|
1996
|
-
onEffectError: o.onEffectError,
|
|
1997
|
-
onReducerError: o.onReducerError
|
|
1998
|
-
});
|
|
1999
|
-
}
|
|
2000
|
-
const G = (o) => (e, t) => t.map((n) => [e, n]), Q = () => (o) => o, I = /* @__PURE__ */ new Set();
|
|
2001
|
-
function j(o) {
|
|
2002
|
-
const e = String(o);
|
|
2003
|
-
I.has(e) || (I.add(e), console.warn(
|
|
2004
|
-
`[yoltra] Entity id "${e}" contains a dot. Paths are dotted, so a subscription to "entities.${e}" is indistinguishable from one to a nested object of the same name. Use ids without dots.`
|
|
2005
|
-
));
|
|
2006
|
-
}
|
|
2007
|
-
function F(o, e) {
|
|
2008
|
-
if (o.length !== e.length) return e;
|
|
2009
|
-
for (let t = 0; t < o.length; t++)
|
|
2010
|
-
if (o[t] !== e[t]) return e;
|
|
2011
|
-
return o;
|
|
2012
|
-
}
|
|
2013
|
-
function J(o = {}) {
|
|
2014
|
-
const e = o.selectId ?? ((r) => r.id), { sortComparer: t } = o, n = (r, f) => {
|
|
2015
|
-
if (t === void 0) return f;
|
|
2016
|
-
const c = [...f].sort((u, l) => {
|
|
2017
|
-
const y = r.entities[u], h = r.entities[l];
|
|
2018
|
-
return y === void 0 || h === void 0 ? 0 : t(y, h);
|
|
2019
|
-
});
|
|
2020
|
-
return F(f, c);
|
|
2021
|
-
}, s = (r, f, c) => {
|
|
2022
|
-
const u = { ...r, entities: f, ids: c };
|
|
2023
|
-
return { ...u, ids: n(u, c) };
|
|
2024
|
-
}, i = (r, f, c) => {
|
|
2025
|
-
let u = null, l = null;
|
|
2026
|
-
for (const y of f) {
|
|
2027
|
-
const h = e(y);
|
|
2028
|
-
process.env.NODE_ENV !== "production" && String(h).includes(".") && j(h);
|
|
2029
|
-
const m = (u ?? r.entities)[h];
|
|
2030
|
-
if (m !== void 0 && c === "add") continue;
|
|
2031
|
-
const g = m !== void 0 && c === "upsert" ? { ...m, ...y } : y;
|
|
2032
|
-
u ?? (u = { ...r.entities }), u[h] = g, m === void 0 && (l ?? (l = [...r.ids]), l.push(h));
|
|
2033
|
-
}
|
|
2034
|
-
return u === null ? r : s(r, u, l ?? r.ids);
|
|
2035
|
-
}, d = (r, f) => {
|
|
2036
|
-
let c = null;
|
|
2037
|
-
for (const { id: u, changes: l } of f) {
|
|
2038
|
-
const y = (c ?? r.entities)[u];
|
|
2039
|
-
y !== void 0 && (c ?? (c = { ...r.entities }), c[u] = { ...y, ...l });
|
|
2040
|
-
}
|
|
2041
|
-
return c === null ? r : s(r, c, r.ids);
|
|
2042
|
-
}, a = (r, f) => {
|
|
2043
|
-
const c = new Set(f.filter((l) => r.entities[l] !== void 0));
|
|
2044
|
-
if (c.size === 0) return r;
|
|
2045
|
-
const u = { ...r.entities };
|
|
2046
|
-
for (const l of c) delete u[l];
|
|
2047
|
-
return s(
|
|
2048
|
-
r,
|
|
2049
|
-
u,
|
|
2050
|
-
r.ids.filter((l) => !c.has(l))
|
|
2051
|
-
);
|
|
2052
|
-
};
|
|
2053
|
-
return {
|
|
2054
|
-
getInitialState(r) {
|
|
2055
|
-
const f = { ids: [], entities: {} };
|
|
2056
|
-
return r === void 0 ? f : { ...f, ...r };
|
|
2057
|
-
},
|
|
2058
|
-
addOne: (r, f) => i(r, [f], "add"),
|
|
2059
|
-
addMany: (r, f) => i(r, f, "add"),
|
|
2060
|
-
setOne: (r, f) => i(r, [f], "set"),
|
|
2061
|
-
setMany: (r, f) => i(r, f, "set"),
|
|
2062
|
-
setAll: (r, f) => {
|
|
2063
|
-
const c = {}, u = [];
|
|
2064
|
-
for (const l of f) {
|
|
2065
|
-
const y = e(l);
|
|
2066
|
-
c[y] === void 0 && u.push(y), c[y] = l;
|
|
2067
|
-
}
|
|
2068
|
-
return s(r, c, u);
|
|
2069
|
-
},
|
|
2070
|
-
updateOne: (r, f) => d(r, [f]),
|
|
2071
|
-
updateMany: (r, f) => d(r, f),
|
|
2072
|
-
upsertOne: (r, f) => i(r, [f], "upsert"),
|
|
2073
|
-
upsertMany: (r, f) => i(r, f, "upsert"),
|
|
2074
|
-
removeOne: (r, f) => a(r, [f]),
|
|
2075
|
-
removeMany: (r, f) => a(r, f),
|
|
2076
|
-
removeAll: (r) => r.ids.length === 0 ? r : s(r, {}, []),
|
|
2077
|
-
selectIds: (r) => r.ids,
|
|
2078
|
-
selectEntities: (r) => r.entities,
|
|
2079
|
-
selectAll: (r) => r.ids.map((f) => r.entities[f]),
|
|
2080
|
-
selectById: (r, f) => r.entities[f],
|
|
2081
|
-
selectTotal: (r) => r.ids.length,
|
|
2082
|
-
idsPath: "ids",
|
|
2083
|
-
pathTo: (r, f) => f === void 0 ? `entities.${r}` : `entities.${r}.${f}`,
|
|
2084
|
-
anyField: (r) => `entities.*.${r}`
|
|
2085
|
-
};
|
|
2086
|
-
}
|
|
2087
|
-
const E = "$yoltra";
|
|
2088
|
-
function _(o, e = {}) {
|
|
2089
|
-
const t = e.maxNodes ?? 1e5, n = e.sanitize, s = [], i = /* @__PURE__ */ new Map();
|
|
2090
|
-
let d = 0, a = !1;
|
|
2091
|
-
function r(c, u) {
|
|
2092
|
-
if (n !== void 0 && (c = n(u, c)), d += 1, d > t)
|
|
2093
|
-
return a = !0, { [E]: "unsupported", kind: "truncated" };
|
|
2094
|
-
switch (typeof c) {
|
|
2095
|
-
case "undefined":
|
|
2096
|
-
return { [E]: "undefined" };
|
|
2097
|
-
case "bigint":
|
|
2098
|
-
return { [E]: "bigint", value: c.toString() };
|
|
2099
|
-
case "number":
|
|
2100
|
-
return Number.isNaN(c) ? { [E]: "nan" } : c === 1 / 0 ? { [E]: "infinity", sign: 1 } : c === -1 / 0 ? { [E]: "infinity", sign: -1 } : c;
|
|
2101
|
-
case "function":
|
|
2102
|
-
case "symbol":
|
|
2103
|
-
return s.push(u), { [E]: "unsupported", kind: typeof c };
|
|
2104
|
-
case "string":
|
|
2105
|
-
case "boolean":
|
|
2106
|
-
return c;
|
|
2107
|
-
}
|
|
2108
|
-
if (c === null) return null;
|
|
2109
|
-
const l = c, y = i.get(l);
|
|
2110
|
-
if (y !== void 0) return { [E]: "ref", path: y };
|
|
2111
|
-
if (i.set(l, u), c instanceof Date)
|
|
2112
|
-
return { [E]: "date", iso: c.toISOString() };
|
|
2113
|
-
if (c instanceof RegExp)
|
|
2114
|
-
return { [E]: "regexp", source: c.source, flags: c.flags };
|
|
2115
|
-
if (c instanceof Error)
|
|
2116
|
-
return { [E]: "error", name: c.name, message: c.message };
|
|
2117
|
-
if (c instanceof Map) {
|
|
2118
|
-
const m = [];
|
|
2119
|
-
let g = 0;
|
|
2120
|
-
for (const [w, D] of c)
|
|
2121
|
-
m.push([r(w, `${u}/@k${g}`), r(D, `${u}/${g}`)]), g += 1;
|
|
2122
|
-
return { [E]: "map", entries: m };
|
|
2123
|
-
}
|
|
2124
|
-
if (c instanceof Set) {
|
|
2125
|
-
const m = [];
|
|
2126
|
-
let g = 0;
|
|
2127
|
-
for (const w of c)
|
|
2128
|
-
m.push(r(w, `${u}/${g}`)), g += 1;
|
|
2129
|
-
return { [E]: "set", values: m };
|
|
2130
|
-
}
|
|
2131
|
-
if (Array.isArray(c))
|
|
2132
|
-
return c.map((m, g) => r(m, `${u}/${g}`));
|
|
2133
|
-
const h = {};
|
|
2134
|
-
for (const [m, g] of Object.entries(c))
|
|
2135
|
-
h[m] = r(g, `${u}/${N(m)}`);
|
|
2136
|
-
return E in h ? { [E]: "escaped", value: h } : h;
|
|
2137
|
-
}
|
|
2138
|
-
return { value: r(o, ""), report: { truncated: a, unsupported: s } };
|
|
2139
|
-
}
|
|
2140
|
-
function V(o) {
|
|
2141
|
-
const e = /* @__PURE__ */ new Map(), t = [];
|
|
2142
|
-
function n(d, a) {
|
|
2143
|
-
if (d === null || typeof d != "object") return d;
|
|
2144
|
-
if (Array.isArray(d)) {
|
|
2145
|
-
const f = [];
|
|
2146
|
-
return e.set(a, f), d.forEach((c, u) => {
|
|
2147
|
-
if (R(c)) {
|
|
2148
|
-
t.push({ target: f, key: u, path: c.path }), f[u] = void 0;
|
|
2149
|
-
return;
|
|
2150
|
-
}
|
|
2151
|
-
f[u] = n(c, `${a}/${u}`);
|
|
2152
|
-
}), f;
|
|
2153
|
-
}
|
|
2154
|
-
if (typeof d[E] == "string") {
|
|
2155
|
-
const f = d;
|
|
2156
|
-
switch (f[E]) {
|
|
2157
|
-
case "undefined":
|
|
2158
|
-
return;
|
|
2159
|
-
case "nan":
|
|
2160
|
-
return Number.NaN;
|
|
2161
|
-
case "infinity":
|
|
2162
|
-
return f.sign === 1 ? 1 / 0 : -1 / 0;
|
|
2163
|
-
case "bigint":
|
|
2164
|
-
return BigInt(f.value);
|
|
2165
|
-
case "date":
|
|
2166
|
-
return new Date(f.iso);
|
|
2167
|
-
case "regexp":
|
|
2168
|
-
return new RegExp(f.source, f.flags);
|
|
2169
|
-
case "error": {
|
|
2170
|
-
const c = new Error(f.message);
|
|
2171
|
-
return c.name = f.name, c;
|
|
2172
|
-
}
|
|
2173
|
-
case "unsupported":
|
|
2174
|
-
return;
|
|
2175
|
-
case "ref":
|
|
2176
|
-
return;
|
|
2177
|
-
case "map": {
|
|
2178
|
-
const c = /* @__PURE__ */ new Map();
|
|
2179
|
-
return e.set(a, c), f.entries.forEach(([u, l], y) => {
|
|
2180
|
-
c.set(n(u, `${a}/@k${y}`), n(l, `${a}/${y}`));
|
|
2181
|
-
}), c;
|
|
2182
|
-
}
|
|
2183
|
-
case "set": {
|
|
2184
|
-
const c = /* @__PURE__ */ new Set();
|
|
2185
|
-
return e.set(a, c), f.values.forEach((u, l) => c.add(n(u, `${a}/${l}`))), c;
|
|
2186
|
-
}
|
|
2187
|
-
case "escaped":
|
|
2188
|
-
return s(f.value, a);
|
|
2189
|
-
default:
|
|
2190
|
-
return;
|
|
2191
|
-
}
|
|
2192
|
-
}
|
|
2193
|
-
return s(d, a);
|
|
2194
|
-
}
|
|
2195
|
-
function s(d, a) {
|
|
2196
|
-
const r = {};
|
|
2197
|
-
e.set(a, r);
|
|
2198
|
-
for (const [f, c] of Object.entries(d)) {
|
|
2199
|
-
const u = `${a}/${N(f)}`;
|
|
2200
|
-
if (R(c)) {
|
|
2201
|
-
t.push({ target: r, key: f, path: c.path }), r[f] = void 0;
|
|
2202
|
-
continue;
|
|
2203
|
-
}
|
|
2204
|
-
r[f] = n(c, u);
|
|
2205
|
-
}
|
|
2206
|
-
return r;
|
|
2207
|
-
}
|
|
2208
|
-
const i = n(o, "");
|
|
2209
|
-
e.set("", i);
|
|
2210
|
-
for (const { target: d, key: a, path: r } of t)
|
|
2211
|
-
d[a] = e.get(r);
|
|
2212
|
-
return i;
|
|
2213
|
-
}
|
|
2214
|
-
function R(o) {
|
|
2215
|
-
return o !== null && typeof o == "object" && o[E] === "ref" && typeof o.path == "string";
|
|
2216
|
-
}
|
|
2217
|
-
function N(o) {
|
|
2218
|
-
return o.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
2219
|
-
}
|
|
2220
|
-
function Y(o, e, t = {}) {
|
|
2221
|
-
let n = t.maxNodes ?? 1e5;
|
|
2222
|
-
for (let s = 0; s < 8; s += 1) {
|
|
2223
|
-
const { value: i, report: d } = _(o, { ...t, maxNodes: n });
|
|
2224
|
-
let a;
|
|
2225
|
-
try {
|
|
2226
|
-
a = JSON.stringify(i)?.length ?? 0;
|
|
2227
|
-
} catch {
|
|
2228
|
-
a = Number.POSITIVE_INFINITY;
|
|
2229
|
-
}
|
|
2230
|
-
if (a <= e)
|
|
2231
|
-
return d.truncated ? {
|
|
2232
|
-
value: i,
|
|
2233
|
-
truncated: !0,
|
|
2234
|
-
note: `State was too large to send in full; parts beyond ${n} nodes are omitted.`
|
|
2235
|
-
} : { value: i, truncated: !1 };
|
|
2236
|
-
const r = Math.floor(n * e * 0.8 / a);
|
|
2237
|
-
if (n = Math.max(1, Math.min(r, n - 1)), n <= 1 && s > 0)
|
|
2238
|
-
break;
|
|
2239
|
-
}
|
|
2240
|
-
return {
|
|
2241
|
-
value: { [E]: "unsupported", kind: "truncated" },
|
|
2242
|
-
truncated: !0,
|
|
2243
|
-
note: `State exceeds the ${e}-byte transport limit and could not be reduced to fit.`
|
|
2244
|
-
};
|
|
2245
|
-
}
|
|
2246
|
-
function b(o, e, t) {
|
|
2247
|
-
o.onError?.(e, t);
|
|
2248
|
-
}
|
|
2249
|
-
async function q(o) {
|
|
2250
|
-
const e = { slices: {}, restored: !1 };
|
|
2251
|
-
let t;
|
|
2252
|
-
try {
|
|
2253
|
-
t = o.source ?? await o.adapter.read(o.key);
|
|
2254
|
-
} catch (s) {
|
|
2255
|
-
return b(o, s, "read"), e;
|
|
2256
|
-
}
|
|
2257
|
-
if (t == null || t === "") return e;
|
|
2258
|
-
let n;
|
|
2259
|
-
try {
|
|
2260
|
-
n = V(JSON.parse(t));
|
|
2261
|
-
} catch (s) {
|
|
2262
|
-
return b(o, s, "decode"), e;
|
|
2263
|
-
}
|
|
2264
|
-
if (n === null || typeof n != "object" || typeof n.version != "number")
|
|
2265
|
-
return b(o, new Error("persisted payload is not a recognisable envelope"), "decode"), e;
|
|
2266
|
-
if (n.version !== o.version) {
|
|
2267
|
-
if (o.migrate === void 0)
|
|
2268
|
-
return b(
|
|
2269
|
-
o,
|
|
2270
|
-
new Error(
|
|
2271
|
-
`persisted state is version ${n.version}, this build expects ${o.version}, and no migrate was supplied`
|
|
2272
|
-
),
|
|
2273
|
-
"migrate"
|
|
2274
|
-
), e;
|
|
2275
|
-
try {
|
|
2276
|
-
const s = o.migrate(n.slices, n.version);
|
|
2277
|
-
return s === null ? e : { slices: s, restored: !0 };
|
|
2278
|
-
} catch (s) {
|
|
2279
|
-
return b(o, s, "migrate"), e;
|
|
2280
|
-
}
|
|
2281
|
-
}
|
|
2282
|
-
return { slices: n.slices ?? {}, restored: !0 };
|
|
2283
|
-
}
|
|
2284
|
-
function X(o, e) {
|
|
2285
|
-
if (!e.restored) return o;
|
|
2286
|
-
const t = {};
|
|
2287
|
-
for (const [n, s] of Object.entries(o)) {
|
|
2288
|
-
const i = e.slices[n];
|
|
2289
|
-
t[n] = i === void 0 ? s : { ...s, state: i };
|
|
2290
|
-
}
|
|
2291
|
-
return t;
|
|
2292
|
-
}
|
|
2293
|
-
function A(o, e) {
|
|
2294
|
-
const t = o ?? {}, n = e.slices === void 0 ? t : Object.fromEntries(e.slices.filter((s) => s in t).map((s) => [s, t[s]]));
|
|
2295
|
-
return JSON.stringify(_({ version: e.version, slices: n }).value);
|
|
2296
|
-
}
|
|
2297
|
-
function Z(o, e) {
|
|
2298
|
-
const t = e.throttleMs ?? 250, n = e.slices;
|
|
2299
|
-
let s = null, i = !1;
|
|
2300
|
-
const d = () => {
|
|
2301
|
-
if (i) {
|
|
2302
|
-
i = !1;
|
|
2303
|
-
try {
|
|
2304
|
-
const f = e.adapter.write(e.key, A(o.getState(), e));
|
|
2305
|
-
f instanceof Promise && f.catch((c) => b(e, c, "write"));
|
|
2306
|
-
} catch (f) {
|
|
2307
|
-
b(e, f, "write");
|
|
2308
|
-
}
|
|
2309
|
-
}
|
|
2310
|
-
}, a = () => {
|
|
2311
|
-
if (i = !0, t <= 0) {
|
|
2312
|
-
d();
|
|
2313
|
-
return;
|
|
2314
|
-
}
|
|
2315
|
-
s === null && (s = setTimeout(() => {
|
|
2316
|
-
s = null, d();
|
|
2317
|
-
}, t), s.unref?.());
|
|
2318
|
-
}, r = o.instrument((f) => {
|
|
2319
|
-
if (n === void 0) {
|
|
2320
|
-
a();
|
|
2321
|
-
return;
|
|
2322
|
-
}
|
|
2323
|
-
(f.changedPaths ?? []).some(
|
|
2324
|
-
(u) => n.some((l) => u === l || u.startsWith(`${l}.`))
|
|
2325
|
-
) && a();
|
|
2326
|
-
});
|
|
2327
|
-
return () => {
|
|
2328
|
-
r(), s !== null && (clearTimeout(s), s = null), d();
|
|
2329
|
-
};
|
|
2330
|
-
}
|
|
2331
|
-
function ee(o, e) {
|
|
2332
|
-
return A(o.getState(), e);
|
|
2333
|
-
}
|
|
2334
|
-
function te(o) {
|
|
2335
|
-
return {
|
|
2336
|
-
read: (e) => o.getItem(e),
|
|
2337
|
-
write: (e, t) => o.setItem(e, t),
|
|
2338
|
-
remove: (e) => o.removeItem(e)
|
|
2339
|
-
};
|
|
2340
|
-
}
|
|
2341
|
-
function se(o) {
|
|
2342
|
-
const e = new Map(Object.entries(o ?? {}));
|
|
2343
|
-
return {
|
|
2344
|
-
read: (t) => e.get(t) ?? null,
|
|
2345
|
-
write: (t, n) => {
|
|
2346
|
-
e.set(t, n);
|
|
2347
|
-
},
|
|
2348
|
-
remove: (t) => {
|
|
2349
|
-
e.delete(t);
|
|
2350
|
-
}
|
|
2351
|
-
};
|
|
2352
|
-
}
|
|
2353
|
-
export {
|
|
2354
|
-
B as EventBus,
|
|
2355
|
-
H as LooseEventBus,
|
|
2356
|
-
K as Reducer,
|
|
2357
|
-
$ as Store,
|
|
2358
|
-
J as createEntityAdapter,
|
|
2359
|
-
se as createMemoryAdapter,
|
|
2360
|
-
L as createStore,
|
|
2361
|
-
te as createWebStorageAdapter,
|
|
2362
|
-
V as decodeState,
|
|
2363
|
-
ee as dehydrate,
|
|
2364
|
-
O as detectChangedProps,
|
|
2365
|
-
_ as encodeState,
|
|
2366
|
-
Y as encodeStateBounded,
|
|
2367
|
-
Q as eventKeys,
|
|
2368
|
-
S as freezeState,
|
|
2369
|
-
q as hydrate,
|
|
2370
|
-
Z as persist,
|
|
2371
|
-
G as typedEvents,
|
|
2372
|
-
X as withHydration
|
|
2373
|
-
};
|
|
2374
|
-
//# sourceMappingURL=yoltra.esm.js.map
|