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