All files / runtime-core/src/helpers withMemo.ts

100% Statements 32/32
100% Branches 12/12
100% Functions 2/2
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40 412x 2x   2x 27x 27x 27x 27x 27x 27x 27x 6x 6x 21x     21x 21x   21x 21x   2x 35x 35x 2x 2x   35x 45x 17x 17x 45x     35x 1x 1x 16x 16x  
import { hasChanged } from '@vue/shared'
import { type VNode, currentBlock, isBlockTreeEnabled } from '../vnode'
 
export function withMemo(
  memo: any[],
  render: () => VNode<any, any>,
  cache: any[],
  index: number,
): VNode<any, any> {
  const cached = cache[index] as VNode | undefined
  if (cached && isMemoSame(cached, memo)) {
    return cached
  }
  const ret = render()
 
  // shallow clone
  ret.memo = memo.slice()
  ret.cacheIndex = index
 
  return (cache[index] = ret)
}
 
export function isMemoSame(cached: VNode, memo: any[]): boolean {
  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
}