Jelajahi Sumber

refactor: inline recordEffectScope

Evan You 2 tahun lalu
induk
melakukan
ef2eaef3aa
2 mengubah file dengan 5 tambahan dan 12 penghapusan
  1. 4 2
      packages/reactivity/src/effect.ts
  2. 1 10
      packages/reactivity/src/effectScope.ts

+ 4 - 2
packages/reactivity/src/effect.ts

@@ -2,7 +2,7 @@ import { extend, hasChanged } from '@vue/shared'
 import type { ComputedRefImpl } from './computed'
 import type { TrackOpTypes, TriggerOpTypes } from './constants'
 import { type Dep, globalVersion } from './dep'
-import { recordEffectScope } from './effectScope'
+import { activeEffectScope } from './effectScope'
 import { warn } from './warning'
 
 export type EffectScheduler = (...args: any[]) => any
@@ -137,7 +137,9 @@ export class ReactiveEffect<T = any>
   onTrigger?: (event: DebuggerEvent) => void
 
   constructor(public fn: () => T) {
-    recordEffectScope(this)
+    if (activeEffectScope && activeEffectScope.active) {
+      activeEffectScope.effects.push(this)
+    }
   }
 
   /**

+ 1 - 10
packages/reactivity/src/effectScope.ts

@@ -1,7 +1,7 @@
 import type { ReactiveEffect } from './effect'
 import { warn } from './warning'
 
-let activeEffectScope: EffectScope | undefined
+export let activeEffectScope: EffectScope | undefined
 
 export class EffectScope {
   /**
@@ -120,15 +120,6 @@ export function effectScope(detached?: boolean) {
   return new EffectScope(detached)
 }
 
-export function recordEffectScope(
-  effect: ReactiveEffect,
-  scope: EffectScope | undefined = activeEffectScope,
-) {
-  if (scope && scope.active) {
-    scope.effects.push(effect)
-  }
-}
-
 /**
  * Returns the current active effect scope if there is one.
  *