Skip to content

Commit

Permalink
fix(reactivity): should trigger all effects when array length is muta…
Browse files Browse the repository at this point in the history
…ted (#754)
  • Loading branch information
guaijie authored Feb 21, 2020
1 parent c54aa43 commit 5fac655
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/reactivity/__tests__/effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,30 @@ describe('reactivity/effect', () => {
expect(fnSpy).toHaveBeenCalledTimes(1)
})

it('should trigger all effects when array lenght is set 0', () => {
const observed: any = reactive([1])
let dummy, record
effect(() => {
dummy = observed.length
})
effect(() => {
record = observed[0]
})
expect(dummy).toBe(1)
expect(record).toBe(1)

observed[1] = 2
expect(observed[1]).toBe(2)

observed.unshift(3)
expect(dummy).toBe(3)
expect(record).toBe(3)

observed.length = 0
expect(dummy).toBe(0)
expect(record).toBeUndefined()
})

it('should handle self dependency mutations', () => {
const count = ref(0)
effect(() => {
Expand Down
5 changes: 3 additions & 2 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ export function trigger(
}
const effects = new Set<ReactiveEffect>()
const computedRunners = new Set<ReactiveEffect>()
if (type === TriggerOpTypes.CLEAR) {
// collection being cleared, trigger all effects for target
if (type === TriggerOpTypes.CLEAR || (key === 'length' && isArray(target))) {
// collection being cleared or Array length mutation
// trigger all effects for target
depsMap.forEach(dep => {
addRunners(effects, computedRunners, dep)
})
Expand Down

0 comments on commit 5fac655

Please sign in to comment.