Skip to content

Commit e1c9153

Browse files
committed
fix(reactivity): trigger iteration effect on Map.set
fix #709
1 parent cb03362 commit e1c9153

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ describe('reactivity/collections', () => {
7474
expect(dummy).toBe(3)
7575
map.set('key2', 2)
7676
expect(dummy).toBe(5)
77+
// iteration should track mutation of existing entries (#709)
78+
map.set('key1', 4)
79+
expect(dummy).toBe(6)
7780
map.delete('key1')
7881
expect(dummy).toBe(2)
7982
map.clear()
@@ -93,6 +96,9 @@ describe('reactivity/collections', () => {
9396
expect(dummy).toBe(3)
9497
map.set('key2', 2)
9598
expect(dummy).toBe(5)
99+
// iteration should track mutation of existing entries (#709)
100+
map.set('key1', 4)
101+
expect(dummy).toBe(6)
96102
map.delete('key1')
97103
expect(dummy).toBe(2)
98104
map.clear()
@@ -135,6 +141,9 @@ describe('reactivity/collections', () => {
135141
expect(dummy).toBe(3)
136142
map.set('key2', 2)
137143
expect(dummy).toBe(5)
144+
// iteration should track mutation of existing entries (#709)
145+
map.set('key1', 4)
146+
expect(dummy).toBe(6)
138147
map.delete('key1')
139148
expect(dummy).toBe(2)
140149
map.clear()
@@ -163,6 +172,10 @@ describe('reactivity/collections', () => {
163172
map.set('key2', 2)
164173
expect(dummy).toBe('key1key2')
165174
expect(dummy2).toBe(5)
175+
// iteration should track mutation of existing entries (#709)
176+
map.set('key1', 4)
177+
expect(dummy).toBe('key1key2')
178+
expect(dummy2).toBe(6)
166179
map.delete('key1')
167180
expect(dummy).toBe('key2')
168181
expect(dummy2).toBe(2)

packages/reactivity/src/effect.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,12 @@ export function trigger(
184184
if (key !== void 0) {
185185
addRunners(effects, computedRunners, depsMap.get(key))
186186
}
187-
// also run for iteration key on ADD | DELETE
188-
if (type === TriggerOpTypes.ADD || type === TriggerOpTypes.DELETE) {
187+
// also run for iteration key on ADD | DELETE | Map.SET
188+
if (
189+
type === TriggerOpTypes.ADD ||
190+
type === TriggerOpTypes.DELETE ||
191+
(type === TriggerOpTypes.SET && target instanceof Map)
192+
) {
189193
const iterationKey = isArray(target) ? 'length' : ITERATE_KEY
190194
addRunners(effects, computedRunners, depsMap.get(iterationKey))
191195
}

0 commit comments

Comments
 (0)