@vue/reactivity 3.5.0-beta.1 → 3.5.0-beta.3

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,5 +1,5 @@
1
1
  /**
2
- * @vue/reactivity v3.5.0-beta.1
2
+ * @vue/reactivity v3.5.0-beta.3
3
3
  * (c) 2018-present Yuxi (Evan) You and Vue contributors
4
4
  * @license MIT
5
5
  **/
@@ -154,10 +154,8 @@ const EffectFlags = {
154
154
  "16": "DIRTY",
155
155
  "ALLOW_RECURSE": 32,
156
156
  "32": "ALLOW_RECURSE",
157
- "NO_BATCH": 64,
158
- "64": "NO_BATCH",
159
- "PAUSED": 128,
160
- "128": "PAUSED"
157
+ "PAUSED": 64,
158
+ "64": "PAUSED"
161
159
  };
162
160
  const pausedQueueEffects = /* @__PURE__ */ new WeakSet();
163
161
  class ReactiveEffect {
@@ -189,11 +187,11 @@ class ReactiveEffect {
189
187
  }
190
188
  }
191
189
  pause() {
192
- this.flags |= 128;
190
+ this.flags |= 64;
193
191
  }
194
192
  resume() {
195
- if (this.flags & 128) {
196
- this.flags &= ~128;
193
+ if (this.flags & 64) {
194
+ this.flags &= ~64;
197
195
  if (pausedQueueEffects.has(this)) {
198
196
  pausedQueueEffects.delete(this);
199
197
  this.trigger();
@@ -207,9 +205,6 @@ class ReactiveEffect {
207
205
  if (this.flags & 2 && !(this.flags & 32)) {
208
206
  return;
209
207
  }
210
- if (this.flags & 64) {
211
- return this.trigger();
212
- }
213
208
  if (!(this.flags & 8)) {
214
209
  this.flags |= 8;
215
210
  this.nextEffect = batchedEffect;
@@ -253,7 +248,7 @@ class ReactiveEffect {
253
248
  }
254
249
  }
255
250
  trigger() {
256
- if (this.flags & 128) {
251
+ if (this.flags & 64) {
257
252
  pausedQueueEffects.add(this);
258
253
  } else if (this.scheduler) {
259
254
  this.scheduler();
@@ -283,6 +278,7 @@ function endBatch() {
283
278
  batchDepth--;
284
279
  return;
285
280
  }
281
+ batchDepth--;
286
282
  let error;
287
283
  while (batchedEffect) {
288
284
  let e = batchedEffect;
@@ -301,7 +297,6 @@ function endBatch() {
301
297
  e = next;
302
298
  }
303
299
  }
304
- batchDepth--;
305
300
  if (error) throw error;
306
301
  }
307
302
  function prepareDeps(sub) {
@@ -717,26 +712,26 @@ const arrayInstrumentations = {
717
712
  });
718
713
  },
719
714
  every(fn, thisArg) {
720
- return apply(this, "every", fn, thisArg);
715
+ return apply(this, "every", fn, thisArg, void 0, arguments);
721
716
  },
722
717
  filter(fn, thisArg) {
723
- return apply(this, "filter", fn, thisArg, (v) => v.map(toReactive));
718
+ return apply(this, "filter", fn, thisArg, (v) => v.map(toReactive), arguments);
724
719
  },
725
720
  find(fn, thisArg) {
726
- return apply(this, "find", fn, thisArg, toReactive);
721
+ return apply(this, "find", fn, thisArg, toReactive, arguments);
727
722
  },
728
723
  findIndex(fn, thisArg) {
729
- return apply(this, "findIndex", fn, thisArg);
724
+ return apply(this, "findIndex", fn, thisArg, void 0, arguments);
730
725
  },
731
726
  findLast(fn, thisArg) {
732
- return apply(this, "findLast", fn, thisArg, toReactive);
727
+ return apply(this, "findLast", fn, thisArg, toReactive, arguments);
733
728
  },
734
729
  findLastIndex(fn, thisArg) {
735
- return apply(this, "findLastIndex", fn, thisArg);
730
+ return apply(this, "findLastIndex", fn, thisArg, void 0, arguments);
736
731
  },
737
732
  // flat, flatMap could benefit from ARRAY_ITERATE but are not straight-forward to implement
738
733
  forEach(fn, thisArg) {
739
- return apply(this, "forEach", fn, thisArg);
734
+ return apply(this, "forEach", fn, thisArg, void 0, arguments);
740
735
  },
741
736
  includes(...args) {
742
737
  return searchProxy(this, "includes", args);
@@ -752,7 +747,7 @@ const arrayInstrumentations = {
752
747
  return searchProxy(this, "lastIndexOf", args);
753
748
  },
754
749
  map(fn, thisArg) {
755
- return apply(this, "map", fn, thisArg);
750
+ return apply(this, "map", fn, thisArg, void 0, arguments);
756
751
  },
757
752
  pop() {
758
753
  return noTracking(this, "pop");
@@ -771,7 +766,7 @@ const arrayInstrumentations = {
771
766
  },
772
767
  // slice could use ARRAY_ITERATE but also seems to beg for range tracking
773
768
  some(fn, thisArg) {
774
- return apply(this, "some", fn, thisArg);
769
+ return apply(this, "some", fn, thisArg, void 0, arguments);
775
770
  },
776
771
  splice(...args) {
777
772
  return noTracking(this, "splice", args);
@@ -807,12 +802,17 @@ function iterator(self, method, wrapValue) {
807
802
  }
808
803
  return iter;
809
804
  }
810
- function apply(self, method, fn, thisArg, wrappedRetFn) {
805
+ const arrayProto = Array.prototype;
806
+ function apply(self, method, fn, thisArg, wrappedRetFn, args) {
811
807
  const arr = shallowReadArray(self);
812
- let needsWrap = false;
808
+ const needsWrap = arr !== self && !isShallow(self);
809
+ const methodFn = arr[method];
810
+ if (methodFn !== arrayProto[method]) {
811
+ const result2 = methodFn.apply(arr, args);
812
+ return needsWrap ? toReactive(result2) : result2;
813
+ }
813
814
  let wrappedFn = fn;
814
815
  if (arr !== self) {
815
- needsWrap = !isShallow(self);
816
816
  if (needsWrap) {
817
817
  wrappedFn = function(item, index) {
818
818
  return fn.call(this, toReactive(item), index, self);
@@ -823,7 +823,7 @@ function apply(self, method, fn, thisArg, wrappedRetFn) {
823
823
  };
824
824
  }
825
825
  }
826
- const result = arr[method](wrappedFn, thisArg);
826
+ const result = methodFn.call(arr, wrappedFn, thisArg);
827
827
  return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result;
828
828
  }
829
829
  function reduce(self, method, fn, args) {
@@ -1682,6 +1682,228 @@ const ReactiveFlags = {
1682
1682
  "IS_REF": "__v_isRef"
1683
1683
  };
1684
1684
 
1685
+ const WatchErrorCodes = {
1686
+ "WATCH_GETTER": 2,
1687
+ "2": "WATCH_GETTER",
1688
+ "WATCH_CALLBACK": 3,
1689
+ "3": "WATCH_CALLBACK",
1690
+ "WATCH_CLEANUP": 4,
1691
+ "4": "WATCH_CLEANUP"
1692
+ };
1693
+ const INITIAL_WATCHER_VALUE = {};
1694
+ const cleanupMap = /* @__PURE__ */ new WeakMap();
1695
+ let activeWatcher = void 0;
1696
+ function getCurrentWatcher() {
1697
+ return activeWatcher;
1698
+ }
1699
+ function onWatcherCleanup(cleanupFn, failSilently = false, owner = activeWatcher) {
1700
+ if (owner) {
1701
+ let cleanups = cleanupMap.get(owner);
1702
+ if (!cleanups) cleanupMap.set(owner, cleanups = []);
1703
+ cleanups.push(cleanupFn);
1704
+ } else if (!failSilently) {
1705
+ warn(
1706
+ `onWatcherCleanup() was called when there was no active watcher to associate with.`
1707
+ );
1708
+ }
1709
+ }
1710
+ function watch(source, cb, options = shared.EMPTY_OBJ) {
1711
+ const { immediate, deep, once, scheduler, augmentJob, call } = options;
1712
+ const warnInvalidSource = (s) => {
1713
+ (options.onWarn || warn)(
1714
+ `Invalid watch source: `,
1715
+ s,
1716
+ `A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types.`
1717
+ );
1718
+ };
1719
+ const reactiveGetter = (source2) => {
1720
+ if (deep) return source2;
1721
+ if (isShallow(source2) || deep === false || deep === 0)
1722
+ return traverse(source2, 1);
1723
+ return traverse(source2);
1724
+ };
1725
+ let effect;
1726
+ let getter;
1727
+ let cleanup;
1728
+ let boundCleanup;
1729
+ let forceTrigger = false;
1730
+ let isMultiSource = false;
1731
+ if (isRef(source)) {
1732
+ getter = () => source.value;
1733
+ forceTrigger = isShallow(source);
1734
+ } else if (isReactive(source)) {
1735
+ getter = () => reactiveGetter(source);
1736
+ forceTrigger = true;
1737
+ } else if (shared.isArray(source)) {
1738
+ isMultiSource = true;
1739
+ forceTrigger = source.some((s) => isReactive(s) || isShallow(s));
1740
+ getter = () => source.map((s) => {
1741
+ if (isRef(s)) {
1742
+ return s.value;
1743
+ } else if (isReactive(s)) {
1744
+ return reactiveGetter(s);
1745
+ } else if (shared.isFunction(s)) {
1746
+ return call ? call(s, 2) : s();
1747
+ } else {
1748
+ warnInvalidSource(s);
1749
+ }
1750
+ });
1751
+ } else if (shared.isFunction(source)) {
1752
+ if (cb) {
1753
+ getter = call ? () => call(source, 2) : source;
1754
+ } else {
1755
+ getter = () => {
1756
+ if (cleanup) {
1757
+ pauseTracking();
1758
+ try {
1759
+ cleanup();
1760
+ } finally {
1761
+ resetTracking();
1762
+ }
1763
+ }
1764
+ const currentEffect = activeWatcher;
1765
+ activeWatcher = effect;
1766
+ try {
1767
+ return call ? call(source, 3, [boundCleanup]) : source(boundCleanup);
1768
+ } finally {
1769
+ activeWatcher = currentEffect;
1770
+ }
1771
+ };
1772
+ }
1773
+ } else {
1774
+ getter = shared.NOOP;
1775
+ warnInvalidSource(source);
1776
+ }
1777
+ if (cb && deep) {
1778
+ const baseGetter = getter;
1779
+ const depth = deep === true ? Infinity : deep;
1780
+ getter = () => traverse(baseGetter(), depth);
1781
+ }
1782
+ if (once) {
1783
+ if (cb) {
1784
+ const _cb = cb;
1785
+ cb = (...args) => {
1786
+ _cb(...args);
1787
+ effect.stop();
1788
+ };
1789
+ } else {
1790
+ const _getter = getter;
1791
+ getter = () => {
1792
+ _getter();
1793
+ effect.stop();
1794
+ };
1795
+ }
1796
+ }
1797
+ let oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
1798
+ const job = (immediateFirstRun) => {
1799
+ if (!(effect.flags & 1) || !effect.dirty && !immediateFirstRun) {
1800
+ return;
1801
+ }
1802
+ if (cb) {
1803
+ const newValue = effect.run();
1804
+ if (deep || forceTrigger || (isMultiSource ? newValue.some((v, i) => shared.hasChanged(v, oldValue[i])) : shared.hasChanged(newValue, oldValue))) {
1805
+ if (cleanup) {
1806
+ cleanup();
1807
+ }
1808
+ const currentWatcher = activeWatcher;
1809
+ activeWatcher = effect;
1810
+ try {
1811
+ const args = [
1812
+ newValue,
1813
+ // pass undefined as the old value when it's changed for the first time
1814
+ oldValue === INITIAL_WATCHER_VALUE ? void 0 : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE ? [] : oldValue,
1815
+ boundCleanup
1816
+ ];
1817
+ call ? call(cb, 3, args) : (
1818
+ // @ts-expect-error
1819
+ cb(...args)
1820
+ );
1821
+ oldValue = newValue;
1822
+ } finally {
1823
+ activeWatcher = currentWatcher;
1824
+ }
1825
+ }
1826
+ } else {
1827
+ effect.run();
1828
+ }
1829
+ };
1830
+ if (augmentJob) {
1831
+ augmentJob(job);
1832
+ }
1833
+ effect = new ReactiveEffect(getter);
1834
+ effect.scheduler = scheduler ? () => scheduler(job, false) : job;
1835
+ boundCleanup = (fn) => onWatcherCleanup(fn, false, effect);
1836
+ cleanup = effect.onStop = () => {
1837
+ const cleanups = cleanupMap.get(effect);
1838
+ if (cleanups) {
1839
+ if (call) {
1840
+ call(cleanups, 4);
1841
+ } else {
1842
+ for (const cleanup2 of cleanups) cleanup2();
1843
+ }
1844
+ cleanupMap.delete(effect);
1845
+ }
1846
+ };
1847
+ {
1848
+ effect.onTrack = options.onTrack;
1849
+ effect.onTrigger = options.onTrigger;
1850
+ }
1851
+ if (cb) {
1852
+ if (immediate) {
1853
+ job(true);
1854
+ } else {
1855
+ oldValue = effect.run();
1856
+ }
1857
+ } else if (scheduler) {
1858
+ scheduler(job.bind(null, true), true);
1859
+ } else {
1860
+ effect.run();
1861
+ }
1862
+ const scope = getCurrentScope();
1863
+ const watchHandle = () => {
1864
+ effect.stop();
1865
+ if (scope) {
1866
+ shared.remove(scope.effects, effect);
1867
+ }
1868
+ };
1869
+ watchHandle.pause = effect.pause.bind(effect);
1870
+ watchHandle.resume = effect.resume.bind(effect);
1871
+ watchHandle.stop = watchHandle;
1872
+ return watchHandle;
1873
+ }
1874
+ function traverse(value, depth = Infinity, seen) {
1875
+ if (depth <= 0 || !shared.isObject(value) || value["__v_skip"]) {
1876
+ return value;
1877
+ }
1878
+ seen = seen || /* @__PURE__ */ new Set();
1879
+ if (seen.has(value)) {
1880
+ return value;
1881
+ }
1882
+ seen.add(value);
1883
+ depth--;
1884
+ if (isRef(value)) {
1885
+ traverse(value.value, depth, seen);
1886
+ } else if (shared.isArray(value)) {
1887
+ for (let i = 0; i < value.length; i++) {
1888
+ traverse(value[i], depth, seen);
1889
+ }
1890
+ } else if (shared.isSet(value) || shared.isMap(value)) {
1891
+ value.forEach((v) => {
1892
+ traverse(v, depth, seen);
1893
+ });
1894
+ } else if (shared.isPlainObject(value)) {
1895
+ for (const key in value) {
1896
+ traverse(value[key], depth, seen);
1897
+ }
1898
+ for (const key of Object.getOwnPropertySymbols(value)) {
1899
+ if (Object.prototype.propertyIsEnumerable.call(value, key)) {
1900
+ traverse(value[key], depth, seen);
1901
+ }
1902
+ }
1903
+ }
1904
+ return value;
1905
+ }
1906
+
1685
1907
  exports.ARRAY_ITERATE_KEY = ARRAY_ITERATE_KEY;
1686
1908
  exports.EffectFlags = EffectFlags;
1687
1909
  exports.EffectScope = EffectScope;
@@ -1691,12 +1913,14 @@ exports.ReactiveEffect = ReactiveEffect;
1691
1913
  exports.ReactiveFlags = ReactiveFlags;
1692
1914
  exports.TrackOpTypes = TrackOpTypes;
1693
1915
  exports.TriggerOpTypes = TriggerOpTypes;
1916
+ exports.WatchErrorCodes = WatchErrorCodes;
1694
1917
  exports.computed = computed;
1695
1918
  exports.customRef = customRef;
1696
1919
  exports.effect = effect;
1697
1920
  exports.effectScope = effectScope;
1698
1921
  exports.enableTracking = enableTracking;
1699
1922
  exports.getCurrentScope = getCurrentScope;
1923
+ exports.getCurrentWatcher = getCurrentWatcher;
1700
1924
  exports.isProxy = isProxy;
1701
1925
  exports.isReactive = isReactive;
1702
1926
  exports.isReadonly = isReadonly;
@@ -1705,6 +1929,7 @@ exports.isShallow = isShallow;
1705
1929
  exports.markRaw = markRaw;
1706
1930
  exports.onEffectCleanup = onEffectCleanup;
1707
1931
  exports.onScopeDispose = onScopeDispose;
1932
+ exports.onWatcherCleanup = onWatcherCleanup;
1708
1933
  exports.pauseTracking = pauseTracking;
1709
1934
  exports.proxyRefs = proxyRefs;
1710
1935
  exports.reactive = reactive;
@@ -1724,6 +1949,8 @@ exports.toRef = toRef;
1724
1949
  exports.toRefs = toRefs;
1725
1950
  exports.toValue = toValue;
1726
1951
  exports.track = track;
1952
+ exports.traverse = traverse;
1727
1953
  exports.trigger = trigger;
1728
1954
  exports.triggerRef = triggerRef;
1729
1955
  exports.unref = unref;
1956
+ exports.watch = watch;