]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
dx(reactivity): disable recursive computed warning by default
authorEvan You <yyx990803@gmail.com>
Sun, 25 Feb 2024 12:38:33 +0000 (20:38 +0800)
committerEvan You <yyx990803@gmail.com>
Sun, 25 Feb 2024 12:38:33 +0000 (20:38 +0800)
Now can be enabled with app.config.warnRecursiveComputed option.

close #10341

packages/reactivity/src/computed.ts
packages/reactivity/src/index.ts
packages/runtime-core/src/apiComputed.ts
packages/runtime-core/src/apiCreateApp.ts

index c3f9c915b19ace4797ab88470cbbcd183e0ea8a4..da63fe84750af13db7bdb7c8d9968e65b8b9cbe0 100644 (file)
@@ -42,6 +42,11 @@ export class ComputedRefImpl<T> {
 
   public _cacheable: boolean
 
+  /**
+   * Dev only
+   */
+  _warnRecursive?: boolean
+
   constructor(
     private getter: ComputedGetter<T>,
     private readonly _setter: ComputedSetter<T>,
@@ -74,7 +79,9 @@ export class ComputedRefImpl<T> {
     }
     trackRefValue(self)
     if (self.effect._dirtyLevel >= DirtyLevels.MaybeDirty_ComputedSideEffect) {
-      __DEV__ && warn(COMPUTED_SIDE_EFFECT_WARN, `\n\ngetter: `, this.getter)
+      if (__DEV__ && (__TEST__ || this._warnRecursive)) {
+        warn(COMPUTED_SIDE_EFFECT_WARN, `\n\ngetter: `, this.getter)
+      }
       triggerRefValue(self, DirtyLevels.MaybeDirty_ComputedSideEffect)
     }
     return self._value
index 1c80fbc752b733290296c05fefb7a7c8aed29351..505ec9e2035939e4d6704c7b647d1086546d7f53 100644 (file)
@@ -43,6 +43,7 @@ export {
   type WritableComputedOptions,
   type ComputedGetter,
   type ComputedSetter,
+  type ComputedRefImpl,
 } from './computed'
 export { deferredComputed } from './deferredComputed'
 export {
index 97db0da453cd63bcb48979c49e81f6067c9ad864..a7d959dfaaa9c8f77f9eab03bc7620a14d5ba84c 100644 (file)
@@ -1,10 +1,17 @@
-import { computed as _computed } from '@vue/reactivity'
-import { isInSSRComponentSetup } from './component'
+import { type ComputedRefImpl, computed as _computed } from '@vue/reactivity'
+import { getCurrentInstance, isInSSRComponentSetup } from './component'
 
 export const computed: typeof _computed = (
   getterOrOptions: any,
   debugOptions?: any,
 ) => {
   // @ts-expect-error
-  return _computed(getterOrOptions, debugOptions, isInSSRComponentSetup)
+  const c = _computed(getterOrOptions, debugOptions, isInSSRComponentSetup)
+  if (__DEV__) {
+    const i = getCurrentInstance()
+    if (i && i.appContext.config.warnRecursiveComputed) {
+      ;(c as unknown as ComputedRefImpl<any>)._warnRecursive = true
+    }
+  }
+  return c
 }
index c476cdbe701df08c3a4060564305ea74846440e1..a1bf09fd2127623316288127bd79e776befa6666 100644 (file)
@@ -110,6 +110,12 @@ export interface AppConfig {
    * @deprecated use config.compilerOptions.isCustomElement
    */
   isCustomElement?: (tag: string) => boolean
+
+  /**
+   * TODO document for 3.5
+   * Enable warnings for computed getters that recursively trigger itself.
+   */
+  warnRecursiveComputed?: boolean
 }
 
 export interface AppContext {