fix(reactivity): trigger iteration effect on Map.set

fix #709
This commit is contained in:
Evan You 2020-02-18 00:09:24 -05:00
parent cb03362b08
commit e1c9153b9e
2 changed files with 19 additions and 2 deletions

View File

@ -74,6 +74,9 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(3)
map.set('key2', 2)
expect(dummy).toBe(5)
// iteration should track mutation of existing entries (#709)
map.set('key1', 4)
expect(dummy).toBe(6)
map.delete('key1')
expect(dummy).toBe(2)
map.clear()
@ -93,6 +96,9 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(3)
map.set('key2', 2)
expect(dummy).toBe(5)
// iteration should track mutation of existing entries (#709)
map.set('key1', 4)
expect(dummy).toBe(6)
map.delete('key1')
expect(dummy).toBe(2)
map.clear()
@ -135,6 +141,9 @@ describe('reactivity/collections', () => {
expect(dummy).toBe(3)
map.set('key2', 2)
expect(dummy).toBe(5)
// iteration should track mutation of existing entries (#709)
map.set('key1', 4)
expect(dummy).toBe(6)
map.delete('key1')
expect(dummy).toBe(2)
map.clear()
@ -163,6 +172,10 @@ describe('reactivity/collections', () => {
map.set('key2', 2)
expect(dummy).toBe('key1key2')
expect(dummy2).toBe(5)
// iteration should track mutation of existing entries (#709)
map.set('key1', 4)
expect(dummy).toBe('key1key2')
expect(dummy2).toBe(6)
map.delete('key1')
expect(dummy).toBe('key2')
expect(dummy2).toBe(2)

View File

@ -184,8 +184,12 @@ export function trigger(
if (key !== void 0) {
addRunners(effects, computedRunners, depsMap.get(key))
}
// also run for iteration key on ADD | DELETE
if (type === TriggerOpTypes.ADD || type === TriggerOpTypes.DELETE) {
// also run for iteration key on ADD | DELETE | Map.SET
if (
type === TriggerOpTypes.ADD ||
type === TriggerOpTypes.DELETE ||
(type === TriggerOpTypes.SET && target instanceof Map)
) {
const iterationKey = isArray(target) ? 'length' : ITERATE_KEY
addRunners(effects, computedRunners, depsMap.get(iterationKey))
}