@vue/reactivity 3.5.10 → 3.5.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
1
  /**
2
- * @vue/reactivity v3.5.10
2
+ * @vue/reactivity v3.5.12
3
3
  * (c) 2018-present Yuxi (Evan) You and Vue contributors
4
4
  * @license MIT
5
5
  **/
6
- import { hasChanged, extend, isArray, isIntegerKey, isSymbol, isMap, hasOwn, isObject, makeMap, capitalize, toRawType, def, isFunction, EMPTY_OBJ, isSet, isPlainObject, NOOP, remove } from '@vue/shared';
6
+ import { hasChanged, extend, isArray, isIntegerKey, isSymbol, isMap, hasOwn, isObject, makeMap, toRawType, capitalize, def, isFunction, EMPTY_OBJ, isSet, isPlainObject, NOOP, remove } from '@vue/shared';
7
7
 
8
8
  function warn(msg, ...args) {
9
9
  console.warn(`[Vue warn] ${msg}`, ...args);
@@ -266,8 +266,14 @@ class ReactiveEffect {
266
266
  }
267
267
  let batchDepth = 0;
268
268
  let batchedSub;
269
- function batch(sub) {
269
+ let batchedComputed;
270
+ function batch(sub, isComputed = false) {
270
271
  sub.flags |= 8;
272
+ if (isComputed) {
273
+ sub.next = batchedComputed;
274
+ batchedComputed = sub;
275
+ return;
276
+ }
271
277
  sub.next = batchedSub;
272
278
  batchedSub = sub;
273
279
  }
@@ -278,20 +284,22 @@ function endBatch() {
278
284
  if (--batchDepth > 0) {
279
285
  return;
280
286
  }
287
+ if (batchedComputed) {
288
+ let e = batchedComputed;
289
+ batchedComputed = void 0;
290
+ while (e) {
291
+ const next = e.next;
292
+ e.next = void 0;
293
+ e.flags &= ~8;
294
+ e = next;
295
+ }
296
+ }
281
297
  let error;
282
298
  while (batchedSub) {
283
299
  let e = batchedSub;
284
- let next;
285
- while (e) {
286
- if (!(e.flags & 1)) {
287
- e.flags &= ~8;
288
- }
289
- e = e.next;
290
- }
291
- e = batchedSub;
292
300
  batchedSub = void 0;
293
301
  while (e) {
294
- next = e.next;
302
+ const next = e.next;
295
303
  e.next = void 0;
296
304
  e.flags &= ~8;
297
305
  if (e.flags & 1) {
@@ -391,16 +399,16 @@ function removeSub(link, soft = false) {
391
399
  nextSub.prevSub = prevSub;
392
400
  link.nextSub = void 0;
393
401
  }
394
- if (dep.subs === link) {
395
- dep.subs = prevSub;
396
- }
397
402
  if (!!(process.env.NODE_ENV !== "production") && dep.subsHead === link) {
398
403
  dep.subsHead = nextSub;
399
404
  }
400
- if (!dep.subs && dep.computed) {
401
- dep.computed.flags &= ~4;
402
- for (let l = dep.computed.deps; l; l = l.nextDep) {
403
- removeSub(l, true);
405
+ if (dep.subs === link) {
406
+ dep.subs = prevSub;
407
+ if (!prevSub && dep.computed) {
408
+ dep.computed.flags &= ~4;
409
+ for (let l = dep.computed.deps; l; l = l.nextDep) {
410
+ removeSub(l, true);
411
+ }
404
412
  }
405
413
  }
406
414
  if (!soft && !--dep.sc && dep.map) {
@@ -500,7 +508,6 @@ class Dep {
500
508
  /**
501
509
  * For object property deps cleanup
502
510
  */
503
- this.target = void 0;
504
511
  this.map = void 0;
505
512
  this.key = void 0;
506
513
  /**
@@ -628,7 +635,6 @@ function track(target, type, key) {
628
635
  let dep = depsMap.get(key);
629
636
  if (!dep) {
630
637
  depsMap.set(key, dep = new Dep());
631
- dep.target = target;
632
638
  dep.map = depsMap;
633
639
  dep.key = key;
634
640
  }
@@ -679,7 +685,7 @@ function trigger(target, type, key, newValue, oldValue, oldTarget) {
679
685
  }
680
686
  });
681
687
  } else {
682
- if (key !== void 0) {
688
+ if (key !== void 0 || depsMap.has(void 0)) {
683
689
  run(depsMap.get(key));
684
690
  }
685
691
  if (isArrayIndex) {
@@ -1054,117 +1060,6 @@ const shallowReadonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler(true
1054
1060
 
1055
1061
  const toShallow = (value) => value;
1056
1062
  const getProto = (v) => Reflect.getPrototypeOf(v);
1057
- function get(target, key, isReadonly2 = false, isShallow2 = false) {
1058
- target = target["__v_raw"];
1059
- const rawTarget = toRaw(target);
1060
- const rawKey = toRaw(key);
1061
- if (!isReadonly2) {
1062
- if (hasChanged(key, rawKey)) {
1063
- track(rawTarget, "get", key);
1064
- }
1065
- track(rawTarget, "get", rawKey);
1066
- }
1067
- const { has: has2 } = getProto(rawTarget);
1068
- const wrap = isShallow2 ? toShallow : isReadonly2 ? toReadonly : toReactive;
1069
- if (has2.call(rawTarget, key)) {
1070
- return wrap(target.get(key));
1071
- } else if (has2.call(rawTarget, rawKey)) {
1072
- return wrap(target.get(rawKey));
1073
- } else if (target !== rawTarget) {
1074
- target.get(key);
1075
- }
1076
- }
1077
- function has(key, isReadonly2 = false) {
1078
- const target = this["__v_raw"];
1079
- const rawTarget = toRaw(target);
1080
- const rawKey = toRaw(key);
1081
- if (!isReadonly2) {
1082
- if (hasChanged(key, rawKey)) {
1083
- track(rawTarget, "has", key);
1084
- }
1085
- track(rawTarget, "has", rawKey);
1086
- }
1087
- return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
1088
- }
1089
- function size(target, isReadonly2 = false) {
1090
- target = target["__v_raw"];
1091
- !isReadonly2 && track(toRaw(target), "iterate", ITERATE_KEY);
1092
- return Reflect.get(target, "size", target);
1093
- }
1094
- function add(value, _isShallow = false) {
1095
- if (!_isShallow && !isShallow(value) && !isReadonly(value)) {
1096
- value = toRaw(value);
1097
- }
1098
- const target = toRaw(this);
1099
- const proto = getProto(target);
1100
- const hadKey = proto.has.call(target, value);
1101
- if (!hadKey) {
1102
- target.add(value);
1103
- trigger(target, "add", value, value);
1104
- }
1105
- return this;
1106
- }
1107
- function set(key, value, _isShallow = false) {
1108
- if (!_isShallow && !isShallow(value) && !isReadonly(value)) {
1109
- value = toRaw(value);
1110
- }
1111
- const target = toRaw(this);
1112
- const { has: has2, get: get2 } = getProto(target);
1113
- let hadKey = has2.call(target, key);
1114
- if (!hadKey) {
1115
- key = toRaw(key);
1116
- hadKey = has2.call(target, key);
1117
- } else if (!!(process.env.NODE_ENV !== "production")) {
1118
- checkIdentityKeys(target, has2, key);
1119
- }
1120
- const oldValue = get2.call(target, key);
1121
- target.set(key, value);
1122
- if (!hadKey) {
1123
- trigger(target, "add", key, value);
1124
- } else if (hasChanged(value, oldValue)) {
1125
- trigger(target, "set", key, value, oldValue);
1126
- }
1127
- return this;
1128
- }
1129
- function deleteEntry(key) {
1130
- const target = toRaw(this);
1131
- const { has: has2, get: get2 } = getProto(target);
1132
- let hadKey = has2.call(target, key);
1133
- if (!hadKey) {
1134
- key = toRaw(key);
1135
- hadKey = has2.call(target, key);
1136
- } else if (!!(process.env.NODE_ENV !== "production")) {
1137
- checkIdentityKeys(target, has2, key);
1138
- }
1139
- const oldValue = get2 ? get2.call(target, key) : void 0;
1140
- const result = target.delete(key);
1141
- if (hadKey) {
1142
- trigger(target, "delete", key, void 0, oldValue);
1143
- }
1144
- return result;
1145
- }
1146
- function clear() {
1147
- const target = toRaw(this);
1148
- const hadItems = target.size !== 0;
1149
- const oldTarget = !!(process.env.NODE_ENV !== "production") ? isMap(target) ? new Map(target) : new Set(target) : void 0;
1150
- const result = target.clear();
1151
- if (hadItems) {
1152
- trigger(target, "clear", void 0, void 0, oldTarget);
1153
- }
1154
- return result;
1155
- }
1156
- function createForEach(isReadonly2, isShallow2) {
1157
- return function forEach(callback, thisArg) {
1158
- const observed = this;
1159
- const target = observed["__v_raw"];
1160
- const rawTarget = toRaw(target);
1161
- const wrap = isShallow2 ? toShallow : isReadonly2 ? toReadonly : toReactive;
1162
- !isReadonly2 && track(rawTarget, "iterate", ITERATE_KEY);
1163
- return target.forEach((value, key) => {
1164
- return callback.call(thisArg, wrap(value), wrap(key), observed);
1165
- });
1166
- };
1167
- }
1168
1063
  function createIterableMethod(method, isReadonly2, isShallow2) {
1169
1064
  return function(...args) {
1170
1065
  const target = this["__v_raw"];
@@ -1207,71 +1102,134 @@ function createReadonlyMethod(type) {
1207
1102
  return type === "delete" ? false : type === "clear" ? void 0 : this;
1208
1103
  };
1209
1104
  }
1210
- function createInstrumentations() {
1211
- const mutableInstrumentations2 = {
1212
- get(key) {
1213
- return get(this, key);
1214
- },
1215
- get size() {
1216
- return size(this);
1217
- },
1218
- has,
1219
- add,
1220
- set,
1221
- delete: deleteEntry,
1222
- clear,
1223
- forEach: createForEach(false, false)
1224
- };
1225
- const shallowInstrumentations2 = {
1226
- get(key) {
1227
- return get(this, key, false, true);
1228
- },
1229
- get size() {
1230
- return size(this);
1231
- },
1232
- has,
1233
- add(value) {
1234
- return add.call(this, value, true);
1235
- },
1236
- set(key, value) {
1237
- return set.call(this, key, value, true);
1238
- },
1239
- delete: deleteEntry,
1240
- clear,
1241
- forEach: createForEach(false, true)
1242
- };
1243
- const readonlyInstrumentations2 = {
1244
- get(key) {
1245
- return get(this, key, true);
1246
- },
1247
- get size() {
1248
- return size(this, true);
1249
- },
1250
- has(key) {
1251
- return has.call(this, key, true);
1252
- },
1253
- add: createReadonlyMethod("add"),
1254
- set: createReadonlyMethod("set"),
1255
- delete: createReadonlyMethod("delete"),
1256
- clear: createReadonlyMethod("clear"),
1257
- forEach: createForEach(true, false)
1258
- };
1259
- const shallowReadonlyInstrumentations2 = {
1105
+ function createInstrumentations(readonly, shallow) {
1106
+ const instrumentations = {
1260
1107
  get(key) {
1261
- return get(this, key, true, true);
1108
+ const target = this["__v_raw"];
1109
+ const rawTarget = toRaw(target);
1110
+ const rawKey = toRaw(key);
1111
+ if (!readonly) {
1112
+ if (hasChanged(key, rawKey)) {
1113
+ track(rawTarget, "get", key);
1114
+ }
1115
+ track(rawTarget, "get", rawKey);
1116
+ }
1117
+ const { has } = getProto(rawTarget);
1118
+ const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
1119
+ if (has.call(rawTarget, key)) {
1120
+ return wrap(target.get(key));
1121
+ } else if (has.call(rawTarget, rawKey)) {
1122
+ return wrap(target.get(rawKey));
1123
+ } else if (target !== rawTarget) {
1124
+ target.get(key);
1125
+ }
1262
1126
  },
1263
1127
  get size() {
1264
- return size(this, true);
1128
+ const target = this["__v_raw"];
1129
+ !readonly && track(toRaw(target), "iterate", ITERATE_KEY);
1130
+ return Reflect.get(target, "size", target);
1265
1131
  },
1266
1132
  has(key) {
1267
- return has.call(this, key, true);
1133
+ const target = this["__v_raw"];
1134
+ const rawTarget = toRaw(target);
1135
+ const rawKey = toRaw(key);
1136
+ if (!readonly) {
1137
+ if (hasChanged(key, rawKey)) {
1138
+ track(rawTarget, "has", key);
1139
+ }
1140
+ track(rawTarget, "has", rawKey);
1141
+ }
1142
+ return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
1268
1143
  },
1269
- add: createReadonlyMethod("add"),
1270
- set: createReadonlyMethod("set"),
1271
- delete: createReadonlyMethod("delete"),
1272
- clear: createReadonlyMethod("clear"),
1273
- forEach: createForEach(true, true)
1144
+ forEach(callback, thisArg) {
1145
+ const observed = this;
1146
+ const target = observed["__v_raw"];
1147
+ const rawTarget = toRaw(target);
1148
+ const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
1149
+ !readonly && track(rawTarget, "iterate", ITERATE_KEY);
1150
+ return target.forEach((value, key) => {
1151
+ return callback.call(thisArg, wrap(value), wrap(key), observed);
1152
+ });
1153
+ }
1274
1154
  };
1155
+ extend(
1156
+ instrumentations,
1157
+ readonly ? {
1158
+ add: createReadonlyMethod("add"),
1159
+ set: createReadonlyMethod("set"),
1160
+ delete: createReadonlyMethod("delete"),
1161
+ clear: createReadonlyMethod("clear")
1162
+ } : {
1163
+ add(value) {
1164
+ if (!shallow && !isShallow(value) && !isReadonly(value)) {
1165
+ value = toRaw(value);
1166
+ }
1167
+ const target = toRaw(this);
1168
+ const proto = getProto(target);
1169
+ const hadKey = proto.has.call(target, value);
1170
+ if (!hadKey) {
1171
+ target.add(value);
1172
+ trigger(target, "add", value, value);
1173
+ }
1174
+ return this;
1175
+ },
1176
+ set(key, value) {
1177
+ if (!shallow && !isShallow(value) && !isReadonly(value)) {
1178
+ value = toRaw(value);
1179
+ }
1180
+ const target = toRaw(this);
1181
+ const { has, get } = getProto(target);
1182
+ let hadKey = has.call(target, key);
1183
+ if (!hadKey) {
1184
+ key = toRaw(key);
1185
+ hadKey = has.call(target, key);
1186
+ } else if (!!(process.env.NODE_ENV !== "production")) {
1187
+ checkIdentityKeys(target, has, key);
1188
+ }
1189
+ const oldValue = get.call(target, key);
1190
+ target.set(key, value);
1191
+ if (!hadKey) {
1192
+ trigger(target, "add", key, value);
1193
+ } else if (hasChanged(value, oldValue)) {
1194
+ trigger(target, "set", key, value, oldValue);
1195
+ }
1196
+ return this;
1197
+ },
1198
+ delete(key) {
1199
+ const target = toRaw(this);
1200
+ const { has, get } = getProto(target);
1201
+ let hadKey = has.call(target, key);
1202
+ if (!hadKey) {
1203
+ key = toRaw(key);
1204
+ hadKey = has.call(target, key);
1205
+ } else if (!!(process.env.NODE_ENV !== "production")) {
1206
+ checkIdentityKeys(target, has, key);
1207
+ }
1208
+ const oldValue = get ? get.call(target, key) : void 0;
1209
+ const result = target.delete(key);
1210
+ if (hadKey) {
1211
+ trigger(target, "delete", key, void 0, oldValue);
1212
+ }
1213
+ return result;
1214
+ },
1215
+ clear() {
1216
+ const target = toRaw(this);
1217
+ const hadItems = target.size !== 0;
1218
+ const oldTarget = !!(process.env.NODE_ENV !== "production") ? isMap(target) ? new Map(target) : new Set(target) : void 0;
1219
+ const result = target.clear();
1220
+ if (hadItems) {
1221
+ trigger(
1222
+ target,
1223
+ "clear",
1224
+ void 0,
1225
+ void 0,
1226
+ oldTarget
1227
+ );
1228
+ }
1229
+ return result;
1230
+ }
1231
+ }
1232
+ );
1275
1233
  const iteratorMethods = [
1276
1234
  "keys",
1277
1235
  "values",
@@ -1279,30 +1237,12 @@ function createInstrumentations() {
1279
1237
  Symbol.iterator
1280
1238
  ];
1281
1239
  iteratorMethods.forEach((method) => {
1282
- mutableInstrumentations2[method] = createIterableMethod(method, false, false);
1283
- readonlyInstrumentations2[method] = createIterableMethod(method, true, false);
1284
- shallowInstrumentations2[method] = createIterableMethod(method, false, true);
1285
- shallowReadonlyInstrumentations2[method] = createIterableMethod(
1286
- method,
1287
- true,
1288
- true
1289
- );
1240
+ instrumentations[method] = createIterableMethod(method, readonly, shallow);
1290
1241
  });
1291
- return [
1292
- mutableInstrumentations2,
1293
- readonlyInstrumentations2,
1294
- shallowInstrumentations2,
1295
- shallowReadonlyInstrumentations2
1296
- ];
1242
+ return instrumentations;
1297
1243
  }
1298
- const [
1299
- mutableInstrumentations,
1300
- readonlyInstrumentations,
1301
- shallowInstrumentations,
1302
- shallowReadonlyInstrumentations
1303
- ] = /* @__PURE__ */ createInstrumentations();
1304
1244
  function createInstrumentationGetter(isReadonly2, shallow) {
1305
- const instrumentations = shallow ? isReadonly2 ? shallowReadonlyInstrumentations : shallowInstrumentations : isReadonly2 ? readonlyInstrumentations : mutableInstrumentations;
1245
+ const instrumentations = createInstrumentations(isReadonly2, shallow);
1306
1246
  return (target, key, receiver) => {
1307
1247
  if (key === "__v_isReactive") {
1308
1248
  return !isReadonly2;
@@ -1330,9 +1270,9 @@ const readonlyCollectionHandlers = {
1330
1270
  const shallowReadonlyCollectionHandlers = {
1331
1271
  get: /* @__PURE__ */ createInstrumentationGetter(true, true)
1332
1272
  };
1333
- function checkIdentityKeys(target, has2, key) {
1273
+ function checkIdentityKeys(target, has, key) {
1334
1274
  const rawKey = toRaw(key);
1335
- if (rawKey !== key && has2.call(target, rawKey)) {
1275
+ if (rawKey !== key && has.call(target, rawKey)) {
1336
1276
  const type = toRawType(target);
1337
1277
  warn(
1338
1278
  `Reactive ${type} contains both the raw and reactive versions of the same object${type === `Map` ? ` as keys` : ``}, which can lead to inconsistencies. Avoid differentiating between the raw and reactive versions of an object and only use the reactive version if possible.`
@@ -1674,7 +1614,7 @@ class ComputedRefImpl {
1674
1614
  this.flags |= 16;
1675
1615
  if (!(this.flags & 8) && // avoid infinite self recursion
1676
1616
  activeSub !== this) {
1677
- batch(this);
1617
+ batch(this, true);
1678
1618
  return true;
1679
1619
  } else if (!!(process.env.NODE_ENV !== "production")) ;
1680
1620
  }