feat(runtime-core): support dynamic / external array in v-memo (#4255)

This commit is contained in:
lidlanca 2021-08-09 15:39:22 -04:00 committed by GitHub
parent 2641422aa7
commit 6779bff537
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -10,6 +10,47 @@ describe('v-memo', () => {
return [el, vm] return [el, vm]
} }
test('on with external array', async () => {
const [el, vm] = mount({
template: `<div v-memo="arr">{{ arr[0] }} {{ arr[1] }} {{arr[2] ?? '_' }} ({{c}})</div>{{c}}`,
data: () => ({ arr: [0, 0], c: 0 })
})
expect(el.innerHTML).toBe(`<div>0 0 _ (0)</div>0`)
let [x, y, z] = [0, 1, 2]
// change at index x - should update
vm.arr[x]++
vm.c++
await nextTick()
expect(el.innerHTML).toBe(`<div>1 0 _ (1)</div>1`)
// change at index y - should update
vm.arr[y]++
vm.c++
await nextTick()
expect(el.innerHTML).toBe(`<div>1 1 _ (2)</div>2`)
// noop change - should NOT update
vm.arr[x] = vm.arr[0]
vm.arr[y] = vm.arr[1]
vm.c++
await nextTick()
expect(el.innerHTML).toBe(`<div>1 1 _ (2)</div>3`)
// add item 3rd item - should update
vm.arr[z] = 0
vm.c++
await nextTick()
expect(el.innerHTML).toBe(`<div>1 1 0 (4)</div>4`)
// remove 3rd item - should update
vm.arr = vm.arr.slice(0, vm.arr.length - 1)
vm.c++
await nextTick()
expect(el.innerHTML).toBe(`<div>1 1 _ (5)</div>5`)
})
test('on normal element', async () => { test('on normal element', async () => {
const [el, vm] = mount({ const [el, vm] = mount({
template: `<div v-memo="[x]">{{ x }} {{ y }}</div>`, template: `<div v-memo="[x]">{{ x }} {{ y }}</div>`,

View File

@ -11,12 +11,17 @@ export function withMemo(
return cached return cached
} }
const ret = render() const ret = render()
ret.memo = memo
// shallow clone
ret.memo = memo.slice()
return (cache[index] = ret) return (cache[index] = ret)
} }
export function isMemoSame(cached: VNode, memo: any[]) { export function isMemoSame(cached: VNode, memo: any[]) {
const prev: any[] = cached.memo! const prev: any[] = cached.memo!
if (prev.length != memo.length) {
return false
}
for (let i = 0; i < prev.length; i++) { for (let i = 0; i < prev.length; i++) {
if (prev[i] !== memo[i]) { if (prev[i] !== memo[i]) {
return false return false