Skip to content

Commit

Permalink
refactor(reactivity): add instance check for effect (#9055)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenfan0 authored Sep 5, 2023
1 parent 5479d1e commit e33d554
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/reactivity/__tests__/effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>>({})
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit e33d554

Please sign in to comment.