Skip to content

Commit

Permalink
fix(reactivity): fix tracking for readonly + reactive Map (#3604)
Browse files Browse the repository at this point in the history
fix #3602
  • Loading branch information
HcySunYang committed May 7, 2021
1 parent c8d9683 commit 5036c51
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/reactivity/__tests__/effect.spec.ts
Expand Up @@ -7,7 +7,8 @@ import {
TriggerOpTypes,
DebuggerEvent,
markRaw,
shallowReactive
shallowReactive,
readonly
} from '../src/index'
import { ITERATE_KEY } from '../src/effect'

Expand Down Expand Up @@ -832,4 +833,32 @@ describe('reactivity/effect', () => {
observed2.obj2 = obj2
expect(fnSpy2).toHaveBeenCalledTimes(1)
})

describe('readonly + reactive for Map', () => {
test('should work with readonly(reactive(Map))', () => {
const m = reactive(new Map())
const roM = readonly(m)
const fnSpy = jest.fn(() => roM.get(1))

effect(fnSpy)
expect(fnSpy).toHaveBeenCalledTimes(1)
m.set(1, 1)
expect(fnSpy).toHaveBeenCalledTimes(2)
})

test('should work with observed value as key', () => {
const key = reactive({})
const m = reactive(new Map())
m.set(key, 1)
const roM = readonly(m)
const fnSpy = jest.fn(() => roM.get(key))

effect(fnSpy)
expect(fnSpy).toHaveBeenCalledTimes(1)
m.set(key, 1)
expect(fnSpy).toHaveBeenCalledTimes(1)
m.set(key, 2)
expect(fnSpy).toHaveBeenCalledTimes(2)
})
})
})
4 changes: 4 additions & 0 deletions packages/reactivity/src/collectionHandlers.ts
Expand Up @@ -49,6 +49,10 @@ function get(
return wrap(target.get(key))
} else if (has.call(rawTarget, rawKey)) {
return wrap(target.get(rawKey))
} else if (target !== rawTarget) {
// #3602 readonly(reactive(Map))
// ensure that the nested reactive `Map` can do tracking for itself
target.get(key)
}
}

Expand Down

0 comments on commit 5036c51

Please sign in to comment.