fix(reactivity): should not trigger length dependency on Array delete

close #774
This commit is contained in:
Evan You 2020-03-06 11:30:56 -05:00
parent 689d45fb3d
commit a3066581f3
2 changed files with 12 additions and 1 deletions

View File

@ -88,6 +88,17 @@ describe('reactivity/reactive/Array', () => {
expect(index).toBe(1) expect(index).toBe(1)
}) })
test('delete on Array should not trigger length dependency', () => {
const arr = reactive([1, 2, 3])
const fn = jest.fn()
effect(() => {
fn(arr.length)
})
expect(fn).toHaveBeenCalledTimes(1)
delete arr[1]
expect(fn).toHaveBeenCalledTimes(1)
})
describe('Array methods w/ refs', () => { describe('Array methods w/ refs', () => {
let original: any[] let original: any[]
beforeEach(() => { beforeEach(() => {

View File

@ -196,7 +196,7 @@ export function trigger(
// also run for iteration key on ADD | DELETE | Map.SET // also run for iteration key on ADD | DELETE | Map.SET
if ( if (
type === TriggerOpTypes.ADD || type === TriggerOpTypes.ADD ||
type === TriggerOpTypes.DELETE || (type === TriggerOpTypes.DELETE && !isArray(target)) ||
(type === TriggerOpTypes.SET && target instanceof Map) (type === TriggerOpTypes.SET && target instanceof Map)
) { ) {
const iterationKey = isArray(target) ? 'length' : ITERATE_KEY const iterationKey = isArray(target) ? 'length' : ITERATE_KEY