Skip to content

Commit 63a6563

Browse files
underfinyyx990803
authored andcommitted
fix(reactivity): should delete observe value (#598)
fix #597
1 parent 30aae0b commit 63a6563

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

packages/reactivity/__tests__/collections/Map.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ describe('reactivity/collections', () => {
2626
expect(dummy).toBe(undefined)
2727
})
2828

29+
it('should observe mutations with observed value as key', () => {
30+
let dummy
31+
const key = reactive({})
32+
const value = reactive({})
33+
const map = reactive(new Map())
34+
effect(() => {
35+
dummy = map.get(key)
36+
})
37+
38+
expect(dummy).toBe(undefined)
39+
map.set(key, value)
40+
expect(dummy).toBe(value)
41+
map.delete(key)
42+
expect(dummy).toBe(undefined)
43+
})
44+
2945
it('should observe size mutations', () => {
3046
let dummy
3147
const map = reactive(new Map())

packages/reactivity/__tests__/collections/Set.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ describe('reactivity/collections', () => {
2222
expect(dummy).toBe(false)
2323
})
2424

25+
it('should observe mutations with observed value', () => {
26+
let dummy
27+
const value = reactive({})
28+
const set = reactive(new Set())
29+
effect(() => (dummy = set.has(value)))
30+
31+
expect(dummy).toBe(false)
32+
set.add(value)
33+
expect(dummy).toBe(true)
34+
set.delete(value)
35+
expect(dummy).toBe(false)
36+
})
37+
2538
it('should observe for of iteration', () => {
2639
let dummy
2740
const set = reactive(new Set() as Set<number>)

packages/reactivity/__tests__/collections/WeakMap.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ describe('reactivity/collections', () => {
2727
expect(dummy).toBe(undefined)
2828
})
2929

30+
it('should observe mutations with observed value as key', () => {
31+
let dummy
32+
const key = reactive({})
33+
const value = reactive({})
34+
const map = reactive(new WeakMap())
35+
effect(() => {
36+
dummy = map.get(key)
37+
})
38+
39+
expect(dummy).toBe(undefined)
40+
map.set(key, value)
41+
expect(dummy).toBe(value)
42+
map.delete(key)
43+
expect(dummy).toBe(undefined)
44+
})
45+
3046
it('should not observe custom property mutations', () => {
3147
let dummy
3248
const map: any = reactive(new WeakMap())

packages/reactivity/__tests__/collections/WeakSet.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ describe('reactivity/collections', () => {
2323
expect(dummy).toBe(false)
2424
})
2525

26+
it('should observe mutations with observed value', () => {
27+
let dummy
28+
const value = reactive({})
29+
const set = reactive(new WeakSet())
30+
effect(() => (dummy = set.has(value)))
31+
32+
expect(dummy).toBe(false)
33+
set.add(value)
34+
expect(dummy).toBe(true)
35+
set.delete(value)
36+
expect(dummy).toBe(false)
37+
})
38+
2639
it('should not observe custom property mutations', () => {
2740
let dummy
2841
const set: any = reactive(new WeakSet())

packages/reactivity/src/collectionHandlers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function add(this: SetTypes, value: unknown) {
6363

6464
function set(this: MapTypes, key: unknown, value: unknown) {
6565
value = toRaw(value)
66+
key = toRaw(key)
6667
const target = toRaw(this)
6768
const proto = getProto(target)
6869
const hadKey = proto.has.call(target, key)
@@ -87,6 +88,7 @@ function set(this: MapTypes, key: unknown, value: unknown) {
8788
}
8889

8990
function deleteEntry(this: CollectionTypes, key: unknown) {
91+
key = toRaw(key)
9092
const target = toRaw(this)
9193
const proto = getProto(target)
9294
const hadKey = proto.has.call(target, key)

0 commit comments

Comments
 (0)