fix(reactivity): fix tracking for readonly + reactive Map (#3604)

fix #3602
This commit is contained in:
HcySunYang 2021-05-08 06:49:11 +08:00 committed by GitHub
parent c8d96837b8
commit 5036c51cb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -7,7 +7,8 @@ import {
TriggerOpTypes,
DebuggerEvent,
markRaw,
shallowReactive
shallowReactive,
readonly
} from '../src/index'
import { ITERATE_KEY } from '../src/effect'
@ -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)
})
})
})

View File

@ -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)
}
}