]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(reactivity): add instance check for effect (#9055)
authorchenfan <83797583+chenfan0@users.noreply.github.com>
Tue, 5 Sep 2023 08:27:54 +0000 (16:27 +0800)
committerGitHub <noreply@github.com>
Tue, 5 Sep 2023 08:27:54 +0000 (03:27 -0500)
packages/reactivity/__tests__/effect.spec.ts
packages/reactivity/src/effect.ts

index 69d24a765205132d2c1678f0537fe316d5fa677c..635e6534abe5355d21a1244a0ac50baca508074a 100644 (file)
@@ -585,6 +585,14 @@ describe('reactivity/effect', () => {
     expect(runner.effect.fn).toBe(otherRunner.effect.fn)
   })
 
+  it('should wrap if the passed function is a fake effect', () => {
+    const fakeRunner = () => {}
+    fakeRunner.effect = {}
+    const runner = effect(fakeRunner)
+    expect(fakeRunner).not.toBe(runner)
+    expect(runner.effect.fn).toBe(fakeRunner)
+  })
+
   it('should not run multiple times for a single mutation', () => {
     let dummy
     const obj = reactive<Record<string, number>>({})
index d4a34edfef41ede0268253adf4c8460bbe4c7481..bbac96a4b2a0bc092b4ff15546eebcebf8943006 100644 (file)
@@ -16,7 +16,7 @@ import { ComputedRefImpl } from './computed'
 // which maintains a Set of subscribers, but we simply store them as
 // raw Sets to reduce memory overhead.
 type KeyToDepMap = Map<any, Dep>
-const targetMap = new WeakMap<any, KeyToDepMap>()
+const targetMap = new WeakMap<object, KeyToDepMap>()
 
 // The number of effects currently being tracked recursively.
 let effectTrackDepth = 0
@@ -181,7 +181,7 @@ export function effect<T = any>(
   fn: () => T,
   options?: ReactiveEffectOptions
 ): ReactiveEffectRunner {
-  if ((fn as ReactiveEffectRunner).effect) {
+  if ((fn as ReactiveEffectRunner).effect instanceof ReactiveEffect) {
     fn = (fn as ReactiveEffectRunner).effect.fn
   }