@coherent.js/state 1.0.0-beta.5 → 1.0.0-beta.7
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/dist/index.js.map +7 -0
- package/dist/reactive-state.js +512 -0
- package/dist/reactive-state.js.map +7 -0
- package/dist/state-manager.js +102 -0
- package/dist/state-manager.js.map +7 -0
- package/dist/state-persistence.js +472 -0
- package/dist/state-persistence.js.map +7 -0
- package/dist/state-validation.js +621 -0
- package/dist/state-validation.js.map +7 -0
- package/package.json +13 -3
- package/types/index.d.ts +364 -39
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
// src/reactive-state.js
|
|
2
|
+
var StateError = class extends Error {
|
|
3
|
+
constructor(message, options = {}) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "StateError";
|
|
6
|
+
this.type = options.type || "state";
|
|
7
|
+
this.component = options.component;
|
|
8
|
+
this.context = options.context;
|
|
9
|
+
this.timestamp = Date.now();
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var globalErrorHandler = {
|
|
13
|
+
handle(error, context = {}) {
|
|
14
|
+
console.error("State Error:", error.message, context);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var Observable = class _Observable {
|
|
18
|
+
constructor(value, options = {}) {
|
|
19
|
+
this._value = value;
|
|
20
|
+
this._observers = /* @__PURE__ */ new Set();
|
|
21
|
+
this._computedDependents = /* @__PURE__ */ new Set();
|
|
22
|
+
this._options = {
|
|
23
|
+
deep: options.deep !== false,
|
|
24
|
+
immediate: options.immediate !== false,
|
|
25
|
+
...options
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
get value() {
|
|
29
|
+
if (_Observable._currentComputed) {
|
|
30
|
+
this._computedDependents.add(_Observable._currentComputed);
|
|
31
|
+
}
|
|
32
|
+
return this._value;
|
|
33
|
+
}
|
|
34
|
+
set value(newValue) {
|
|
35
|
+
if (this._value === newValue && !this._options.deep) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const oldValue = this._value;
|
|
39
|
+
this._value = newValue;
|
|
40
|
+
this._observers.forEach((observer) => {
|
|
41
|
+
try {
|
|
42
|
+
observer(newValue, oldValue);
|
|
43
|
+
} catch (_error) {
|
|
44
|
+
globalErrorHandler.handle(_error, {
|
|
45
|
+
type: "watcher-_error",
|
|
46
|
+
context: { newValue, oldValue }
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
this._computedDependents.forEach((computed2) => {
|
|
51
|
+
computed2._invalidate();
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
watch(callback, options = {}) {
|
|
55
|
+
if (typeof callback !== "function") {
|
|
56
|
+
throw new StateError("Watch callback must be a function");
|
|
57
|
+
}
|
|
58
|
+
const observer = (newValue, oldValue) => {
|
|
59
|
+
callback(newValue, oldValue, () => this.unwatch(observer));
|
|
60
|
+
};
|
|
61
|
+
this._observers.add(observer);
|
|
62
|
+
if (options.immediate !== false) {
|
|
63
|
+
observer(this._value, void 0);
|
|
64
|
+
}
|
|
65
|
+
return () => this.unwatch(observer);
|
|
66
|
+
}
|
|
67
|
+
unwatch(observer) {
|
|
68
|
+
this._observers.delete(observer);
|
|
69
|
+
}
|
|
70
|
+
unwatchAll() {
|
|
71
|
+
this._observers.clear();
|
|
72
|
+
this._computedDependents.clear();
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var Computed = class extends Observable {
|
|
76
|
+
constructor(getter, options = {}) {
|
|
77
|
+
super(void 0, options);
|
|
78
|
+
this._getter = getter;
|
|
79
|
+
this._cached = false;
|
|
80
|
+
this._dirty = true;
|
|
81
|
+
if (typeof getter !== "function") {
|
|
82
|
+
throw new StateError("Computed getter must be a function");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
get value() {
|
|
86
|
+
if (this._dirty || !this._cached) {
|
|
87
|
+
this._compute();
|
|
88
|
+
}
|
|
89
|
+
return this._value;
|
|
90
|
+
}
|
|
91
|
+
set value(newValue) {
|
|
92
|
+
throw new StateError("Cannot set value on computed property");
|
|
93
|
+
}
|
|
94
|
+
_compute() {
|
|
95
|
+
const prevComputed = Observable._currentComputed;
|
|
96
|
+
Observable._currentComputed = this;
|
|
97
|
+
try {
|
|
98
|
+
const newValue = this._getter();
|
|
99
|
+
if (newValue !== this._value) {
|
|
100
|
+
const oldValue = this._value;
|
|
101
|
+
this._value = newValue;
|
|
102
|
+
this._observers.forEach((observer) => {
|
|
103
|
+
observer(newValue, oldValue);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
this._cached = true;
|
|
107
|
+
this._dirty = false;
|
|
108
|
+
} catch (_error) {
|
|
109
|
+
globalErrorHandler.handle(_error, {
|
|
110
|
+
type: "computed-_error",
|
|
111
|
+
context: { getter: this._getter.toString() }
|
|
112
|
+
});
|
|
113
|
+
} finally {
|
|
114
|
+
Observable._currentComputed = prevComputed;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
_invalidate() {
|
|
118
|
+
this._dirty = true;
|
|
119
|
+
this._computedDependents.forEach((computed2) => {
|
|
120
|
+
computed2._invalidate();
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
Observable._currentComputed = null;
|
|
125
|
+
var ReactiveState = class {
|
|
126
|
+
constructor(initialState = {}, options = {}) {
|
|
127
|
+
this._state = /* @__PURE__ */ new Map();
|
|
128
|
+
this._computed = /* @__PURE__ */ new Map();
|
|
129
|
+
this._watchers = /* @__PURE__ */ new Map();
|
|
130
|
+
this._middleware = [];
|
|
131
|
+
this._history = [];
|
|
132
|
+
this._options = {
|
|
133
|
+
enableHistory: options.enableHistory !== false,
|
|
134
|
+
maxHistorySize: options.maxHistorySize || 50,
|
|
135
|
+
enableMiddleware: options.enableMiddleware !== false,
|
|
136
|
+
deep: options.deep !== false,
|
|
137
|
+
...options
|
|
138
|
+
};
|
|
139
|
+
Object.entries(initialState).forEach(([key, value]) => {
|
|
140
|
+
this.set(key, value);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Get reactive state value
|
|
145
|
+
*/
|
|
146
|
+
get(key) {
|
|
147
|
+
const observable2 = this._state.get(key);
|
|
148
|
+
return observable2 ? observable2.value : void 0;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Set reactive state value
|
|
152
|
+
*/
|
|
153
|
+
set(key, value, options = {}) {
|
|
154
|
+
const config = { ...this._options, ...options };
|
|
155
|
+
if (config.enableMiddleware) {
|
|
156
|
+
const middlewareResult = this._runMiddleware("set", { key, value, oldValue: this.get(key) });
|
|
157
|
+
if (middlewareResult.cancelled) {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
value = middlewareResult.value !== void 0 ? middlewareResult.value : value;
|
|
161
|
+
}
|
|
162
|
+
let observable2 = this._state.get(key);
|
|
163
|
+
if (!observable2) {
|
|
164
|
+
observable2 = new Observable(value, config);
|
|
165
|
+
this._state.set(key, observable2);
|
|
166
|
+
} else {
|
|
167
|
+
if (config.enableHistory) {
|
|
168
|
+
this._addToHistory("set", key, observable2.value, value);
|
|
169
|
+
}
|
|
170
|
+
observable2.value = value;
|
|
171
|
+
}
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Check if state has a key
|
|
176
|
+
*/
|
|
177
|
+
has(key) {
|
|
178
|
+
return this._state.has(key);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Delete state key
|
|
182
|
+
*/
|
|
183
|
+
delete(key) {
|
|
184
|
+
const observable2 = this._state.get(key);
|
|
185
|
+
if (observable2) {
|
|
186
|
+
if (this._options.enableHistory) {
|
|
187
|
+
this._addToHistory("delete", key, observable2.value, void 0);
|
|
188
|
+
}
|
|
189
|
+
observable2.unwatchAll();
|
|
190
|
+
this._state.delete(key);
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Clear all state
|
|
197
|
+
*/
|
|
198
|
+
clear() {
|
|
199
|
+
if (this._options.enableHistory) {
|
|
200
|
+
this._addToHistory("clear", null, this.toObject(), {});
|
|
201
|
+
}
|
|
202
|
+
for (const observable2 of this._state.values()) {
|
|
203
|
+
observable2.unwatchAll();
|
|
204
|
+
}
|
|
205
|
+
this._state.clear();
|
|
206
|
+
this._computed.clear();
|
|
207
|
+
this._watchers.clear();
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Create computed property
|
|
211
|
+
*/
|
|
212
|
+
computed(key, getter, options = {}) {
|
|
213
|
+
if (typeof getter !== "function") {
|
|
214
|
+
throw new StateError(`Computed property '${key}' getter must be a function`);
|
|
215
|
+
}
|
|
216
|
+
const computed2 = new Computed(getter, { ...this._options, ...options });
|
|
217
|
+
this._computed.set(key, computed2);
|
|
218
|
+
return computed2;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get computed property value
|
|
222
|
+
*/
|
|
223
|
+
getComputed(key) {
|
|
224
|
+
const computed2 = this._computed.get(key);
|
|
225
|
+
return computed2 ? computed2.value : void 0;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Watch state changes
|
|
229
|
+
*/
|
|
230
|
+
watch(key, callback, options = {}) {
|
|
231
|
+
if (typeof key === "function") {
|
|
232
|
+
return this._watchComputed(key, callback, options);
|
|
233
|
+
}
|
|
234
|
+
const observable2 = this._state.get(key);
|
|
235
|
+
if (!observable2) {
|
|
236
|
+
throw new StateError(`Cannot watch undefined state key: ${key}`);
|
|
237
|
+
}
|
|
238
|
+
const unwatch = observable2.watch(callback, options);
|
|
239
|
+
if (!this._watchers.has(key)) {
|
|
240
|
+
this._watchers.set(key, /* @__PURE__ */ new Set());
|
|
241
|
+
}
|
|
242
|
+
this._watchers.get(key).add(unwatch);
|
|
243
|
+
return unwatch;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Watch computed expression
|
|
247
|
+
*/
|
|
248
|
+
_watchComputed(expression, callback, options = {}) {
|
|
249
|
+
const computed2 = new Computed(expression, options);
|
|
250
|
+
const unwatch = computed2.watch(callback, options);
|
|
251
|
+
return unwatch;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Batch state updates
|
|
255
|
+
*/
|
|
256
|
+
batch(updates) {
|
|
257
|
+
if (typeof updates === "function") {
|
|
258
|
+
const oldEnableHistory = this._options.enableHistory;
|
|
259
|
+
this._options.enableHistory = false;
|
|
260
|
+
try {
|
|
261
|
+
const result = updates(this);
|
|
262
|
+
if (oldEnableHistory) {
|
|
263
|
+
this._addToHistory("batch", null, null, this.toObject());
|
|
264
|
+
}
|
|
265
|
+
return result;
|
|
266
|
+
} finally {
|
|
267
|
+
this._options.enableHistory = oldEnableHistory;
|
|
268
|
+
}
|
|
269
|
+
} else if (typeof updates === "object") {
|
|
270
|
+
return this.batch(() => {
|
|
271
|
+
Object.entries(updates).forEach(([key, value]) => {
|
|
272
|
+
this.set(key, value);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Subscribe to multiple state changes
|
|
279
|
+
*/
|
|
280
|
+
subscribe(keys, callback, options = {}) {
|
|
281
|
+
if (!Array.isArray(keys)) {
|
|
282
|
+
keys = [keys];
|
|
283
|
+
}
|
|
284
|
+
const unwatchers = keys.map((key) => {
|
|
285
|
+
return this.watch(key, (newValue, oldValue) => {
|
|
286
|
+
callback({
|
|
287
|
+
key,
|
|
288
|
+
newValue,
|
|
289
|
+
oldValue,
|
|
290
|
+
state: this.toObject()
|
|
291
|
+
});
|
|
292
|
+
}, options);
|
|
293
|
+
});
|
|
294
|
+
return () => {
|
|
295
|
+
unwatchers.forEach((unwatch) => unwatch());
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Add middleware for state changes
|
|
300
|
+
*/
|
|
301
|
+
use(middleware) {
|
|
302
|
+
if (typeof middleware !== "function") {
|
|
303
|
+
throw new StateError("Middleware must be a function");
|
|
304
|
+
}
|
|
305
|
+
this._middleware.push(middleware);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Run middleware chain
|
|
309
|
+
*/
|
|
310
|
+
_runMiddleware(action, context) {
|
|
311
|
+
let result = { ...context, cancelled: false };
|
|
312
|
+
for (const middleware of this._middleware) {
|
|
313
|
+
try {
|
|
314
|
+
const middlewareResult = middleware(action, result);
|
|
315
|
+
if (middlewareResult) {
|
|
316
|
+
result = { ...result, ...middlewareResult };
|
|
317
|
+
if (result.cancelled) {
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
} catch (_error) {
|
|
322
|
+
globalErrorHandler.handle(_error, {
|
|
323
|
+
type: "middleware-_error",
|
|
324
|
+
context: { action, middleware: middleware.toString() }
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Add action to history
|
|
332
|
+
*/
|
|
333
|
+
_addToHistory(action, key, oldValue, newValue) {
|
|
334
|
+
if (!this._options.enableHistory) return;
|
|
335
|
+
this._history.unshift({
|
|
336
|
+
action,
|
|
337
|
+
key,
|
|
338
|
+
oldValue,
|
|
339
|
+
newValue,
|
|
340
|
+
timestamp: Date.now()
|
|
341
|
+
});
|
|
342
|
+
if (this._history.length > this._options.maxHistorySize) {
|
|
343
|
+
this._history = this._history.slice(0, this._options.maxHistorySize);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Get state history
|
|
348
|
+
*/
|
|
349
|
+
getHistory(limit = 10) {
|
|
350
|
+
return this._history.slice(0, limit);
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Undo last action
|
|
354
|
+
*/
|
|
355
|
+
undo() {
|
|
356
|
+
const lastAction = this._history.shift();
|
|
357
|
+
if (!lastAction) return false;
|
|
358
|
+
const { action, key, oldValue } = lastAction;
|
|
359
|
+
const oldEnableHistory = this._options.enableHistory;
|
|
360
|
+
this._options.enableHistory = false;
|
|
361
|
+
try {
|
|
362
|
+
switch (action) {
|
|
363
|
+
case "set":
|
|
364
|
+
if (oldValue === void 0) {
|
|
365
|
+
this.delete(key);
|
|
366
|
+
} else {
|
|
367
|
+
this.set(key, oldValue);
|
|
368
|
+
}
|
|
369
|
+
break;
|
|
370
|
+
case "delete":
|
|
371
|
+
this.set(key, oldValue);
|
|
372
|
+
break;
|
|
373
|
+
case "clear":
|
|
374
|
+
this.clear();
|
|
375
|
+
Object.entries(oldValue || {}).forEach(([k, v]) => {
|
|
376
|
+
this.set(k, v);
|
|
377
|
+
});
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
380
|
+
return true;
|
|
381
|
+
} finally {
|
|
382
|
+
this._options.enableHistory = oldEnableHistory;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Convert state to plain object
|
|
387
|
+
*/
|
|
388
|
+
toObject() {
|
|
389
|
+
const result = {};
|
|
390
|
+
for (const [key, observable2] of this._state.entries()) {
|
|
391
|
+
result[key] = observable2.value;
|
|
392
|
+
}
|
|
393
|
+
return result;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Convert computed properties to object
|
|
397
|
+
*/
|
|
398
|
+
getComputedValues() {
|
|
399
|
+
const result = {};
|
|
400
|
+
for (const [key, computed2] of this._computed.entries()) {
|
|
401
|
+
result[key] = computed2.value;
|
|
402
|
+
}
|
|
403
|
+
return result;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Get state statistics
|
|
407
|
+
*/
|
|
408
|
+
getStats() {
|
|
409
|
+
return {
|
|
410
|
+
stateKeys: this._state.size,
|
|
411
|
+
computedKeys: this._computed.size,
|
|
412
|
+
watcherKeys: this._watchers.size,
|
|
413
|
+
historyLength: this._history.length,
|
|
414
|
+
middlewareCount: this._middleware.length
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Cleanup and destroy
|
|
419
|
+
*/
|
|
420
|
+
destroy() {
|
|
421
|
+
for (const observable2 of this._state.values()) {
|
|
422
|
+
observable2.unwatchAll();
|
|
423
|
+
}
|
|
424
|
+
for (const computed2 of this._computed.values()) {
|
|
425
|
+
computed2.unwatchAll();
|
|
426
|
+
}
|
|
427
|
+
this._state.clear();
|
|
428
|
+
this._computed.clear();
|
|
429
|
+
this._watchers.clear();
|
|
430
|
+
this._middleware.length = 0;
|
|
431
|
+
this._history.length = 0;
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
function createReactiveState(initialState, options = {}) {
|
|
435
|
+
return new ReactiveState(initialState, options);
|
|
436
|
+
}
|
|
437
|
+
function observable(value, options = {}) {
|
|
438
|
+
return new Observable(value, options);
|
|
439
|
+
}
|
|
440
|
+
function computed(getter, options = {}) {
|
|
441
|
+
return new Computed(getter, options);
|
|
442
|
+
}
|
|
443
|
+
var stateUtils = {
|
|
444
|
+
/**
|
|
445
|
+
* Create a toggle state
|
|
446
|
+
*/
|
|
447
|
+
toggle(initialValue = false) {
|
|
448
|
+
const obs = observable(initialValue);
|
|
449
|
+
obs.toggle = () => {
|
|
450
|
+
obs.value = !obs.value;
|
|
451
|
+
};
|
|
452
|
+
return obs;
|
|
453
|
+
},
|
|
454
|
+
/**
|
|
455
|
+
* Create a counter state
|
|
456
|
+
*/
|
|
457
|
+
counter(initialValue = 0) {
|
|
458
|
+
const obs = observable(initialValue);
|
|
459
|
+
obs.increment = (by = 1) => {
|
|
460
|
+
obs.value += by;
|
|
461
|
+
};
|
|
462
|
+
obs.decrement = (by = 1) => {
|
|
463
|
+
obs.value -= by;
|
|
464
|
+
};
|
|
465
|
+
obs.reset = () => {
|
|
466
|
+
obs.value = initialValue;
|
|
467
|
+
};
|
|
468
|
+
return obs;
|
|
469
|
+
},
|
|
470
|
+
/**
|
|
471
|
+
* Create an array state with utilities
|
|
472
|
+
*/
|
|
473
|
+
array(initialArray = []) {
|
|
474
|
+
const obs = observable([...initialArray]);
|
|
475
|
+
obs.push = (...items) => {
|
|
476
|
+
obs.value = [...obs.value, ...items];
|
|
477
|
+
};
|
|
478
|
+
obs.pop = () => {
|
|
479
|
+
const newArray = [...obs.value];
|
|
480
|
+
const result = newArray.pop();
|
|
481
|
+
obs.value = newArray;
|
|
482
|
+
return result;
|
|
483
|
+
};
|
|
484
|
+
obs.filter = (predicate) => {
|
|
485
|
+
obs.value = obs.value.filter(predicate);
|
|
486
|
+
};
|
|
487
|
+
obs.clear = () => {
|
|
488
|
+
obs.value = [];
|
|
489
|
+
};
|
|
490
|
+
return obs;
|
|
491
|
+
},
|
|
492
|
+
/**
|
|
493
|
+
* Create object state with deep reactivity
|
|
494
|
+
*/
|
|
495
|
+
object(initialObject = {}) {
|
|
496
|
+
const state = createReactiveState(initialObject, { deep: true });
|
|
497
|
+
return state;
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
var reactive_state_default = ReactiveState;
|
|
501
|
+
export {
|
|
502
|
+
Observable,
|
|
503
|
+
ReactiveState,
|
|
504
|
+
StateError,
|
|
505
|
+
computed,
|
|
506
|
+
createReactiveState,
|
|
507
|
+
reactive_state_default as default,
|
|
508
|
+
globalErrorHandler,
|
|
509
|
+
observable,
|
|
510
|
+
stateUtils
|
|
511
|
+
};
|
|
512
|
+
//# sourceMappingURL=reactive-state.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/reactive-state.js"],
|
|
4
|
+
"sourcesContent": ["/**\n * Reactive State Management System for Coherent.js\n * Provides computed properties, watchers, and reactive updates\n */\n\n// Simple error handling for this module\nexport class StateError extends Error {\n constructor(message, options = {}) {\n super(message);\n this.name = 'StateError';\n this.type = options.type || 'state';\n this.component = options.component;\n this.context = options.context;\n this.timestamp = Date.now();\n }\n}\n\nexport const globalErrorHandler = {\n handle(error, context = {}) {\n console.error('State Error:', error.message, context);\n }\n};\n\n/**\n * Observable wrapper for tracking state changes\n */\nexport class Observable {\n constructor(value, options = {}) {\n this._value = value;\n this._observers = new Set();\n this._computedDependents = new Set();\n this._options = {\n deep: options.deep !== false,\n immediate: options.immediate !== false,\n ...options\n };\n }\n\n get value() {\n // Track dependency for computed properties\n if (Observable._currentComputed) {\n this._computedDependents.add(Observable._currentComputed);\n }\n return this._value;\n }\n\n set value(newValue) {\n if (this._value === newValue && !this._options.deep) {\n return;\n }\n\n const oldValue = this._value;\n this._value = newValue;\n\n // Notify observers\n this._observers.forEach(observer => {\n try {\n observer(newValue, oldValue);\n } catch (_error) {\n globalErrorHandler.handle(_error, {\n type: 'watcher-_error',\n context: { newValue, oldValue }\n });\n }\n });\n\n // Update computed dependents\n this._computedDependents.forEach(computed => {\n computed._invalidate();\n });\n }\n\n watch(callback, options = {}) {\n if (typeof callback !== 'function') {\n throw new StateError('Watch callback must be a function');\n }\n\n const observer = (newValue, oldValue) => {\n callback(newValue, oldValue, () => this.unwatch(observer));\n };\n\n this._observers.add(observer);\n\n // Call immediately if requested\n if (options.immediate !== false) {\n observer(this._value, undefined);\n }\n\n // Return unwatch function\n return () => this.unwatch(observer);\n }\n\n unwatch(observer) {\n this._observers.delete(observer);\n }\n\n unwatchAll() {\n this._observers.clear();\n this._computedDependents.clear();\n }\n}\n\n/**\n * Computed property implementation\n */\nclass Computed extends Observable {\n constructor(getter, options = {}) {\n super(undefined, options);\n this._getter = getter;\n this._cached = false;\n this._dirty = true;\n\n if (typeof getter !== 'function') {\n throw new StateError('Computed getter must be a function');\n }\n }\n\n get value() {\n if (this._dirty || !this._cached) {\n this._compute();\n }\n return this._value;\n }\n\n set value(newValue) {\n throw new StateError('Cannot set value on computed property');\n }\n\n _compute() {\n const prevComputed = Observable._currentComputed;\n Observable._currentComputed = this;\n\n try {\n const newValue = this._getter();\n\n if (newValue !== this._value) {\n const oldValue = this._value;\n this._value = newValue;\n\n // Notify observers\n this._observers.forEach(observer => {\n observer(newValue, oldValue);\n });\n }\n\n this._cached = true;\n this._dirty = false;\n } catch (_error) {\n globalErrorHandler.handle(_error, {\n type: 'computed-_error',\n context: { getter: this._getter.toString() }\n });\n } finally {\n Observable._currentComputed = prevComputed;\n }\n }\n\n _invalidate() {\n this._dirty = true;\n this._computedDependents.forEach(computed => {\n computed._invalidate();\n });\n }\n}\n\n// Static property for tracking current computed\nObservable._currentComputed = null;\n\n/**\n * Reactive state container with advanced features\n */\nexport class ReactiveState {\n constructor(initialState = {}, options = {}) {\n this._state = new Map();\n this._computed = new Map();\n this._watchers = new Map();\n this._middleware = [];\n this._history = [];\n this._options = {\n enableHistory: options.enableHistory !== false,\n maxHistorySize: options.maxHistorySize || 50,\n enableMiddleware: options.enableMiddleware !== false,\n deep: options.deep !== false,\n ...options\n };\n\n // Initialize state\n Object.entries(initialState).forEach(([key, value]) => {\n this.set(key, value);\n });\n }\n\n /**\n * Get reactive state value\n */\n get(key) {\n const observable = this._state.get(key);\n return observable ? observable.value : undefined;\n }\n\n /**\n * Set reactive state value\n */\n set(key, value, options = {}) {\n const config = { ...this._options, ...options };\n\n // Run middleware\n if (config.enableMiddleware) {\n const middlewareResult = this._runMiddleware('set', { key, value, oldValue: this.get(key) });\n if (middlewareResult.cancelled) {\n return false;\n }\n value = middlewareResult.value !== undefined ? middlewareResult.value : value;\n }\n\n // Get or create observable\n let observable = this._state.get(key);\n if (!observable) {\n observable = new Observable(value, config);\n this._state.set(key, observable);\n } else {\n // Record history\n if (config.enableHistory) {\n this._addToHistory('set', key, observable.value, value);\n }\n\n observable.value = value;\n }\n\n return true;\n }\n\n /**\n * Check if state has a key\n */\n has(key) {\n return this._state.has(key);\n }\n\n /**\n * Delete state key\n */\n delete(key) {\n const observable = this._state.get(key);\n if (observable) {\n // Record history\n if (this._options.enableHistory) {\n this._addToHistory('delete', key, observable.value, undefined);\n }\n\n observable.unwatchAll();\n this._state.delete(key);\n return true;\n }\n return false;\n }\n\n /**\n * Clear all state\n */\n clear() {\n // Record history\n if (this._options.enableHistory) {\n this._addToHistory('clear', null, this.toObject(), {});\n }\n\n // Cleanup observables\n for (const observable of this._state.values()) {\n observable.unwatchAll();\n }\n\n this._state.clear();\n this._computed.clear();\n this._watchers.clear();\n }\n\n /**\n * Create computed property\n */\n computed(key, getter, options = {}) {\n if (typeof getter !== 'function') {\n throw new StateError(`Computed property '${key}' getter must be a function`);\n }\n\n const computed = new Computed(getter, { ...this._options, ...options });\n this._computed.set(key, computed);\n\n return computed;\n }\n\n /**\n * Get computed property value\n */\n getComputed(key) {\n const computed = this._computed.get(key);\n return computed ? computed.value : undefined;\n }\n\n /**\n * Watch state changes\n */\n watch(key, callback, options = {}) {\n if (typeof key === 'function') {\n // Watch computed expression\n return this._watchComputed(key, callback, options);\n }\n\n const observable = this._state.get(key);\n if (!observable) {\n throw new StateError(`Cannot watch undefined state key: ${key}`);\n }\n\n const unwatch = observable.watch(callback, options);\n\n // Store watcher for cleanup\n if (!this._watchers.has(key)) {\n this._watchers.set(key, new Set());\n }\n this._watchers.get(key).add(unwatch);\n\n return unwatch;\n }\n\n /**\n * Watch computed expression\n */\n _watchComputed(expression, callback, options = {}) {\n const computed = new Computed(expression, options);\n const unwatch = computed.watch(callback, options);\n\n return unwatch;\n }\n\n /**\n * Batch state updates\n */\n batch(updates) {\n if (typeof updates === 'function') {\n // Batch function updates\n const oldEnableHistory = this._options.enableHistory;\n this._options.enableHistory = false;\n\n try {\n const result = updates(this);\n\n // Record batch in history\n if (oldEnableHistory) {\n this._addToHistory('batch', null, null, this.toObject());\n }\n\n return result;\n } finally {\n this._options.enableHistory = oldEnableHistory;\n }\n } else if (typeof updates === 'object') {\n // Batch object updates\n return this.batch(() => {\n Object.entries(updates).forEach(([key, value]) => {\n this.set(key, value);\n });\n });\n }\n }\n\n /**\n * Subscribe to multiple state changes\n */\n subscribe(keys, callback, options = {}) {\n if (!Array.isArray(keys)) {\n keys = [keys];\n }\n\n const unwatchers = keys.map(key => {\n return this.watch(key, (newValue, oldValue) => {\n callback({\n key,\n newValue,\n oldValue,\n state: this.toObject()\n });\n }, options);\n });\n\n // Return unsubscribe function\n return () => {\n unwatchers.forEach(unwatch => unwatch());\n };\n }\n\n /**\n * Add middleware for state changes\n */\n use(middleware) {\n if (typeof middleware !== 'function') {\n throw new StateError('Middleware must be a function');\n }\n this._middleware.push(middleware);\n }\n\n /**\n * Run middleware chain\n */\n _runMiddleware(action, context) {\n let result = { ...context, cancelled: false };\n\n for (const middleware of this._middleware) {\n try {\n const middlewareResult = middleware(action, result);\n if (middlewareResult) {\n result = { ...result, ...middlewareResult };\n if (result.cancelled) {\n break;\n }\n }\n } catch (_error) {\n globalErrorHandler.handle(_error, {\n type: 'middleware-_error',\n context: { action, middleware: middleware.toString() }\n });\n }\n }\n\n return result;\n }\n\n /**\n * Add action to history\n */\n _addToHistory(action, key, oldValue, newValue) {\n if (!this._options.enableHistory) return;\n\n this._history.unshift({\n action,\n key,\n oldValue,\n newValue,\n timestamp: Date.now()\n });\n\n // Limit history size\n if (this._history.length > this._options.maxHistorySize) {\n this._history = this._history.slice(0, this._options.maxHistorySize);\n }\n }\n\n /**\n * Get state history\n */\n getHistory(limit = 10) {\n return this._history.slice(0, limit);\n }\n\n /**\n * Undo last action\n */\n undo() {\n const lastAction = this._history.shift();\n if (!lastAction) return false;\n\n const { action, key, oldValue } = lastAction;\n\n // Temporarily disable history\n const oldEnableHistory = this._options.enableHistory;\n this._options.enableHistory = false;\n\n try {\n switch (action) {\n case 'set':\n if (oldValue === undefined) {\n this.delete(key);\n } else {\n this.set(key, oldValue);\n }\n break;\n case 'delete':\n this.set(key, oldValue);\n break;\n case 'clear':\n this.clear();\n Object.entries(oldValue || {}).forEach(([k, v]) => {\n this.set(k, v);\n });\n break;\n }\n return true;\n } finally {\n this._options.enableHistory = oldEnableHistory;\n }\n }\n\n /**\n * Convert state to plain object\n */\n toObject() {\n const result = {};\n for (const [key, observable] of this._state.entries()) {\n result[key] = observable.value;\n }\n return result;\n }\n\n /**\n * Convert computed properties to object\n */\n getComputedValues() {\n const result = {};\n for (const [key, computed] of this._computed.entries()) {\n result[key] = computed.value;\n }\n return result;\n }\n\n /**\n * Get state statistics\n */\n getStats() {\n return {\n stateKeys: this._state.size,\n computedKeys: this._computed.size,\n watcherKeys: this._watchers.size,\n historyLength: this._history.length,\n middlewareCount: this._middleware.length\n };\n }\n\n /**\n * Cleanup and destroy\n */\n destroy() {\n // Clear all watchers\n for (const observable of this._state.values()) {\n observable.unwatchAll();\n }\n for (const computed of this._computed.values()) {\n computed.unwatchAll();\n }\n\n // Clear collections\n this._state.clear();\n this._computed.clear();\n this._watchers.clear();\n this._middleware.length = 0;\n this._history.length = 0;\n }\n}\n\n/**\n * Create reactive state store\n */\nexport function createReactiveState(initialState, options = {}) {\n return new ReactiveState(initialState, options);\n}\n\n/**\n * Create observable value\n */\nexport function observable(value, options = {}) {\n return new Observable(value, options);\n}\n\n/**\n * Create computed property\n */\nexport function computed(getter, options = {}) {\n return new Computed(getter, options);\n}\n\n/**\n * Utility functions for common state patterns\n */\nexport const stateUtils = {\n /**\n * Create a toggle state\n */\n toggle(initialValue = false) {\n const obs = observable(initialValue);\n obs.toggle = () => {\n obs.value = !obs.value;\n };\n return obs;\n },\n\n /**\n * Create a counter state\n */\n counter(initialValue = 0) {\n const obs = observable(initialValue);\n obs.increment = (by = 1) => {\n obs.value += by;\n };\n obs.decrement = (by = 1) => {\n obs.value -= by;\n };\n obs.reset = () => {\n obs.value = initialValue;\n };\n return obs;\n },\n\n /**\n * Create an array state with utilities\n */\n array(initialArray = []) {\n const obs = observable([...initialArray]);\n obs.push = (...items) => {\n obs.value = [...obs.value, ...items];\n };\n obs.pop = () => {\n const newArray = [...obs.value];\n const result = newArray.pop();\n obs.value = newArray;\n return result;\n };\n obs.filter = (predicate) => {\n obs.value = obs.value.filter(predicate);\n };\n obs.clear = () => {\n obs.value = [];\n };\n return obs;\n },\n\n /**\n * Create object state with deep reactivity\n */\n object(initialObject = {}) {\n const state = createReactiveState(initialObject, { deep: true });\n return state;\n }\n};\n\nexport default ReactiveState;\n"],
|
|
5
|
+
"mappings": ";AAMO,IAAM,aAAN,cAAyB,MAAM;AAAA,EAClC,YAAY,SAAS,UAAU,CAAC,GAAG;AAC/B,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,OAAO,QAAQ,QAAQ;AAC5B,SAAK,YAAY,QAAQ;AACzB,SAAK,UAAU,QAAQ;AACvB,SAAK,YAAY,KAAK,IAAI;AAAA,EAC9B;AACJ;AAEO,IAAM,qBAAqB;AAAA,EAC9B,OAAO,OAAO,UAAU,CAAC,GAAG;AACxB,YAAQ,MAAM,gBAAgB,MAAM,SAAS,OAAO;AAAA,EACxD;AACJ;AAKO,IAAM,aAAN,MAAM,YAAW;AAAA,EACpB,YAAY,OAAO,UAAU,CAAC,GAAG;AAC7B,SAAK,SAAS;AACd,SAAK,aAAa,oBAAI,IAAI;AAC1B,SAAK,sBAAsB,oBAAI,IAAI;AACnC,SAAK,WAAW;AAAA,MACZ,MAAM,QAAQ,SAAS;AAAA,MACvB,WAAW,QAAQ,cAAc;AAAA,MACjC,GAAG;AAAA,IACP;AAAA,EACJ;AAAA,EAEA,IAAI,QAAQ;AAER,QAAI,YAAW,kBAAkB;AAC7B,WAAK,oBAAoB,IAAI,YAAW,gBAAgB;AAAA,IAC5D;AACA,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,IAAI,MAAM,UAAU;AAChB,QAAI,KAAK,WAAW,YAAY,CAAC,KAAK,SAAS,MAAM;AACjD;AAAA,IACJ;AAEA,UAAM,WAAW,KAAK;AACtB,SAAK,SAAS;AAGd,SAAK,WAAW,QAAQ,cAAY;AAChC,UAAI;AACA,iBAAS,UAAU,QAAQ;AAAA,MAC/B,SAAS,QAAQ;AACb,2BAAmB,OAAO,QAAQ;AAAA,UAC9B,MAAM;AAAA,UACN,SAAS,EAAE,UAAU,SAAS;AAAA,QAClC,CAAC;AAAA,MACL;A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;AACA,WAAO;AAAA,EACX;AAAA;AAAA;AAAA;AAAA,EAKA,OAAO,gBAAgB,CAAC,GAAG;AACvB,UAAM,QAAQ,oBAAoB,eAAe,EAAE,MAAM,KAAK,CAAC;AAC/D,WAAO;AAAA,EACX;AACJ;AAEA,IAAO,yBAAQ;",
|
|
6
|
+
"names": ["computed", "observable"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// src/state-manager.js
|
|
2
|
+
var globalState = /* @__PURE__ */ new Map();
|
|
3
|
+
function createState(initialState = {}) {
|
|
4
|
+
const state = new Map(Object.entries(initialState));
|
|
5
|
+
return {
|
|
6
|
+
get(key) {
|
|
7
|
+
return state.get(key);
|
|
8
|
+
},
|
|
9
|
+
set(key, value) {
|
|
10
|
+
state.set(key, value);
|
|
11
|
+
return this;
|
|
12
|
+
},
|
|
13
|
+
has(key) {
|
|
14
|
+
return state.has(key);
|
|
15
|
+
},
|
|
16
|
+
delete(key) {
|
|
17
|
+
return state.delete(key);
|
|
18
|
+
},
|
|
19
|
+
clear() {
|
|
20
|
+
state.clear();
|
|
21
|
+
return this;
|
|
22
|
+
},
|
|
23
|
+
toObject() {
|
|
24
|
+
return Object.fromEntries(state);
|
|
25
|
+
},
|
|
26
|
+
// For debugging
|
|
27
|
+
_internal: state
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
var globalStateManager = {
|
|
31
|
+
set(key, value) {
|
|
32
|
+
globalState.set(key, value);
|
|
33
|
+
},
|
|
34
|
+
get(key) {
|
|
35
|
+
return globalState.get(key);
|
|
36
|
+
},
|
|
37
|
+
has(key) {
|
|
38
|
+
return globalState.has(key);
|
|
39
|
+
},
|
|
40
|
+
clear() {
|
|
41
|
+
globalState.clear();
|
|
42
|
+
},
|
|
43
|
+
// Create isolated state for each request
|
|
44
|
+
createRequestState() {
|
|
45
|
+
return createState();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var contextStacks = /* @__PURE__ */ new Map();
|
|
49
|
+
function provideContext(key, value) {
|
|
50
|
+
if (!contextStacks.has(key)) {
|
|
51
|
+
contextStacks.set(key, []);
|
|
52
|
+
}
|
|
53
|
+
const stack = contextStacks.get(key);
|
|
54
|
+
const previousValue = globalState.get(key);
|
|
55
|
+
stack.push(previousValue);
|
|
56
|
+
globalState.set(key, value);
|
|
57
|
+
}
|
|
58
|
+
function createContextProvider(key, value, children) {
|
|
59
|
+
return (renderFunction) => {
|
|
60
|
+
try {
|
|
61
|
+
provideContext(key, value);
|
|
62
|
+
if (renderFunction && typeof renderFunction === "function") {
|
|
63
|
+
return renderFunction(children);
|
|
64
|
+
} else {
|
|
65
|
+
return children;
|
|
66
|
+
}
|
|
67
|
+
} finally {
|
|
68
|
+
restoreContext(key);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function restoreContext(key) {
|
|
73
|
+
if (!contextStacks.has(key)) return;
|
|
74
|
+
const stack = contextStacks.get(key);
|
|
75
|
+
const previousValue = stack.pop();
|
|
76
|
+
if (stack.length === 0) {
|
|
77
|
+
if (previousValue === void 0) {
|
|
78
|
+
globalState.delete(key);
|
|
79
|
+
} else {
|
|
80
|
+
globalState.set(key, previousValue);
|
|
81
|
+
}
|
|
82
|
+
contextStacks.delete(key);
|
|
83
|
+
} else {
|
|
84
|
+
globalState.set(key, previousValue);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function clearAllContexts() {
|
|
88
|
+
contextStacks.clear();
|
|
89
|
+
}
|
|
90
|
+
function useContext(key) {
|
|
91
|
+
return globalState.get(key);
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
clearAllContexts,
|
|
95
|
+
createContextProvider,
|
|
96
|
+
createState,
|
|
97
|
+
globalStateManager,
|
|
98
|
+
provideContext,
|
|
99
|
+
restoreContext,
|
|
100
|
+
useContext
|
|
101
|
+
};
|
|
102
|
+
//# sourceMappingURL=state-manager.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/state-manager.js"],
|
|
4
|
+
"sourcesContent": ["/**\n * Simple state management for server-side rendering\n * This is mainly for component state during rendering\n */\n\nconst globalState = new Map();\n\n/**\n * Creates a state container for a request/render cycle\n * @param {Object} initialState - Initial state object\n * @returns {Object} State container\n */\nexport function createState(initialState = {}) {\n const state = new Map(Object.entries(initialState));\n\n return {\n get(key) {\n return state.get(key);\n },\n\n set(key, value) {\n state.set(key, value);\n return this;\n },\n\n has(key) {\n return state.has(key);\n },\n\n delete(key) {\n return state.delete(key);\n },\n\n clear() {\n state.clear();\n return this;\n },\n\n toObject() {\n return Object.fromEntries(state);\n },\n\n // For debugging\n _internal: state\n };\n}\n\n/**\n * Global state for sharing data across components during SSR\n */\nexport const globalStateManager = {\n set(key, value) {\n globalState.set(key, value);\n },\n\n get(key) {\n return globalState.get(key);\n },\n\n has(key) {\n return globalState.has(key);\n },\n\n clear() {\n globalState.clear();\n },\n\n // Create isolated state for each request\n createRequestState() {\n return createState();\n }\n};\n\n/**\n * Context stack for managing nested context providers\n */\nconst contextStacks = new Map();\n\n/**\n * Context provider for passing data down the component tree\n * @param {string} key - Context key\n * @param {*} value - Context value\n * @param {Object} children - Children to render with context\n * @returns {Object} Children with context available\n */\nexport function provideContext(key, value) {\n // Initialize context stack if it doesn't exist\n if (!contextStacks.has(key)) {\n contextStacks.set(key, []);\n }\n \n const stack = contextStacks.get(key);\n \n // Store previous value\n const previousValue = globalState.get(key);\n \n // Push previous value to stack and set new value\n stack.push(previousValue);\n globalState.set(key, value);\n}\n\n/**\n * Create a context provider component that works with the rendering system\n * @param {string} key - Context key\n * @param {*} value - Context value\n * @param {Object} children - Children to render with context\n * @returns {Function} Component function that provides context\n */\nexport function createContextProvider(key, value, children) {\n // Return a function that will render the children within the context\n return (renderFunction) => {\n try {\n // Provide context\n provideContext(key, value);\n \n // If a render function is provided, use it to render children\n // Otherwise return children to be rendered by the caller\n if (renderFunction && typeof renderFunction === 'function') {\n return renderFunction(children);\n } else {\n return children;\n }\n } finally {\n // Always restore context when done\n restoreContext(key);\n }\n };\n}\n\n/**\n * Restore context to previous value\n * @param {string} key - Context key\n */\nexport function restoreContext(key) {\n if (!contextStacks.has(key)) return;\n \n const stack = contextStacks.get(key);\n \n // Restore previous value from stack\n const previousValue = stack.pop();\n \n if (stack.length === 0) {\n // No more providers, delete the key if it was undefined before\n if (previousValue === undefined) {\n globalState.delete(key);\n } else {\n globalState.set(key, previousValue);\n }\n \n // Clean up empty stack\n contextStacks.delete(key);\n } else {\n // Restore previous value\n globalState.set(key, previousValue);\n }\n}\n\n/**\n * Clear all context stacks (useful for cleanup after rendering)\n */\nexport function clearAllContexts() {\n contextStacks.clear();\n // Note: This doesn't clear globalState as it might contain other data\n}\n\n/**\n * Context consumer to access provided context\n * @param {string} key - Context key\n * @returns {*} Context value\n */\nexport function useContext(key) {\n return globalState.get(key);\n}\n"],
|
|
5
|
+
"mappings": ";AAKA,IAAM,cAAc,oBAAI,IAAI;AAOrB,SAAS,YAAY,eAAe,CAAC,GAAG;AAC3C,QAAM,QAAQ,IAAI,IAAI,OAAO,QAAQ,YAAY,CAAC;AAElD,SAAO;AAAA,IACH,IAAI,KAAK;AACL,aAAO,MAAM,IAAI,GAAG;AAAA,IACxB;AAAA,IAEA,IAAI,KAAK,OAAO;AACZ,YAAM,IAAI,KAAK,KAAK;AACpB,aAAO;AAAA,IACX;AAAA,IAEA,IAAI,KAAK;AACL,aAAO,MAAM,IAAI,GAAG;AAAA,IACxB;AAAA,IAEA,OAAO,KAAK;AACR,aAAO,MAAM,OAAO,GAAG;AAAA,IAC3B;AAAA,IAEA,QAAQ;AACJ,YAAM,MAAM;AACZ,aAAO;AAAA,IACX;AAAA,IAEA,WAAW;AACP,aAAO,OAAO,YAAY,KAAK;AAAA,IACnC;AAAA;AAAA,IAGA,WAAW;AAAA,EACf;AACJ;AAKO,IAAM,qBAAqB;AAAA,EAC9B,IAAI,KAAK,OAA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