Skip to content

Commit e33d554

Browse files
authored
refactor(reactivity): add instance check for effect (#9055)
1 parent 5479d1e commit e33d554

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/reactivity/__tests__/effect.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,14 @@ describe('reactivity/effect', () => {
585585
expect(runner.effect.fn).toBe(otherRunner.effect.fn)
586586
})
587587

588+
it('should wrap if the passed function is a fake effect', () => {
589+
const fakeRunner = () => {}
590+
fakeRunner.effect = {}
591+
const runner = effect(fakeRunner)
592+
expect(fakeRunner).not.toBe(runner)
593+
expect(runner.effect.fn).toBe(fakeRunner)
594+
})
595+
588596
it('should not run multiple times for a single mutation', () => {
589597
let dummy
590598
const obj = reactive<Record<string, number>>({})

packages/reactivity/src/effect.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { ComputedRefImpl } from './computed'
1616
// which maintains a Set of subscribers, but we simply store them as
1717
// raw Sets to reduce memory overhead.
1818
type KeyToDepMap = Map<any, Dep>
19-
const targetMap = new WeakMap<any, KeyToDepMap>()
19+
const targetMap = new WeakMap<object, KeyToDepMap>()
2020

2121
// The number of effects currently being tracked recursively.
2222
let effectTrackDepth = 0
@@ -181,7 +181,7 @@ export function effect<T = any>(
181181
fn: () => T,
182182
options?: ReactiveEffectOptions
183183
): ReactiveEffectRunner {
184-
if ((fn as ReactiveEffectRunner).effect) {
184+
if ((fn as ReactiveEffectRunner).effect instanceof ReactiveEffect) {
185185
fn = (fn as ReactiveEffectRunner).effect.fn
186186
}
187187

0 commit comments

Comments
 (0)