@yiin/reactive-proxy-state 1.0.19 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.cjs +1612 -0
  2. package/package.json +7 -3
package/dist/index.cjs ADDED
@@ -0,0 +1,1612 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __moduleCache = /* @__PURE__ */ new WeakMap;
6
+ var __toCommonJS = (from) => {
7
+ var entry = __moduleCache.get(from), desc;
8
+ if (entry)
9
+ return entry;
10
+ entry = __defProp({}, "__esModule", { value: true });
11
+ if (from && typeof from === "object" || typeof from === "function")
12
+ __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ }));
16
+ __moduleCache.set(from, entry);
17
+ return entry;
18
+ };
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, {
22
+ get: all[name],
23
+ enumerable: true,
24
+ configurable: true,
25
+ set: (newValue) => all[name] = () => newValue
26
+ });
27
+ };
28
+
29
+ // src/index.ts
30
+ var exports_src = {};
31
+ __export(exports_src, {
32
+ wrapperCache: () => wrapperCache,
33
+ wrapSet: () => wrapSet,
34
+ wrapMap: () => wrapMap,
35
+ wrapArray: () => wrapArray,
36
+ watchEffect: () => watchEffect,
37
+ watch: () => watch,
38
+ updateState: () => updateState,
39
+ unref: () => unref,
40
+ triggerRef: () => triggerRef,
41
+ trigger: () => trigger,
42
+ traverse: () => traverse,
43
+ track: () => track,
44
+ toRefs: () => toRefs,
45
+ toRef: () => toRef,
46
+ toRaw: () => toRaw,
47
+ setPathConcat: () => setPathConcat,
48
+ setInPathCache: () => setInPathCache,
49
+ setActiveEffect: () => setActiveEffect,
50
+ runCleanupFunctions: () => runCleanupFunctions,
51
+ ref: () => ref,
52
+ reactive: () => reactive,
53
+ pathConcatCache: () => pathConcatCache,
54
+ pathCache: () => pathCache,
55
+ markRaw: () => markRaw,
56
+ isRefSymbol: () => isRefSymbol,
57
+ isRef: () => isRef,
58
+ isReactive: () => isReactive,
59
+ isComputed: () => isComputed,
60
+ globalSeen: () => globalSeen,
61
+ getPathConcat: () => getPathConcat,
62
+ getFromPathCache: () => getFromPathCache,
63
+ deepEqual: () => deepEqual,
64
+ deepClone: () => deepClone,
65
+ computed: () => computed,
66
+ cleanupEffect: () => cleanupEffect,
67
+ activeEffect: () => activeEffect,
68
+ ReactiveFlags: () => ReactiveFlags
69
+ });
70
+ module.exports = __toCommonJS(exports_src);
71
+
72
+ // src/utils.ts
73
+ var deepEqualCache = new WeakMap;
74
+ var MAX_CACHE_SIZE = 1000;
75
+ var pathCache = new WeakMap;
76
+ var pathCacheSize = new WeakMap;
77
+ var pathConcatCache = new Map;
78
+ var MAX_PATH_CACHE_SIZE = 1000;
79
+ var globalSeen = new WeakMap;
80
+ var wrapperCache = new WeakMap;
81
+ function cleanupPathCache(root) {
82
+ const cache = pathCache.get(root);
83
+ if (cache && pathCacheSize.get(root) > MAX_CACHE_SIZE) {
84
+ const entriesToRemove = Math.floor(MAX_CACHE_SIZE * 0.2);
85
+ let count = 0;
86
+ for (const key of cache.keys()) {
87
+ if (count >= entriesToRemove)
88
+ break;
89
+ cache.delete(key);
90
+ count++;
91
+ }
92
+ pathCacheSize.set(root, MAX_CACHE_SIZE - entriesToRemove);
93
+ }
94
+ }
95
+ function cleanupPathConcatCache() {
96
+ if (pathConcatCache.size > MAX_PATH_CACHE_SIZE) {
97
+ const entriesToRemove = Math.floor(MAX_PATH_CACHE_SIZE * 0.2);
98
+ let count = 0;
99
+ for (const key of pathConcatCache.keys()) {
100
+ if (count >= entriesToRemove)
101
+ break;
102
+ pathConcatCache.delete(key);
103
+ count++;
104
+ }
105
+ }
106
+ }
107
+ function deepEqual(a, b, seen = globalSeen) {
108
+ if (a === b)
109
+ return true;
110
+ if (a == null || b == null)
111
+ return a === b;
112
+ if (typeof a !== typeof b)
113
+ return false;
114
+ if (a instanceof Date && b instanceof Date)
115
+ return a.getTime() === b.getTime();
116
+ if (typeof a !== "object")
117
+ return false;
118
+ if (Array.isArray(a) !== Array.isArray(b))
119
+ return false;
120
+ if (seen.has(a))
121
+ return seen.get(a) === b;
122
+ seen.set(a, b);
123
+ if (deepEqualCache.has(a) && deepEqualCache.get(a)?.has(b)) {
124
+ return deepEqualCache.get(a).get(b);
125
+ }
126
+ if (!deepEqualCache.has(a)) {
127
+ deepEqualCache.set(a, new WeakMap);
128
+ }
129
+ let result;
130
+ if (Array.isArray(a)) {
131
+ result = a.length === b.length && a.every((val, idx) => deepEqual(val, b[idx], seen));
132
+ } else if (a instanceof Map && b instanceof Map) {
133
+ result = a.size === b.size;
134
+ if (result) {
135
+ for (const [key, value] of a) {
136
+ if (!b.has(key) || !deepEqual(value, b.get(key), seen)) {
137
+ result = false;
138
+ break;
139
+ }
140
+ }
141
+ }
142
+ } else if (a instanceof Set && b instanceof Set) {
143
+ result = a.size === b.size;
144
+ if (result) {
145
+ for (const value of a) {
146
+ if (!b.has(value)) {
147
+ result = false;
148
+ break;
149
+ }
150
+ }
151
+ }
152
+ } else {
153
+ const keysA = Object.keys(a);
154
+ const keysB = Object.keys(b);
155
+ result = keysA.length === keysB.length && keysA.every((key) => Object.prototype.hasOwnProperty.call(b, key) && deepEqual(a[key], b[key], seen));
156
+ }
157
+ deepEqualCache.get(a).set(b, result);
158
+ return result;
159
+ }
160
+ function getFromPathCache(root, pathKey) {
161
+ const cache = pathCache.get(root);
162
+ if (!cache)
163
+ return;
164
+ const result = cache.get(pathKey);
165
+ if (result !== undefined) {
166
+ cache.delete(pathKey);
167
+ cache.set(pathKey, result);
168
+ }
169
+ return result;
170
+ }
171
+ function setInPathCache(root, pathKey, value) {
172
+ if (!pathCache.has(root)) {
173
+ pathCache.set(root, new Map);
174
+ pathCacheSize.set(root, 0);
175
+ }
176
+ const cache = pathCache.get(root);
177
+ if (!cache.has(pathKey)) {
178
+ pathCacheSize.set(root, pathCacheSize.get(root) + 1);
179
+ } else {
180
+ cache.delete(pathKey);
181
+ }
182
+ cache.set(pathKey, value);
183
+ cleanupPathCache(root);
184
+ }
185
+ function getPathConcat(path) {
186
+ const result = pathConcatCache.get(path);
187
+ if (result !== undefined) {
188
+ pathConcatCache.delete(path);
189
+ pathConcatCache.set(path, result);
190
+ }
191
+ return result;
192
+ }
193
+ function setPathConcat(path, value) {
194
+ if (pathConcatCache.has(path)) {
195
+ pathConcatCache.delete(path);
196
+ }
197
+ pathConcatCache.set(path, value);
198
+ cleanupPathConcatCache();
199
+ }
200
+ function isObject(val) {
201
+ return val !== null && typeof val === "object";
202
+ }
203
+ function traverse(value, seen = new Set) {
204
+ if (!isObject(value) || seen.has(value)) {
205
+ return value;
206
+ }
207
+ seen.add(value);
208
+ if (Array.isArray(value)) {
209
+ value.length;
210
+ for (let i = 0;i < value.length; i++) {
211
+ traverse(value[i], seen);
212
+ }
213
+ } else if (value instanceof Set || value instanceof Map) {
214
+ const rawValue = value.__v_raw || value;
215
+ for (const v of rawValue) {
216
+ if (Array.isArray(v)) {
217
+ traverse(v[0], seen);
218
+ traverse(v[1], seen);
219
+ } else {
220
+ traverse(v, seen);
221
+ }
222
+ }
223
+ return value;
224
+ } else {
225
+ for (const key in value) {
226
+ traverse(value[key], seen);
227
+ }
228
+ }
229
+ return value;
230
+ }
231
+ function deepClone(value, seen = new WeakMap) {
232
+ if (value === null || typeof value !== "object") {
233
+ return value;
234
+ }
235
+ if (value instanceof Date) {
236
+ return new Date(value.getTime());
237
+ }
238
+ if (seen.has(value)) {
239
+ return seen.get(value);
240
+ }
241
+ if (Array.isArray(value)) {
242
+ const newArray = [];
243
+ seen.set(value, newArray);
244
+ for (let i = 0;i < value.length; i++) {
245
+ newArray[i] = deepClone(value[i], seen);
246
+ }
247
+ return newArray;
248
+ }
249
+ if (value instanceof Map) {
250
+ const newMap = new Map;
251
+ seen.set(value, newMap);
252
+ value.forEach((val, key) => {
253
+ newMap.set(deepClone(key, seen), deepClone(val, seen));
254
+ });
255
+ return newMap;
256
+ }
257
+ if (value instanceof Set) {
258
+ const newSet = new Set;
259
+ seen.set(value, newSet);
260
+ value.forEach((val) => {
261
+ newSet.add(deepClone(val, seen));
262
+ });
263
+ return newSet;
264
+ }
265
+ const newObject = Object.create(Object.getPrototypeOf(value));
266
+ seen.set(value, newObject);
267
+ for (const key in value) {
268
+ if (Object.prototype.hasOwnProperty.call(value, key)) {
269
+ newObject[key] = deepClone(value[key], seen);
270
+ }
271
+ }
272
+ const symbolKeys = Object.getOwnPropertySymbols(value);
273
+ for (const symbolKey of symbolKeys) {
274
+ const descriptor = Object.getOwnPropertyDescriptor(value, symbolKey);
275
+ if (descriptor && Object.prototype.propertyIsEnumerable.call(value, symbolKey)) {
276
+ newObject[symbolKey] = deepClone(value[symbolKey], seen);
277
+ }
278
+ }
279
+ return newObject;
280
+ }
281
+ // src/state.ts
282
+ function getValue(obj, key) {
283
+ if (obj instanceof Map)
284
+ return obj.get(key);
285
+ return obj[key];
286
+ }
287
+ function setValue(obj, key, value) {
288
+ if (obj instanceof Map)
289
+ obj.set(key, value);
290
+ else
291
+ obj[key] = value;
292
+ }
293
+ function deleteValue(obj, key) {
294
+ if (obj instanceof Map)
295
+ obj.delete(key);
296
+ else
297
+ delete obj[key];
298
+ }
299
+ var actionHandlers = {
300
+ set: function(parent, key, event) {
301
+ setValue(parent, key, event.newValue);
302
+ },
303
+ delete: function(parent, key) {
304
+ deleteValue(parent, key);
305
+ },
306
+ "array-push": function(targetArray, _keyIgnored, event) {
307
+ if (!Array.isArray(targetArray)) {
308
+ console.warn(`expected array at path ${event.path.join(".")}`);
309
+ return;
310
+ }
311
+ if (!event.items) {
312
+ console.warn("array-push event missing items");
313
+ return;
314
+ }
315
+ targetArray.push(...event.items);
316
+ },
317
+ "array-pop": function(targetArray, _keyIgnored, event) {
318
+ if (!Array.isArray(targetArray)) {
319
+ console.warn(`expected array at path ${event.path.join(".")}`);
320
+ return;
321
+ }
322
+ if (targetArray.length > 0) {
323
+ targetArray.pop();
324
+ }
325
+ },
326
+ "array-splice": function(targetArray, _keyIgnored, event) {
327
+ if (!Array.isArray(targetArray)) {
328
+ console.warn(`expected array at path ${event.path.join(".")}`);
329
+ return;
330
+ }
331
+ if (event.key === undefined || event.deleteCount === undefined) {
332
+ console.warn("array-splice event missing key or deletecount");
333
+ return;
334
+ }
335
+ if (event.items && event.items.length > 0) {
336
+ targetArray.splice(event.key, event.deleteCount, ...event.items);
337
+ } else {
338
+ targetArray.splice(event.key, event.deleteCount);
339
+ }
340
+ },
341
+ "array-shift": function(targetArray, _keyIgnored, event) {
342
+ if (!Array.isArray(targetArray)) {
343
+ console.warn(`expected array at path ${event.path.join(".")}`);
344
+ return;
345
+ }
346
+ if (targetArray.length > 0) {
347
+ targetArray.shift();
348
+ }
349
+ },
350
+ "array-unshift": function(targetArray, _keyIgnored, event) {
351
+ if (!Array.isArray(targetArray)) {
352
+ console.warn(`expected array at path ${event.path.join(".")}`);
353
+ return;
354
+ }
355
+ if (!event.items) {
356
+ console.warn("array-unshift event missing items");
357
+ return;
358
+ }
359
+ targetArray.unshift(...event.items);
360
+ },
361
+ "map-set": function(targetMap, _parentKeyIgnored, event) {
362
+ if (!(targetMap instanceof Map)) {
363
+ console.warn(`expected map at path ${event.path.join(".")}`);
364
+ return;
365
+ }
366
+ targetMap.set(event.key, event.newValue);
367
+ },
368
+ "map-delete": function(targetMap, _parentKeyIgnored, event) {
369
+ if (!(targetMap instanceof Map)) {
370
+ console.warn(`expected map at path ${event.path.join(".")}`);
371
+ return;
372
+ }
373
+ targetMap.delete(event.key);
374
+ },
375
+ "map-clear": function(targetMap, _parentKeyIgnored, event) {
376
+ if (!(targetMap instanceof Map)) {
377
+ console.warn(`expected map at path ${event.path.join(".")}`);
378
+ return;
379
+ }
380
+ targetMap.clear();
381
+ },
382
+ "set-add": function(targetSet, _keyIgnored, event) {
383
+ if (!(targetSet instanceof Set)) {
384
+ console.warn(`expected set at path ${event.path.join(".")}`);
385
+ return;
386
+ }
387
+ targetSet.add(event.value);
388
+ },
389
+ "set-delete": function(targetSet, _keyIgnored, event) {
390
+ if (!(targetSet instanceof Set)) {
391
+ console.warn(`expected set at path ${event.path.join(".")}`);
392
+ return;
393
+ }
394
+ targetSet.delete(event.value);
395
+ },
396
+ "set-clear": function(targetSet, _keyIgnored, event) {
397
+ if (!(targetSet instanceof Set)) {
398
+ console.warn(`expected set at path ${event.path.join(".")}`);
399
+ return;
400
+ }
401
+ targetSet.clear();
402
+ },
403
+ replace: function(target, _keyIgnored, event) {
404
+ const newValue = event.newValue;
405
+ if (newValue === undefined || newValue === null) {
406
+ console.warn("replace action requires newValue");
407
+ return;
408
+ }
409
+ if (Array.isArray(target) && Array.isArray(newValue)) {
410
+ target.splice(0, target.length, ...newValue);
411
+ } else if (target instanceof Map && newValue instanceof Map) {
412
+ const newValueEntries = [...newValue.entries()];
413
+ target.clear();
414
+ for (const [key, value] of newValueEntries) {
415
+ target.set(key, value);
416
+ }
417
+ } else if (target instanceof Set && newValue instanceof Set) {
418
+ const newValueEntries = [...newValue.values()];
419
+ target.clear();
420
+ for (const value of newValueEntries) {
421
+ target.add(value);
422
+ }
423
+ } else if (typeof target === "object" && target !== null && typeof newValue === "object" && newValue !== null) {
424
+ Object.keys(target).forEach((key) => delete target[key]);
425
+ Object.assign(target, newValue);
426
+ } else {
427
+ console.warn(`Type mismatch or unsupported type for 'replace' action at path ${event.path.join(".")}. Target type: ${typeof target} ${target.constructor.name}, New value type: ${typeof newValue} ${newValue.constructor.name}`);
428
+ }
429
+ }
430
+ };
431
+ function updateState(root, event) {
432
+ const { action, path } = event;
433
+ if (!path || path.length === 0 && action !== "replace") {
434
+ console.warn("event path is invalid for action", event);
435
+ return;
436
+ }
437
+ const handler = actionHandlers[action];
438
+ if (!handler) {
439
+ console.error(`unhandled action type: ${action}`, event);
440
+ return;
441
+ }
442
+ let targetForHandler;
443
+ let keyForHandler = null;
444
+ if (action === "set" || action === "delete") {
445
+ if (path.length === 1) {
446
+ targetForHandler = root;
447
+ keyForHandler = path[0];
448
+ } else {
449
+ const parentPath = path.slice(0, -1);
450
+ const parentPathKey = parentPath.join(".");
451
+ let parent = pathCache.get(root)?.get(parentPathKey);
452
+ if (parent === undefined) {
453
+ parent = parentPath.reduce((acc, key) => acc ? getValue(acc, key) : undefined, root);
454
+ if (parent !== undefined)
455
+ setInPathCache(root, parentPathKey, parent);
456
+ }
457
+ if (parent === undefined) {
458
+ console.warn(`parent path ${parentPathKey} not found for action ${action}`);
459
+ return;
460
+ }
461
+ targetForHandler = parent;
462
+ keyForHandler = path[path.length - 1];
463
+ }
464
+ } else if (action.startsWith("array-") || action.startsWith("map-") || action.startsWith("set-") || action === "replace") {
465
+ if (path.length === 0 && action === "replace") {
466
+ targetForHandler = root;
467
+ } else {
468
+ const targetPath = path;
469
+ const targetPathKey = targetPath.join(".");
470
+ let targetCollection = pathCache.get(root)?.get(targetPathKey);
471
+ if (targetCollection === undefined) {
472
+ targetCollection = targetPath.reduce((acc, key) => acc ? getValue(acc, key) : undefined, root);
473
+ if (targetCollection !== undefined)
474
+ setInPathCache(root, targetPathKey, targetCollection);
475
+ }
476
+ if (targetCollection === undefined) {
477
+ console.warn(`target at path ${targetPathKey} not found for action ${action}`);
478
+ return;
479
+ }
480
+ targetForHandler = targetCollection;
481
+ }
482
+ } else {
483
+ console.error(`unexpected action type passed checks: ${action}`);
484
+ return;
485
+ }
486
+ handler(targetForHandler, keyForHandler, event);
487
+ }
488
+ // src/watch-effect.ts
489
+ var activeEffect = null;
490
+ var queuedEffects = new Map;
491
+ var isFlushing = false;
492
+ var currentTriggerDepth = 0;
493
+ function setActiveEffect(effect) {
494
+ activeEffect = effect;
495
+ }
496
+ var targetMap = new WeakMap;
497
+ function cleanupEffect(effect) {
498
+ if (effect.dependencies) {
499
+ effect.dependencies.forEach((dep) => {
500
+ dep.delete(effect);
501
+ });
502
+ effect.dependencies.clear();
503
+ }
504
+ }
505
+ function runCleanupFunctions(effect) {
506
+ if (effect.cleanupFns && effect.cleanupFns.length > 0) {
507
+ effect.cleanupFns.forEach((cleanupFn) => {
508
+ try {
509
+ cleanupFn();
510
+ } catch (error) {
511
+ console.error("Error in effect cleanup function:", error);
512
+ }
513
+ });
514
+ effect.cleanupFns = [];
515
+ }
516
+ }
517
+ function track(target, key) {
518
+ if (!activeEffect || !activeEffect.active)
519
+ return;
520
+ let depsMap = targetMap.get(target);
521
+ if (!depsMap) {
522
+ depsMap = new Map;
523
+ targetMap.set(target, depsMap);
524
+ }
525
+ let dep = depsMap.get(key);
526
+ if (!dep) {
527
+ dep = new Set;
528
+ depsMap.set(key, dep);
529
+ }
530
+ const effectToAdd = activeEffect;
531
+ if (!dep.has(effectToAdd)) {
532
+ dep.add(effectToAdd);
533
+ if (!effectToAdd.dependencies) {
534
+ effectToAdd.dependencies = new Set;
535
+ }
536
+ effectToAdd.dependencies.add(dep);
537
+ if (effectToAdd.options?.onTrack) {
538
+ effectToAdd.options.onTrack({ effect: effectToAdd._rawCallback, target, key, type: "track" });
539
+ }
540
+ }
541
+ }
542
+ function flushEffects() {
543
+ if (isFlushing)
544
+ return;
545
+ isFlushing = true;
546
+ try {
547
+ let minDepth = Infinity;
548
+ for (const depth of queuedEffects.values()) {
549
+ if (depth < minDepth)
550
+ minDepth = depth;
551
+ }
552
+ const effectsToRun = [];
553
+ for (const [effect, depth] of queuedEffects.entries()) {
554
+ if (depth === minDepth) {
555
+ effectsToRun.push(effect);
556
+ queuedEffects.delete(effect);
557
+ }
558
+ }
559
+ for (const effect of effectsToRun) {
560
+ if (!effect.active)
561
+ continue;
562
+ if (effect.options?.scheduler) {
563
+ effect.options.scheduler(effect.run);
564
+ }
565
+ }
566
+ for (const effect of effectsToRun) {
567
+ if (!effect.active)
568
+ continue;
569
+ if (!effect.options?.scheduler) {
570
+ effect.run();
571
+ }
572
+ }
573
+ } finally {
574
+ isFlushing = false;
575
+ if (queuedEffects.size > 0) {
576
+ flushEffects();
577
+ }
578
+ }
579
+ }
580
+ function trigger(target, key) {
581
+ const depsMap = targetMap.get(target);
582
+ if (!depsMap)
583
+ return;
584
+ currentTriggerDepth++;
585
+ const triggerLevel = currentTriggerDepth;
586
+ try {
587
+ const addEffects = (depKey) => {
588
+ const dep = depsMap.get(depKey);
589
+ if (dep) {
590
+ dep.forEach((effect) => {
591
+ if (effect !== activeEffect && effect.active) {
592
+ if (effect.options?.onTrigger) {
593
+ effect.options.onTrigger({ effect: effect._rawCallback, target, key, type: "trigger" });
594
+ }
595
+ if (!queuedEffects.has(effect) || triggerLevel < queuedEffects.get(effect)) {
596
+ queuedEffects.set(effect, triggerLevel);
597
+ }
598
+ }
599
+ });
600
+ }
601
+ };
602
+ addEffects(key);
603
+ if (queuedEffects.size > 0) {
604
+ flushEffects();
605
+ }
606
+ } finally {
607
+ currentTriggerDepth--;
608
+ }
609
+ }
610
+ function watchEffect(effectCallback, options = {}) {
611
+ const run = () => {
612
+ if (!effectFn.active) {
613
+ throw new Error("Trying to run a stopped effect");
614
+ }
615
+ const previousEffect = activeEffect;
616
+ try {
617
+ runCleanupFunctions(effectFn);
618
+ cleanupEffect(effectFn);
619
+ setActiveEffect(effectFn);
620
+ const onCleanup = (cleanupFn) => {
621
+ if (!effectFn.cleanupFns) {
622
+ effectFn.cleanupFns = [];
623
+ }
624
+ effectFn.cleanupFns.push(cleanupFn);
625
+ };
626
+ return effectCallback(onCleanup);
627
+ } finally {
628
+ setActiveEffect(previousEffect);
629
+ }
630
+ };
631
+ const effectFn = {
632
+ run,
633
+ dependencies: new Set,
634
+ options,
635
+ active: true,
636
+ _rawCallback: effectCallback,
637
+ cleanupFns: []
638
+ };
639
+ if (!options.lazy) {
640
+ effectFn.run();
641
+ }
642
+ const stopHandle = () => {
643
+ if (effectFn.active) {
644
+ runCleanupFunctions(effectFn);
645
+ cleanupEffect(effectFn);
646
+ effectFn.active = false;
647
+ queuedEffects.delete(effectFn);
648
+ }
649
+ };
650
+ stopHandle.effect = effectFn;
651
+ return stopHandle;
652
+ }
653
+
654
+ // src/wrap-set.ts
655
+ function wrapSet(set, emit, path = []) {
656
+ const cachedProxy = wrapperCache.get(set);
657
+ if (cachedProxy)
658
+ return cachedProxy;
659
+ if (globalSeen.has(set))
660
+ return globalSeen.get(set);
661
+ const methodCache = {};
662
+ const proxy = new Proxy(set, {
663
+ get(target, prop, receiver) {
664
+ track(target, prop);
665
+ if (prop === Symbol.iterator || prop === "entries" || prop === "values" || prop === "keys" || prop === "forEach") {
666
+ track(target, Symbol.iterator);
667
+ }
668
+ if (methodCache[prop]) {
669
+ return methodCache[prop];
670
+ }
671
+ if (prop === "add") {
672
+ methodCache[prop] = function(value2) {
673
+ const existed = target.has(value2);
674
+ const oldSize = target.size;
675
+ if (!existed) {
676
+ target.add(value2);
677
+ const newSize = target.size;
678
+ const event = {
679
+ action: "set-add",
680
+ path,
681
+ value: value2
682
+ };
683
+ emit?.(event);
684
+ trigger(target, Symbol.iterator);
685
+ if (oldSize !== newSize) {
686
+ trigger(target, "size");
687
+ }
688
+ }
689
+ return receiver;
690
+ };
691
+ return methodCache[prop];
692
+ }
693
+ if (prop === "delete") {
694
+ methodCache[prop] = function(value2) {
695
+ const existed = target.has(value2);
696
+ const oldSize = target.size;
697
+ if (existed) {
698
+ const oldValue = value2;
699
+ const result = target.delete(value2);
700
+ const newSize = target.size;
701
+ if (result) {
702
+ const event = {
703
+ action: "set-delete",
704
+ path,
705
+ value: value2,
706
+ oldValue
707
+ };
708
+ emit?.(event);
709
+ trigger(target, Symbol.iterator);
710
+ if (oldSize !== newSize) {
711
+ trigger(target, "size");
712
+ }
713
+ }
714
+ return result;
715
+ }
716
+ return false;
717
+ };
718
+ return methodCache[prop];
719
+ }
720
+ if (prop === "clear") {
721
+ methodCache[prop] = function() {
722
+ const oldSize = target.size;
723
+ if (oldSize === 0)
724
+ return;
725
+ target.clear();
726
+ const newSize = target.size;
727
+ const event = {
728
+ action: "set-clear",
729
+ path,
730
+ value: null
731
+ };
732
+ emit?.(event);
733
+ trigger(target, Symbol.iterator);
734
+ if (oldSize !== newSize) {
735
+ trigger(target, "size");
736
+ }
737
+ };
738
+ return methodCache[prop];
739
+ }
740
+ if (prop === "has") {
741
+ track(target, Symbol.iterator);
742
+ methodCache[prop] = function(value2) {
743
+ if (typeof value2 === "string" || typeof value2 === "number" || typeof value2 === "symbol") {
744
+ track(target, String(value2));
745
+ }
746
+ return target.has(value2);
747
+ }.bind(target);
748
+ return methodCache[prop];
749
+ }
750
+ if (prop === "values" || prop === Symbol.iterator || prop === "entries" || prop === "keys" || prop === "forEach") {
751
+ track(target, Symbol.iterator);
752
+ const originalMethod = Reflect.get(target, prop, receiver);
753
+ if (prop === "forEach") {
754
+ methodCache[prop] = (callbackfn, thisArg) => {
755
+ const valuesIterator = proxy.values();
756
+ for (const value2 of valuesIterator) {
757
+ callbackfn.call(thisArg, value2, value2, proxy);
758
+ }
759
+ };
760
+ return methodCache[prop];
761
+ }
762
+ methodCache[prop] = function* (...args) {
763
+ let index = 0;
764
+ const iterator = originalMethod.apply(target, args);
765
+ for (const entry of iterator) {
766
+ let valueToWrap = entry;
767
+ if (prop === "entries") {
768
+ valueToWrap = entry[1];
769
+ }
770
+ track(target, String(index));
771
+ let wrappedValue = valueToWrap;
772
+ if (valueToWrap && typeof valueToWrap === "object") {
773
+ if (globalSeen.has(valueToWrap)) {
774
+ wrappedValue = globalSeen.get(valueToWrap);
775
+ } else {
776
+ const cachedValueProxy = wrapperCache.get(valueToWrap);
777
+ if (cachedValueProxy) {
778
+ wrappedValue = cachedValueProxy;
779
+ } else {
780
+ const keyForPath = String(index);
781
+ const pathKey = path.length > 0 ? `${path.join(".")}.${keyForPath}` : keyForPath;
782
+ let newPath = getPathConcat(pathKey);
783
+ if (newPath === undefined) {
784
+ newPath = path.concat(keyForPath);
785
+ setPathConcat(pathKey, newPath);
786
+ }
787
+ if (valueToWrap instanceof Map)
788
+ wrappedValue = wrapMap(valueToWrap, emit, newPath);
789
+ else if (valueToWrap instanceof Set)
790
+ wrappedValue = wrapSet(valueToWrap, emit, newPath);
791
+ else if (Array.isArray(valueToWrap))
792
+ wrappedValue = wrapArray(valueToWrap, emit, newPath);
793
+ else if (valueToWrap instanceof Date)
794
+ wrappedValue = new Date(valueToWrap.getTime());
795
+ else
796
+ wrappedValue = reactive(valueToWrap, emit, newPath);
797
+ }
798
+ }
799
+ }
800
+ if (prop === "entries") {
801
+ yield [wrappedValue, wrappedValue];
802
+ } else {
803
+ yield wrappedValue;
804
+ }
805
+ index++;
806
+ }
807
+ };
808
+ return methodCache[prop];
809
+ }
810
+ if (prop === "size") {
811
+ track(target, "size");
812
+ return target.size;
813
+ }
814
+ if (prop === "constructor") {
815
+ return Set;
816
+ }
817
+ const value = Reflect.get(target, prop, receiver);
818
+ if (typeof value === "function") {
819
+ return value.bind(target);
820
+ }
821
+ return value;
822
+ }
823
+ });
824
+ globalSeen.set(set, proxy);
825
+ wrapperCache.set(set, proxy);
826
+ return proxy;
827
+ }
828
+
829
+ // src/wrap-map.ts
830
+ function wrapMap(map, emit, path = []) {
831
+ const cachedProxy = wrapperCache.get(map);
832
+ if (cachedProxy)
833
+ return cachedProxy;
834
+ if (globalSeen.has(map))
835
+ return globalSeen.get(map);
836
+ const methodCache = {};
837
+ const proxy = new Proxy(map, {
838
+ get(target, prop, receiver) {
839
+ track(target, prop);
840
+ if (prop === Symbol.iterator || prop === "entries" || prop === "values" || prop === "keys" || prop === "forEach") {
841
+ track(target, Symbol.iterator);
842
+ }
843
+ if (methodCache[prop]) {
844
+ return methodCache[prop];
845
+ }
846
+ if (prop === "set") {
847
+ methodCache[prop] = function(key, value2) {
848
+ const existed = target.has(key);
849
+ const oldValue = target.get(key);
850
+ const oldSize = target.size;
851
+ if (oldValue === value2)
852
+ return receiver;
853
+ if (oldValue && typeof oldValue === "object" && value2 && typeof value2 === "object" && deepEqual(oldValue, value2, new WeakMap))
854
+ return receiver;
855
+ target.set(key, value2);
856
+ const newSize = target.size;
857
+ const pathKey = path.join(".");
858
+ let cachedPath = getPathConcat(pathKey);
859
+ if (cachedPath === undefined) {
860
+ cachedPath = path;
861
+ setPathConcat(pathKey, cachedPath);
862
+ }
863
+ const event = {
864
+ action: "map-set",
865
+ path: cachedPath,
866
+ key,
867
+ oldValue,
868
+ newValue: value2
869
+ };
870
+ emit?.(event);
871
+ if (!existed) {
872
+ trigger(target, Symbol.iterator);
873
+ if (oldSize !== newSize) {
874
+ trigger(target, "size");
875
+ }
876
+ } else {
877
+ trigger(target, String(key));
878
+ }
879
+ return receiver;
880
+ };
881
+ return methodCache[prop];
882
+ }
883
+ if (prop === "delete") {
884
+ methodCache[prop] = function(key) {
885
+ const existed = target.has(key);
886
+ if (!existed)
887
+ return false;
888
+ const oldValue = target.get(key);
889
+ const oldSize = target.size;
890
+ const result = target.delete(key);
891
+ const newSize = target.size;
892
+ if (result) {
893
+ const pathKey = path.join(".");
894
+ let cachedPath = getPathConcat(pathKey);
895
+ if (cachedPath === undefined) {
896
+ cachedPath = path;
897
+ setPathConcat(pathKey, cachedPath);
898
+ }
899
+ const event = {
900
+ action: "map-delete",
901
+ path: cachedPath,
902
+ key,
903
+ oldValue
904
+ };
905
+ emit?.(event);
906
+ trigger(target, Symbol.iterator);
907
+ if (oldSize !== newSize) {
908
+ trigger(target, "size");
909
+ }
910
+ trigger(target, String(key));
911
+ }
912
+ return result;
913
+ };
914
+ return methodCache[prop];
915
+ }
916
+ if (prop === "clear") {
917
+ methodCache[prop] = function() {
918
+ const oldSize = target.size;
919
+ if (oldSize === 0)
920
+ return;
921
+ target.clear();
922
+ const newSize = target.size;
923
+ const event = {
924
+ action: "map-clear",
925
+ path,
926
+ key: null
927
+ };
928
+ emit?.(event);
929
+ trigger(target, Symbol.iterator);
930
+ if (oldSize !== newSize) {
931
+ trigger(target, "size");
932
+ }
933
+ };
934
+ return methodCache[prop];
935
+ }
936
+ if (prop === "get") {
937
+ methodCache[prop] = function(key) {
938
+ track(target, String(key));
939
+ const value2 = target.get(key);
940
+ if (!value2 || typeof value2 !== "object")
941
+ return value2;
942
+ if (globalSeen.has(value2))
943
+ return globalSeen.get(value2);
944
+ const cachedValueProxy = wrapperCache.get(value2);
945
+ if (cachedValueProxy)
946
+ return cachedValueProxy;
947
+ const keyString = String(key);
948
+ const pathKey = path.length > 0 ? `${path.join(".")}.${keyString}` : keyString;
949
+ let newPath = getPathConcat(pathKey);
950
+ if (newPath === undefined) {
951
+ newPath = path.concat(keyString);
952
+ setPathConcat(pathKey, newPath);
953
+ }
954
+ if (value2 instanceof Map)
955
+ return wrapMap(value2, emit, newPath);
956
+ if (value2 instanceof Set)
957
+ return wrapSet(value2, emit, newPath);
958
+ if (Array.isArray(value2))
959
+ return wrapArray(value2, emit, newPath);
960
+ if (value2 instanceof Date)
961
+ return new Date(value2.getTime());
962
+ return reactive(value2, emit, newPath);
963
+ };
964
+ return methodCache[prop];
965
+ }
966
+ if (prop === "has") {
967
+ track(target, Symbol.iterator);
968
+ methodCache[prop] = function(key) {
969
+ track(target, String(key));
970
+ return target.has(key);
971
+ }.bind(target);
972
+ return methodCache[prop];
973
+ }
974
+ if (prop === Symbol.iterator || prop === "entries" || prop === "values" || prop === "keys" || prop === "forEach") {
975
+ track(target, Symbol.iterator);
976
+ const originalMethod = Reflect.get(target, prop, receiver);
977
+ if (prop === "forEach") {
978
+ methodCache[prop] = (callbackfn, thisArg) => {
979
+ const entriesIterator = proxy.entries();
980
+ for (const [key, value2] of entriesIterator) {
981
+ callbackfn.call(thisArg, value2, key, proxy);
982
+ }
983
+ };
984
+ return methodCache[prop];
985
+ }
986
+ methodCache[prop] = function* (...args) {
987
+ const iterator = originalMethod.apply(target, args);
988
+ for (const entry of iterator) {
989
+ let keyToWrap = entry;
990
+ let valueToWrap = entry;
991
+ let isEntry = false;
992
+ if (prop === "entries" || prop === Symbol.iterator) {
993
+ keyToWrap = entry[0];
994
+ valueToWrap = entry[1];
995
+ isEntry = true;
996
+ }
997
+ let wrappedKey = keyToWrap;
998
+ if (isEntry && keyToWrap && typeof keyToWrap === "object") {
999
+ if (globalSeen.has(keyToWrap)) {
1000
+ wrappedKey = globalSeen.get(keyToWrap);
1001
+ } else {
1002
+ const pathKey = path.length > 0 ? `${path.join(".")}.${String(keyToWrap)}` : String(keyToWrap);
1003
+ let keyPath = getPathConcat(pathKey);
1004
+ if (keyPath === undefined) {
1005
+ keyPath = path.concat(String(keyToWrap));
1006
+ setPathConcat(pathKey, keyPath);
1007
+ }
1008
+ wrappedKey = reactive(keyToWrap, emit, keyPath);
1009
+ }
1010
+ }
1011
+ let wrappedValue = valueToWrap;
1012
+ if (valueToWrap && typeof valueToWrap === "object") {
1013
+ if (globalSeen.has(valueToWrap)) {
1014
+ wrappedValue = globalSeen.get(valueToWrap);
1015
+ } else {
1016
+ const cachedValueProxy = wrapperCache.get(valueToWrap);
1017
+ if (cachedValueProxy) {
1018
+ wrappedValue = cachedValueProxy;
1019
+ } else {
1020
+ const keyString = String(keyToWrap);
1021
+ const pathKey = path.length > 0 ? `${path.join(".")}.${keyString}` : keyString;
1022
+ let newPath = getPathConcat(pathKey);
1023
+ if (newPath === undefined) {
1024
+ newPath = path.concat(keyString);
1025
+ setPathConcat(pathKey, newPath);
1026
+ }
1027
+ if (valueToWrap instanceof Map)
1028
+ wrappedValue = wrapMap(valueToWrap, emit, newPath);
1029
+ else if (valueToWrap instanceof Set)
1030
+ wrappedValue = wrapSet(valueToWrap, emit, newPath);
1031
+ else if (Array.isArray(valueToWrap))
1032
+ wrappedValue = wrapArray(valueToWrap, emit, newPath);
1033
+ else if (valueToWrap instanceof Date)
1034
+ wrappedValue = new Date(valueToWrap.getTime());
1035
+ else
1036
+ wrappedValue = reactive(valueToWrap, emit, newPath);
1037
+ }
1038
+ }
1039
+ }
1040
+ if (prop === "entries" || prop === Symbol.iterator) {
1041
+ yield [wrappedKey, wrappedValue];
1042
+ } else if (prop === "values") {
1043
+ yield wrappedValue;
1044
+ } else {
1045
+ yield wrappedKey;
1046
+ }
1047
+ }
1048
+ };
1049
+ return methodCache[prop];
1050
+ }
1051
+ if (prop === "size") {
1052
+ track(target, "size");
1053
+ return target.size;
1054
+ }
1055
+ if (prop === "constructor") {
1056
+ return Map;
1057
+ }
1058
+ const value = Reflect.get(target, prop, receiver);
1059
+ if (typeof value === "function") {
1060
+ return value.bind(target);
1061
+ }
1062
+ return value;
1063
+ }
1064
+ });
1065
+ globalSeen.set(map, proxy);
1066
+ wrapperCache.set(map, proxy);
1067
+ return proxy;
1068
+ }
1069
+
1070
+ // src/wrap-array.ts
1071
+ function isObject2(v) {
1072
+ return v && typeof v === "object";
1073
+ }
1074
+ function wrapArray(arr, emit, path = []) {
1075
+ const cachedProxy = wrapperCache.get(arr);
1076
+ if (cachedProxy)
1077
+ return cachedProxy;
1078
+ if (globalSeen.has(arr))
1079
+ return globalSeen.get(arr);
1080
+ const methodCache = {};
1081
+ const proxy = new Proxy(arr, {
1082
+ get(target, prop, receiver) {
1083
+ track(target, prop);
1084
+ if (methodCache[prop]) {
1085
+ return methodCache[prop];
1086
+ }
1087
+ switch (prop) {
1088
+ case "push":
1089
+ track(target, "length");
1090
+ methodCache[prop] = function(...items) {
1091
+ const oldLength = target.length;
1092
+ const result = target.push(...items);
1093
+ const newLength = target.length;
1094
+ if (items.length > 0) {
1095
+ const event = {
1096
+ action: "array-push",
1097
+ path,
1098
+ key: oldLength,
1099
+ items
1100
+ };
1101
+ emit?.(event);
1102
+ trigger(target, Symbol.iterator);
1103
+ if (oldLength !== newLength) {
1104
+ trigger(target, "length");
1105
+ }
1106
+ }
1107
+ return result;
1108
+ };
1109
+ return methodCache[prop];
1110
+ case "pop":
1111
+ track(target, "length");
1112
+ methodCache[prop] = function() {
1113
+ if (target.length === 0)
1114
+ return;
1115
+ const oldLength = target.length;
1116
+ const poppedIndex = oldLength - 1;
1117
+ const oldValue = target[poppedIndex];
1118
+ const result = target.pop();
1119
+ const newLength = target.length;
1120
+ const event = {
1121
+ action: "array-pop",
1122
+ path,
1123
+ key: poppedIndex,
1124
+ oldValue
1125
+ };
1126
+ emit?.(event);
1127
+ trigger(target, Symbol.iterator);
1128
+ if (oldLength !== newLength) {
1129
+ trigger(target, "length");
1130
+ }
1131
+ return result;
1132
+ };
1133
+ return methodCache[prop];
1134
+ case "shift":
1135
+ track(target, "length");
1136
+ methodCache[prop] = function() {
1137
+ if (target.length === 0)
1138
+ return;
1139
+ const oldLength = target.length;
1140
+ const oldValue = target[0];
1141
+ const result = target.shift();
1142
+ const newLength = target.length;
1143
+ const event = {
1144
+ action: "array-shift",
1145
+ path,
1146
+ key: 0,
1147
+ oldValue
1148
+ };
1149
+ emit?.(event);
1150
+ trigger(target, Symbol.iterator);
1151
+ if (oldLength !== newLength) {
1152
+ trigger(target, "length");
1153
+ }
1154
+ return result;
1155
+ };
1156
+ return methodCache[prop];
1157
+ case "unshift":
1158
+ track(target, "length");
1159
+ methodCache[prop] = function(...items) {
1160
+ const oldLength = target.length;
1161
+ const result = target.unshift(...items);
1162
+ const newLength = target.length;
1163
+ if (items.length > 0) {
1164
+ const event = {
1165
+ action: "array-unshift",
1166
+ path,
1167
+ key: 0,
1168
+ items
1169
+ };
1170
+ emit?.(event);
1171
+ trigger(target, Symbol.iterator);
1172
+ if (oldLength !== newLength) {
1173
+ trigger(target, "length");
1174
+ }
1175
+ }
1176
+ return result;
1177
+ };
1178
+ return methodCache[prop];
1179
+ case "splice":
1180
+ track(target, "length");
1181
+ methodCache[prop] = function(start, deleteCount, ...items) {
1182
+ const oldLength = target.length;
1183
+ const actualStart = start < 0 ? Math.max(target.length + start, 0) : Math.min(start, target.length);
1184
+ const deleteCountNum = deleteCount === undefined ? target.length - actualStart : Number(deleteCount);
1185
+ const actualDeleteCount = Math.min(deleteCountNum, target.length - actualStart);
1186
+ const deletedItems = target.slice(actualStart, actualStart + actualDeleteCount);
1187
+ const result = target.splice(start, deleteCountNum, ...items);
1188
+ const newLength = target.length;
1189
+ if (actualDeleteCount > 0 || items.length > 0) {
1190
+ const event = {
1191
+ action: "array-splice",
1192
+ path,
1193
+ key: actualStart,
1194
+ deleteCount: actualDeleteCount,
1195
+ items: items.length > 0 ? items : undefined,
1196
+ oldValues: deletedItems.length > 0 ? deletedItems : undefined
1197
+ };
1198
+ emit?.(event);
1199
+ trigger(target, Symbol.iterator);
1200
+ if (oldLength !== newLength) {
1201
+ trigger(target, "length");
1202
+ }
1203
+ }
1204
+ return result;
1205
+ };
1206
+ return methodCache[prop];
1207
+ case Symbol.iterator:
1208
+ case "values":
1209
+ case "keys":
1210
+ case "entries":
1211
+ case "forEach":
1212
+ case "map":
1213
+ case "filter":
1214
+ case "reduce":
1215
+ case "reduceRight":
1216
+ case "find":
1217
+ case "findIndex":
1218
+ case "every":
1219
+ case "some":
1220
+ case "join":
1221
+ track(target, Symbol.iterator);
1222
+ break;
1223
+ case "length":
1224
+ track(target, "length");
1225
+ return Reflect.get(target, prop, receiver);
1226
+ }
1227
+ const value = Reflect.get(target, prop, receiver);
1228
+ const isNumericIndex = typeof prop === "number" || typeof prop === "string" && !isNaN(parseInt(prop, 10));
1229
+ if (isNumericIndex) {
1230
+ track(target, String(prop));
1231
+ if (!isObject2(value))
1232
+ return value;
1233
+ if (globalSeen.has(value))
1234
+ return globalSeen.get(value);
1235
+ const cachedValueProxy = wrapperCache.get(value);
1236
+ if (cachedValueProxy)
1237
+ return cachedValueProxy;
1238
+ const propKey = String(prop);
1239
+ const pathKey = path.length > 0 ? `${path.join(".")}.${propKey}` : propKey;
1240
+ let newPath = getPathConcat(pathKey);
1241
+ if (newPath === undefined) {
1242
+ newPath = path.concat(propKey);
1243
+ setPathConcat(pathKey, newPath);
1244
+ }
1245
+ if (Array.isArray(value))
1246
+ return wrapArray(value, emit, newPath);
1247
+ if (value instanceof Map)
1248
+ return wrapMap(value, emit, newPath);
1249
+ if (value instanceof Set)
1250
+ return wrapSet(value, emit, newPath);
1251
+ if (value instanceof Date)
1252
+ return new Date(value.getTime());
1253
+ return reactive(value, emit, newPath);
1254
+ }
1255
+ if (typeof value === "function") {
1256
+ return value.bind(target);
1257
+ }
1258
+ return value;
1259
+ },
1260
+ set(target, prop, value, receiver) {
1261
+ const oldValue = target[prop];
1262
+ if (oldValue === value)
1263
+ return true;
1264
+ if (isObject2(oldValue) && isObject2(value) && deepEqual(oldValue, value, new WeakMap))
1265
+ return true;
1266
+ const descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
1267
+ const result = Reflect.set(target, prop, value, receiver);
1268
+ const isNumericIndex = typeof prop === "number" || typeof prop === "string" && !isNaN(parseInt(String(prop)));
1269
+ if (result && (!descriptor || !descriptor.set || isNumericIndex)) {
1270
+ const propKey = String(prop);
1271
+ const pathKey = path.length > 0 ? `${path.join(".")}.${propKey}` : propKey;
1272
+ let newPath = getPathConcat(pathKey);
1273
+ if (newPath === undefined) {
1274
+ newPath = path.concat(propKey);
1275
+ setPathConcat(pathKey, newPath);
1276
+ }
1277
+ const event = {
1278
+ action: "set",
1279
+ path: newPath,
1280
+ oldValue,
1281
+ newValue: value
1282
+ };
1283
+ emit?.(event);
1284
+ trigger(target, prop);
1285
+ }
1286
+ return result;
1287
+ }
1288
+ });
1289
+ globalSeen.set(arr, proxy);
1290
+ wrapperCache.set(arr, proxy);
1291
+ return proxy;
1292
+ }
1293
+
1294
+ // src/constants.ts
1295
+ var ReactiveFlags;
1296
+ ((ReactiveFlags2) => {
1297
+ ReactiveFlags2["RAW"] = "__v_raw";
1298
+ ReactiveFlags2["IS_REACTIVE"] = "__v_isReactive";
1299
+ ReactiveFlags2["SKIP"] = "__v_skip";
1300
+ })(ReactiveFlags ||= {});
1301
+
1302
+ // src/reactive.ts
1303
+ function isObject3(v) {
1304
+ return v && typeof v === "object";
1305
+ }
1306
+ function isReactive(value) {
1307
+ return !!(value && value["__v_isReactive" /* IS_REACTIVE */]);
1308
+ }
1309
+ function toRaw(observed) {
1310
+ const raw = observed && observed["__v_raw" /* RAW */];
1311
+ return raw ? toRaw(raw) : observed;
1312
+ }
1313
+ function reactive(obj, emit, path = []) {
1314
+ if (obj["__v_skip" /* SKIP */]) {
1315
+ return obj;
1316
+ }
1317
+ if (globalSeen.has(obj))
1318
+ return globalSeen.get(obj);
1319
+ if (emit && path.length === 0) {
1320
+ try {
1321
+ const initialEvent = {
1322
+ action: "replace",
1323
+ path: [],
1324
+ newValue: obj
1325
+ };
1326
+ emit(initialEvent);
1327
+ } catch (error) {
1328
+ console.error("Failed to emit initial reactive state:", error);
1329
+ }
1330
+ }
1331
+ if (Array.isArray(obj)) {
1332
+ return wrapArray(obj, emit, path);
1333
+ }
1334
+ if (obj instanceof Map) {
1335
+ return wrapMap(obj, emit, path);
1336
+ }
1337
+ if (obj instanceof Set) {
1338
+ return wrapSet(obj, emit, path);
1339
+ }
1340
+ function wrapValue(val, subPath) {
1341
+ if (!isObject3(val))
1342
+ return val;
1343
+ if (globalSeen.has(val))
1344
+ return globalSeen.get(val);
1345
+ if (Array.isArray(val))
1346
+ return wrapArray(val, emit, subPath);
1347
+ if (val instanceof Map)
1348
+ return wrapMap(val, emit, subPath);
1349
+ if (val instanceof Set)
1350
+ return wrapSet(val, emit, subPath);
1351
+ if (val instanceof Date)
1352
+ return new Date(val.getTime());
1353
+ return reactive(val, emit, subPath);
1354
+ }
1355
+ const proxy = new Proxy(obj, {
1356
+ get(target, prop, receiver) {
1357
+ if (prop === "__v_raw" /* RAW */) {
1358
+ return target;
1359
+ }
1360
+ if (prop === "__v_isReactive" /* IS_REACTIVE */) {
1361
+ return true;
1362
+ }
1363
+ const value = Reflect.get(target, prop, receiver);
1364
+ track(target, prop);
1365
+ if (!isObject3(value))
1366
+ return value;
1367
+ const propKey = String(prop);
1368
+ const pathKey = path.length > 0 ? `${path.join(".")}.${propKey}` : propKey;
1369
+ let newPath = getPathConcat(pathKey);
1370
+ if (newPath === undefined) {
1371
+ newPath = path.concat(propKey);
1372
+ setPathConcat(pathKey, newPath);
1373
+ }
1374
+ return wrapValue(value, newPath);
1375
+ },
1376
+ set(target, prop, value, receiver) {
1377
+ const oldValue = target[prop];
1378
+ if (oldValue === value)
1379
+ return true;
1380
+ if (isObject3(oldValue) && isObject3(value) && deepEqual(oldValue, value, new WeakMap))
1381
+ return true;
1382
+ const descriptor = Reflect.getOwnPropertyDescriptor(target, prop);
1383
+ const result = Reflect.set(target, prop, value, receiver);
1384
+ if (result && (!descriptor || !descriptor.set)) {
1385
+ const propKey = String(prop);
1386
+ const pathKey = path.length > 0 ? `${path.join(".")}.${propKey}` : propKey;
1387
+ let newPath = getPathConcat(pathKey);
1388
+ if (newPath === undefined) {
1389
+ newPath = path.concat(propKey);
1390
+ setPathConcat(pathKey, newPath);
1391
+ }
1392
+ const event = {
1393
+ action: "set",
1394
+ path: newPath,
1395
+ oldValue,
1396
+ newValue: value
1397
+ };
1398
+ emit?.(event);
1399
+ trigger(target, prop);
1400
+ }
1401
+ return result;
1402
+ },
1403
+ deleteProperty(target, prop) {
1404
+ const oldValue = target[prop];
1405
+ const hadProperty = Object.prototype.hasOwnProperty.call(target, prop);
1406
+ const result = Reflect.deleteProperty(target, prop);
1407
+ if (hadProperty && result) {
1408
+ const propKey = String(prop);
1409
+ const pathKey = path.length > 0 ? `${path.join(".")}.${propKey}` : propKey;
1410
+ let newPath = getPathConcat(pathKey);
1411
+ if (newPath === undefined) {
1412
+ newPath = path.concat(propKey);
1413
+ setPathConcat(pathKey, newPath);
1414
+ }
1415
+ const event = {
1416
+ action: "delete",
1417
+ path: newPath,
1418
+ oldValue
1419
+ };
1420
+ emit?.(event);
1421
+ trigger(target, prop);
1422
+ }
1423
+ return result;
1424
+ }
1425
+ });
1426
+ globalSeen.set(obj, proxy);
1427
+ return proxy;
1428
+ }
1429
+ // src/ref.ts
1430
+ var isRefSymbol = Symbol("isRef");
1431
+ function ref(value) {
1432
+ return createRef(value);
1433
+ }
1434
+ function createRef(rawValue) {
1435
+ if (isRef(rawValue)) {
1436
+ return rawValue;
1437
+ }
1438
+ let _value = rawValue;
1439
+ const r = {
1440
+ [isRefSymbol]: true,
1441
+ get value() {
1442
+ track(r, "value");
1443
+ return _value;
1444
+ },
1445
+ set value(newValue) {
1446
+ if (_value !== newValue) {
1447
+ _value = newValue;
1448
+ trigger(r, "value");
1449
+ }
1450
+ }
1451
+ };
1452
+ return r;
1453
+ }
1454
+ function isRef(r) {
1455
+ return !!(r && r[isRefSymbol]);
1456
+ }
1457
+ function unref(refValue) {
1458
+ return isRef(refValue) ? refValue.value : refValue;
1459
+ }
1460
+ function toRefs(object) {
1461
+ const result = {};
1462
+ for (const key in object) {
1463
+ result[key] = toRef(object, key);
1464
+ }
1465
+ return result;
1466
+ }
1467
+ function toRef(object, key) {
1468
+ return {
1469
+ [isRefSymbol]: true,
1470
+ get value() {
1471
+ track(this, "value");
1472
+ return object[key];
1473
+ },
1474
+ set value(newVal) {
1475
+ object[key] = newVal;
1476
+ }
1477
+ };
1478
+ }
1479
+ function triggerRef(ref2) {
1480
+ trigger(ref2, "value");
1481
+ }
1482
+
1483
+ // src/computed.ts
1484
+ var isComputedSymbol = Symbol("isComputed");
1485
+ function computed(getterOrOptions) {
1486
+ let getter;
1487
+ let setter;
1488
+ const isGetter = typeof getterOrOptions === "function";
1489
+ if (isGetter) {
1490
+ getter = getterOrOptions;
1491
+ } else {
1492
+ getter = getterOrOptions.get;
1493
+ setter = getterOrOptions.set;
1494
+ }
1495
+ let _value;
1496
+ let _dirty = true;
1497
+ let computedRef;
1498
+ const stopHandle = watchEffect(getter, {
1499
+ lazy: true,
1500
+ scheduler: () => {
1501
+ if (!_dirty) {
1502
+ _dirty = true;
1503
+ trigger(computedRef, "value");
1504
+ }
1505
+ }
1506
+ });
1507
+ const effectRunner = stopHandle.effect;
1508
+ computedRef = {
1509
+ [isRefSymbol]: true,
1510
+ [isComputedSymbol]: true,
1511
+ get value() {
1512
+ track(computedRef, "value");
1513
+ if (_dirty) {
1514
+ _value = effectRunner.run();
1515
+ _dirty = false;
1516
+ }
1517
+ return _value;
1518
+ },
1519
+ set value(newValue) {
1520
+ if (setter) {
1521
+ setter(newValue);
1522
+ } else {
1523
+ console.warn("computed value is read-only");
1524
+ }
1525
+ },
1526
+ stop: stopHandle
1527
+ };
1528
+ return computedRef;
1529
+ }
1530
+ function isComputed(c) {
1531
+ return !!(c && c[isComputedSymbol]);
1532
+ }
1533
+
1534
+ // src/watch.ts
1535
+ function watch(source, callback, options = {}) {
1536
+ const { immediate = false, deep = true } = options;
1537
+ const isMultiSource = Array.isArray(source);
1538
+ const getter = () => {
1539
+ if (isMultiSource) {
1540
+ return source.map((s) => {
1541
+ if (isRef(s))
1542
+ return s.value;
1543
+ if (isComputed(s))
1544
+ return s.value;
1545
+ if (typeof s === "function")
1546
+ return s();
1547
+ return s;
1548
+ });
1549
+ }
1550
+ if (isRef(source))
1551
+ return source.value;
1552
+ if (isComputed(source))
1553
+ return source.value;
1554
+ if (typeof source === "function")
1555
+ return source();
1556
+ return source;
1557
+ };
1558
+ let oldValue;
1559
+ let initialized = false;
1560
+ const stopEffect = watchEffect(() => {
1561
+ let currentValue = getter();
1562
+ if (deep) {
1563
+ traverse(currentValue);
1564
+ }
1565
+ const job = () => {
1566
+ if (initialized) {
1567
+ let hasChanged = false;
1568
+ if (isMultiSource) {
1569
+ if (!Array.isArray(oldValue) || currentValue.length !== oldValue.length) {
1570
+ hasChanged = true;
1571
+ } else {
1572
+ hasChanged = currentValue.some((val, i) => {
1573
+ return deep ? !deepEqual(val, oldValue[i]) : val !== oldValue[i];
1574
+ });
1575
+ }
1576
+ } else {
1577
+ hasChanged = deep ? !deepEqual(currentValue, oldValue) : currentValue !== oldValue;
1578
+ }
1579
+ if (hasChanged) {
1580
+ const prevOldValue = oldValue;
1581
+ if (isMultiSource) {
1582
+ currentValue = getter();
1583
+ }
1584
+ const valueToClone = currentValue && typeof currentValue === "object" && currentValue.__v_raw ? currentValue.__v_raw : currentValue;
1585
+ oldValue = deep ? deepClone(valueToClone) : currentValue;
1586
+ callback(currentValue, prevOldValue);
1587
+ }
1588
+ } else {
1589
+ const valueToClone = currentValue && typeof currentValue === "object" && currentValue.__v_raw ? currentValue.__v_raw : currentValue;
1590
+ oldValue = deep ? deepClone(valueToClone) : currentValue;
1591
+ initialized = true;
1592
+ if (immediate) {
1593
+ callback(currentValue, undefined);
1594
+ }
1595
+ }
1596
+ };
1597
+ job();
1598
+ });
1599
+ return stopEffect;
1600
+ }
1601
+ // src/mark-raw.ts
1602
+ function markRaw(obj) {
1603
+ if (obj && typeof obj === "object") {
1604
+ Object.defineProperty(obj, "__v_skip" /* SKIP */, {
1605
+ value: true,
1606
+ enumerable: false,
1607
+ configurable: false,
1608
+ writable: false
1609
+ });
1610
+ }
1611
+ return obj;
1612
+ }