Przeglądaj źródła

dx(reactivity): disable recursive computed warning by default

Now can be enabled with app.config.warnRecursiveComputed option.

close #10341
Evan You 2 lat temu
rodzic
commit
b31dd7468b

+ 8 - 1
packages/reactivity/src/computed.ts

@@ -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

+ 1 - 0
packages/reactivity/src/index.ts

@@ -43,6 +43,7 @@ export {
   type WritableComputedOptions,
   type ComputedGetter,
   type ComputedSetter,
+  type ComputedRefImpl,
 } from './computed'
 export { deferredComputed } from './deferredComputed'
 export {

+ 10 - 3
packages/runtime-core/src/apiComputed.ts

@@ -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
 }

+ 6 - 0
packages/runtime-core/src/apiCreateApp.ts

@@ -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 {