Skip to content

Commit

Permalink
fix(reactivity): trigger iteration effect on Map.set
Browse files Browse the repository at this point in the history
fix #709
  • Loading branch information
yyx990803 committed Feb 18, 2020
1 parent cb03362 commit e1c9153
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 13 additions & 0 deletions packages/reactivity/__tests__/collections/Map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit e1c9153

Please sign in to comment.