From: chenfan <83797583+chenfan0@users.noreply.github.com> Date: Tue, 5 Sep 2023 08:27:54 +0000 (+0800) Subject: refactor(reactivity): add instance check for effect (#9055) X-Git-Tag: v3.3.5~40 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e33d554cae3824a30fea140ae73852ae47b13e58;p=thirdparty%2Fvuejs%2Fcore.git refactor(reactivity): add instance check for effect (#9055) --- diff --git a/packages/reactivity/__tests__/effect.spec.ts b/packages/reactivity/__tests__/effect.spec.ts index 69d24a7652..635e6534ab 100644 --- a/packages/reactivity/__tests__/effect.spec.ts +++ b/packages/reactivity/__tests__/effect.spec.ts @@ -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>({}) diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index d4a34edfef..bbac96a4b2 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -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 -const targetMap = new WeakMap() +const targetMap = new WeakMap() // The number of effects currently being tracked recursively. let effectTrackDepth = 0 @@ -181,7 +181,7 @@ export function effect( fn: () => T, options?: ReactiveEffectOptions ): ReactiveEffectRunner { - if ((fn as ReactiveEffectRunner).effect) { + if ((fn as ReactiveEffectRunner).effect instanceof ReactiveEffect) { fn = (fn as ReactiveEffectRunner).effect.fn }