@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
  **/
@@ -144,10 +144,8 @@ const EffectFlags = {
144
144
  "16": "DIRTY",
145
145
  "ALLOW_RECURSE": 32,
146
146
  "32": "ALLOW_RECURSE",
147
- "NO_BATCH": 64,
148
- "64": "NO_BATCH",
149
- "PAUSED": 128,
150
- "128": "PAUSED"
147
+ "PAUSED": 64,
148
+ "64": "PAUSED"
151
149
  };
152
150
  const pausedQueueEffects = /* @__PURE__ */ new WeakSet();
153
151
  class ReactiveEffect {
@@ -179,11 +177,11 @@ class ReactiveEffect {
179
177
  }
180
178
  }
181
179
  pause() {
182
- this.flags |= 128;
180
+ this.flags |= 64;
183
181
  }
184
182
  resume() {
185
- if (this.flags & 128) {
186
- this.flags &= ~128;
183
+ if (this.flags & 64) {
184
+ this.flags &= ~64;
187
185
  if (pausedQueueEffects.has(this)) {
188
186
  pausedQueueEffects.delete(this);
189
187
  this.trigger();
@@ -197,9 +195,6 @@ class ReactiveEffect {
197
195
  if (this.flags & 2 && !(this.flags & 32)) {
198
196
  return;
199
197
  }
200
- if (this.flags & 64) {
201
- return this.trigger();
202
- }
203
198
  if (!(this.flags & 8)) {
204
199
  this.flags |= 8;
205
200
  this.nextEffect = batchedEffect;
@@ -238,7 +233,7 @@ class ReactiveEffect {
238
233
  }
239
234
  }
240
235
  trigger() {
241
- if (this.flags & 128) {
236
+ if (this.flags & 64) {
242
237
  pausedQueueEffects.add(this);
243
238
  } else if (this.scheduler) {
244
239
  this.scheduler();
@@ -268,6 +263,7 @@ function endBatch() {
268
263
  batchDepth--;
269
264
  return;
270
265
  }
266
+ batchDepth--;
271
267
  let error;
272
268
  while (batchedEffect) {
273
269
  let e = batchedEffect;
@@ -286,7 +282,6 @@ function endBatch() {
286
282
  e = next;
287
283
  }
288
284
  }
289
- batchDepth--;
290
285
  if (error) throw error;
291
286
  }
292
287
  function prepareDeps(sub) {
@@ -658,26 +653,26 @@ const arrayInstrumentations = {
658
653
  });
659
654
  },
660
655
  every(fn, thisArg) {
661
- return apply(this, "every", fn, thisArg);
656
+ return apply(this, "every", fn, thisArg, void 0, arguments);
662
657
  },
663
658
  filter(fn, thisArg) {
664
- return apply(this, "filter", fn, thisArg, (v) => v.map(toReactive));
659
+ return apply(this, "filter", fn, thisArg, (v) => v.map(toReactive), arguments);
665
660
  },
666
661
  find(fn, thisArg) {
667
- return apply(this, "find", fn, thisArg, toReactive);
662
+ return apply(this, "find", fn, thisArg, toReactive, arguments);
668
663
  },
669
664
  findIndex(fn, thisArg) {
670
- return apply(this, "findIndex", fn, thisArg);
665
+ return apply(this, "findIndex", fn, thisArg, void 0, arguments);
671
666
  },
672
667
  findLast(fn, thisArg) {
673
- return apply(this, "findLast", fn, thisArg, toReactive);
668
+ return apply(this, "findLast", fn, thisArg, toReactive, arguments);
674
669
  },
675
670
  findLastIndex(fn, thisArg) {
676
- return apply(this, "findLastIndex", fn, thisArg);
671
+ return apply(this, "findLastIndex", fn, thisArg, void 0, arguments);
677
672
  },
678
673
  // flat, flatMap could benefit from ARRAY_ITERATE but are not straight-forward to implement
679
674
  forEach(fn, thisArg) {
680
- return apply(this, "forEach", fn, thisArg);
675
+ return apply(this, "forEach", fn, thisArg, void 0, arguments);
681
676
  },
682
677
  includes(...args) {
683
678
  return searchProxy(this, "includes", args);
@@ -693,7 +688,7 @@ const arrayInstrumentations = {
693
688
  return searchProxy(this, "lastIndexOf", args);
694
689
  },
695
690
  map(fn, thisArg) {
696
- return apply(this, "map", fn, thisArg);
691
+ return apply(this, "map", fn, thisArg, void 0, arguments);
697
692
  },
698
693
  pop() {
699
694
  return noTracking(this, "pop");
@@ -712,7 +707,7 @@ const arrayInstrumentations = {
712
707
  },
713
708
  // slice could use ARRAY_ITERATE but also seems to beg for range tracking
714
709
  some(fn, thisArg) {
715
- return apply(this, "some", fn, thisArg);
710
+ return apply(this, "some", fn, thisArg, void 0, arguments);
716
711
  },
717
712
  splice(...args) {
718
713
  return noTracking(this, "splice", args);
@@ -748,12 +743,17 @@ function iterator(self, method, wrapValue) {
748
743
  }
749
744
  return iter;
750
745
  }
751
- function apply(self, method, fn, thisArg, wrappedRetFn) {
746
+ const arrayProto = Array.prototype;
747
+ function apply(self, method, fn, thisArg, wrappedRetFn, args) {
752
748
  const arr = shallowReadArray(self);
753
- let needsWrap = false;
749
+ const needsWrap = arr !== self && !isShallow(self);
750
+ const methodFn = arr[method];
751
+ if (methodFn !== arrayProto[method]) {
752
+ const result2 = methodFn.apply(arr, args);
753
+ return needsWrap ? toReactive(result2) : result2;
754
+ }
754
755
  let wrappedFn = fn;
755
756
  if (arr !== self) {
756
- needsWrap = !isShallow(self);
757
757
  if (needsWrap) {
758
758
  wrappedFn = function(item, index) {
759
759
  return fn.call(this, toReactive(item), index, self);
@@ -764,7 +764,7 @@ function apply(self, method, fn, thisArg, wrappedRetFn) {
764
764
  };
765
765
  }
766
766
  }
767
- const result = arr[method](wrappedFn, thisArg);
767
+ const result = methodFn.call(arr, wrappedFn, thisArg);
768
768
  return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result;
769
769
  }
770
770
  function reduce(self, method, fn, args) {
@@ -1555,6 +1555,210 @@ const ReactiveFlags = {
1555
1555
  "IS_REF": "__v_isRef"
1556
1556
  };
1557
1557
 
1558
+ const WatchErrorCodes = {
1559
+ "WATCH_GETTER": 2,
1560
+ "2": "WATCH_GETTER",
1561
+ "WATCH_CALLBACK": 3,
1562
+ "3": "WATCH_CALLBACK",
1563
+ "WATCH_CLEANUP": 4,
1564
+ "4": "WATCH_CLEANUP"
1565
+ };
1566
+ const INITIAL_WATCHER_VALUE = {};
1567
+ const cleanupMap = /* @__PURE__ */ new WeakMap();
1568
+ let activeWatcher = void 0;
1569
+ function getCurrentWatcher() {
1570
+ return activeWatcher;
1571
+ }
1572
+ function onWatcherCleanup(cleanupFn, failSilently = false, owner = activeWatcher) {
1573
+ if (owner) {
1574
+ let cleanups = cleanupMap.get(owner);
1575
+ if (!cleanups) cleanupMap.set(owner, cleanups = []);
1576
+ cleanups.push(cleanupFn);
1577
+ }
1578
+ }
1579
+ function watch(source, cb, options = shared.EMPTY_OBJ) {
1580
+ const { immediate, deep, once, scheduler, augmentJob, call } = options;
1581
+ const reactiveGetter = (source2) => {
1582
+ if (deep) return source2;
1583
+ if (isShallow(source2) || deep === false || deep === 0)
1584
+ return traverse(source2, 1);
1585
+ return traverse(source2);
1586
+ };
1587
+ let effect;
1588
+ let getter;
1589
+ let cleanup;
1590
+ let boundCleanup;
1591
+ let forceTrigger = false;
1592
+ let isMultiSource = false;
1593
+ if (isRef(source)) {
1594
+ getter = () => source.value;
1595
+ forceTrigger = isShallow(source);
1596
+ } else if (isReactive(source)) {
1597
+ getter = () => reactiveGetter(source);
1598
+ forceTrigger = true;
1599
+ } else if (shared.isArray(source)) {
1600
+ isMultiSource = true;
1601
+ forceTrigger = source.some((s) => isReactive(s) || isShallow(s));
1602
+ getter = () => source.map((s) => {
1603
+ if (isRef(s)) {
1604
+ return s.value;
1605
+ } else if (isReactive(s)) {
1606
+ return reactiveGetter(s);
1607
+ } else if (shared.isFunction(s)) {
1608
+ return call ? call(s, 2) : s();
1609
+ } else ;
1610
+ });
1611
+ } else if (shared.isFunction(source)) {
1612
+ if (cb) {
1613
+ getter = call ? () => call(source, 2) : source;
1614
+ } else {
1615
+ getter = () => {
1616
+ if (cleanup) {
1617
+ pauseTracking();
1618
+ try {
1619
+ cleanup();
1620
+ } finally {
1621
+ resetTracking();
1622
+ }
1623
+ }
1624
+ const currentEffect = activeWatcher;
1625
+ activeWatcher = effect;
1626
+ try {
1627
+ return call ? call(source, 3, [boundCleanup]) : source(boundCleanup);
1628
+ } finally {
1629
+ activeWatcher = currentEffect;
1630
+ }
1631
+ };
1632
+ }
1633
+ } else {
1634
+ getter = shared.NOOP;
1635
+ }
1636
+ if (cb && deep) {
1637
+ const baseGetter = getter;
1638
+ const depth = deep === true ? Infinity : deep;
1639
+ getter = () => traverse(baseGetter(), depth);
1640
+ }
1641
+ if (once) {
1642
+ if (cb) {
1643
+ const _cb = cb;
1644
+ cb = (...args) => {
1645
+ _cb(...args);
1646
+ effect.stop();
1647
+ };
1648
+ } else {
1649
+ const _getter = getter;
1650
+ getter = () => {
1651
+ _getter();
1652
+ effect.stop();
1653
+ };
1654
+ }
1655
+ }
1656
+ let oldValue = isMultiSource ? new Array(source.length).fill(INITIAL_WATCHER_VALUE) : INITIAL_WATCHER_VALUE;
1657
+ const job = (immediateFirstRun) => {
1658
+ if (!(effect.flags & 1) || !effect.dirty && !immediateFirstRun) {
1659
+ return;
1660
+ }
1661
+ if (cb) {
1662
+ const newValue = effect.run();
1663
+ if (deep || forceTrigger || (isMultiSource ? newValue.some((v, i) => shared.hasChanged(v, oldValue[i])) : shared.hasChanged(newValue, oldValue))) {
1664
+ if (cleanup) {
1665
+ cleanup();
1666
+ }
1667
+ const currentWatcher = activeWatcher;
1668
+ activeWatcher = effect;
1669
+ try {
1670
+ const args = [
1671
+ newValue,
1672
+ // pass undefined as the old value when it's changed for the first time
1673
+ oldValue === INITIAL_WATCHER_VALUE ? void 0 : isMultiSource && oldValue[0] === INITIAL_WATCHER_VALUE ? [] : oldValue,
1674
+ boundCleanup
1675
+ ];
1676
+ call ? call(cb, 3, args) : (
1677
+ // @ts-expect-error
1678
+ cb(...args)
1679
+ );
1680
+ oldValue = newValue;
1681
+ } finally {
1682
+ activeWatcher = currentWatcher;
1683
+ }
1684
+ }
1685
+ } else {
1686
+ effect.run();
1687
+ }
1688
+ };
1689
+ if (augmentJob) {
1690
+ augmentJob(job);
1691
+ }
1692
+ effect = new ReactiveEffect(getter);
1693
+ effect.scheduler = scheduler ? () => scheduler(job, false) : job;
1694
+ boundCleanup = (fn) => onWatcherCleanup(fn, false, effect);
1695
+ cleanup = effect.onStop = () => {
1696
+ const cleanups = cleanupMap.get(effect);
1697
+ if (cleanups) {
1698
+ if (call) {
1699
+ call(cleanups, 4);
1700
+ } else {
1701
+ for (const cleanup2 of cleanups) cleanup2();
1702
+ }
1703
+ cleanupMap.delete(effect);
1704
+ }
1705
+ };
1706
+ if (cb) {
1707
+ if (immediate) {
1708
+ job(true);
1709
+ } else {
1710
+ oldValue = effect.run();
1711
+ }
1712
+ } else if (scheduler) {
1713
+ scheduler(job.bind(null, true), true);
1714
+ } else {
1715
+ effect.run();
1716
+ }
1717
+ const scope = getCurrentScope();
1718
+ const watchHandle = () => {
1719
+ effect.stop();
1720
+ if (scope) {
1721
+ shared.remove(scope.effects, effect);
1722
+ }
1723
+ };
1724
+ watchHandle.pause = effect.pause.bind(effect);
1725
+ watchHandle.resume = effect.resume.bind(effect);
1726
+ watchHandle.stop = watchHandle;
1727
+ return watchHandle;
1728
+ }
1729
+ function traverse(value, depth = Infinity, seen) {
1730
+ if (depth <= 0 || !shared.isObject(value) || value["__v_skip"]) {
1731
+ return value;
1732
+ }
1733
+ seen = seen || /* @__PURE__ */ new Set();
1734
+ if (seen.has(value)) {
1735
+ return value;
1736
+ }
1737
+ seen.add(value);
1738
+ depth--;
1739
+ if (isRef(value)) {
1740
+ traverse(value.value, depth, seen);
1741
+ } else if (shared.isArray(value)) {
1742
+ for (let i = 0; i < value.length; i++) {
1743
+ traverse(value[i], depth, seen);
1744
+ }
1745
+ } else if (shared.isSet(value) || shared.isMap(value)) {
1746
+ value.forEach((v) => {
1747
+ traverse(v, depth, seen);
1748
+ });
1749
+ } else if (shared.isPlainObject(value)) {
1750
+ for (const key in value) {
1751
+ traverse(value[key], depth, seen);
1752
+ }
1753
+ for (const key of Object.getOwnPropertySymbols(value)) {
1754
+ if (Object.prototype.propertyIsEnumerable.call(value, key)) {
1755
+ traverse(value[key], depth, seen);
1756
+ }
1757
+ }
1758
+ }
1759
+ return value;
1760
+ }
1761
+
1558
1762
  exports.ARRAY_ITERATE_KEY = ARRAY_ITERATE_KEY;
1559
1763
  exports.EffectFlags = EffectFlags;
1560
1764
  exports.EffectScope = EffectScope;
@@ -1564,12 +1768,14 @@ exports.ReactiveEffect = ReactiveEffect;
1564
1768
  exports.ReactiveFlags = ReactiveFlags;
1565
1769
  exports.TrackOpTypes = TrackOpTypes;
1566
1770
  exports.TriggerOpTypes = TriggerOpTypes;
1771
+ exports.WatchErrorCodes = WatchErrorCodes;
1567
1772
  exports.computed = computed;
1568
1773
  exports.customRef = customRef;
1569
1774
  exports.effect = effect;
1570
1775
  exports.effectScope = effectScope;
1571
1776
  exports.enableTracking = enableTracking;
1572
1777
  exports.getCurrentScope = getCurrentScope;
1778
+ exports.getCurrentWatcher = getCurrentWatcher;
1573
1779
  exports.isProxy = isProxy;
1574
1780
  exports.isReactive = isReactive;
1575
1781
  exports.isReadonly = isReadonly;
@@ -1578,6 +1784,7 @@ exports.isShallow = isShallow;
1578
1784
  exports.markRaw = markRaw;
1579
1785
  exports.onEffectCleanup = onEffectCleanup;
1580
1786
  exports.onScopeDispose = onScopeDispose;
1787
+ exports.onWatcherCleanup = onWatcherCleanup;
1581
1788
  exports.pauseTracking = pauseTracking;
1582
1789
  exports.proxyRefs = proxyRefs;
1583
1790
  exports.reactive = reactive;
@@ -1597,6 +1804,8 @@ exports.toRef = toRef;
1597
1804
  exports.toRefs = toRefs;
1598
1805
  exports.toValue = toValue;
1599
1806
  exports.track = track;
1807
+ exports.traverse = traverse;
1600
1808
  exports.trigger = trigger;
1601
1809
  exports.triggerRef = triggerRef;
1602
1810
  exports.unref = unref;
1811
+ exports.watch = watch;
@@ -278,8 +278,7 @@ export declare enum EffectFlags {
278
278
  NOTIFIED = 8,
279
279
  DIRTY = 16,
280
280
  ALLOW_RECURSE = 32,
281
- NO_BATCH = 64,
282
- PAUSED = 128
281
+ PAUSED = 64
283
282
  }
284
283
  /**
285
284
  * Subscriber is a type that tracks (or subscribes to) a list of deps.
@@ -342,16 +341,20 @@ export declare function resetTracking(): void;
342
341
  export declare function onEffectCleanup(fn: () => void, failSilently?: boolean): void;
343
342
 
344
343
  declare const ComputedRefSymbol: unique symbol;
345
- export interface ComputedRef<T = any> extends WritableComputedRef<T> {
346
- readonly value: T;
344
+ declare const WritableComputedRefSymbol: unique symbol;
345
+ interface BaseComputedRef<T, S = T> extends Ref<T, S> {
347
346
  [ComputedRefSymbol]: true;
348
- }
349
- export interface WritableComputedRef<T, S = T> extends Ref<T, S> {
350
347
  /**
351
348
  * @deprecated computed no longer uses effect
352
349
  */
353
350
  effect: ComputedRefImpl;
354
351
  }
352
+ export interface ComputedRef<T = any> extends BaseComputedRef<T> {
353
+ readonly value: T;
354
+ }
355
+ export interface WritableComputedRef<T, S = T> extends BaseComputedRef<T, S> {
356
+ [WritableComputedRefSymbol]: true;
357
+ }
355
358
  export type ComputedGetter<T> = (oldValue?: T) => T;
356
359
  export type ComputedSetter<T> = (newValue: T) => void;
357
360
  export interface WritableComputedOptions<T, S = T> {
@@ -704,3 +707,41 @@ export declare function reactiveReadArray<T>(array: T[]): T[];
704
707
  */
705
708
  export declare function shallowReadArray<T>(arr: T[]): T[];
706
709
 
710
+ export declare enum WatchErrorCodes {
711
+ WATCH_GETTER = 2,
712
+ WATCH_CALLBACK = 3,
713
+ WATCH_CLEANUP = 4
714
+ }
715
+ type WatchEffect = (onCleanup: OnCleanup) => void;
716
+ type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T);
717
+ type WatchCallback<V = any, OV = any> = (value: V, oldValue: OV, onCleanup: OnCleanup) => any;
718
+ type OnCleanup = (cleanupFn: () => void) => void;
719
+ export interface WatchOptions<Immediate = boolean> extends DebuggerOptions {
720
+ immediate?: Immediate;
721
+ deep?: boolean | number;
722
+ once?: boolean;
723
+ scheduler?: WatchScheduler;
724
+ onWarn?: (msg: string, ...args: any[]) => void;
725
+ }
726
+ export type WatchStopHandle = () => void;
727
+ export interface WatchHandle extends WatchStopHandle {
728
+ pause: () => void;
729
+ resume: () => void;
730
+ stop: () => void;
731
+ }
732
+ export type WatchScheduler = (job: () => void, isFirstRun: boolean) => void;
733
+ /**
734
+ * Returns the current active effect if there is one.
735
+ */
736
+ export declare function getCurrentWatcher(): ReactiveEffect<any> | undefined;
737
+ /**
738
+ * Registers a cleanup callback on the current active effect. This
739
+ * registered cleanup callback will be invoked right before the
740
+ * associated effect re-runs.
741
+ *
742
+ * @param cleanupFn - The callback function to attach to the effect's cleanup.
743
+ */
744
+ export declare function onWatcherCleanup(cleanupFn: () => void, failSilently?: boolean, owner?: ReactiveEffect | undefined): void;
745
+ export declare function watch(source: WatchSource | WatchSource[] | WatchEffect | object, cb?: WatchCallback | null, options?: WatchOptions): WatchHandle;
746
+ export declare function traverse(value: unknown, depth?: number, seen?: Set<unknown>): unknown;
747
+