Skip to content

Commit 5036c51

Browse files
authoredMay 7, 2021
fix(reactivity): fix tracking for readonly + reactive Map (#3604)
fix #3602
1 parent c8d9683 commit 5036c51

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
 

‎packages/reactivity/__tests__/effect.spec.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
TriggerOpTypes,
88
DebuggerEvent,
99
markRaw,
10-
shallowReactive
10+
shallowReactive,
11+
readonly
1112
} from '../src/index'
1213
import { ITERATE_KEY } from '../src/effect'
1314

@@ -832,4 +833,32 @@ describe('reactivity/effect', () => {
832833
observed2.obj2 = obj2
833834
expect(fnSpy2).toHaveBeenCalledTimes(1)
834835
})
836+
837+
describe('readonly + reactive for Map', () => {
838+
test('should work with readonly(reactive(Map))', () => {
839+
const m = reactive(new Map())
840+
const roM = readonly(m)
841+
const fnSpy = jest.fn(() => roM.get(1))
842+
843+
effect(fnSpy)
844+
expect(fnSpy).toHaveBeenCalledTimes(1)
845+
m.set(1, 1)
846+
expect(fnSpy).toHaveBeenCalledTimes(2)
847+
})
848+
849+
test('should work with observed value as key', () => {
850+
const key = reactive({})
851+
const m = reactive(new Map())
852+
m.set(key, 1)
853+
const roM = readonly(m)
854+
const fnSpy = jest.fn(() => roM.get(key))
855+
856+
effect(fnSpy)
857+
expect(fnSpy).toHaveBeenCalledTimes(1)
858+
m.set(key, 1)
859+
expect(fnSpy).toHaveBeenCalledTimes(1)
860+
m.set(key, 2)
861+
expect(fnSpy).toHaveBeenCalledTimes(2)
862+
})
863+
})
835864
})

‎packages/reactivity/src/collectionHandlers.ts

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ function get(
4949
return wrap(target.get(key))
5050
} else if (has.call(rawTarget, rawKey)) {
5151
return wrap(target.get(rawKey))
52+
} else if (target !== rawTarget) {
53+
// #3602 readonly(reactive(Map))
54+
// ensure that the nested reactive `Map` can do tracking for itself
55+
target.get(key)
5256
}
5357
}
5458

0 commit comments

Comments
 (0)
Please sign in to comment.