forked from vuejs/core
/
withMemo.ts
38 lines (33 loc) 路 864 Bytes
/
withMemo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { hasChanged } from '@vue/shared'
import { currentBlock, isBlockTreeEnabled, VNode } from '../vnode'
export function withMemo(
memo: any[],
render: () => VNode<any, any>,
cache: any[],
index: number
) {
const cached = cache[index] as VNode | undefined
if (cached && isMemoSame(cached, memo)) {
return cached
}
const ret = render()
// shallow clone
ret.memo = memo.slice()
return (cache[index] = ret)
}
export function isMemoSame(cached: VNode, memo: any[]) {
const prev: any[] = cached.memo!
if (prev.length != memo.length) {
return false
}
for (let i = 0; i < prev.length; i++) {
if (hasChanged(prev[i], memo[i])) {
return false
}
}
// make sure to let parent block track it when returning cached
if (isBlockTreeEnabled > 0 && currentBlock) {
currentBlock.push(cached)
}
return true
}