import { NodeTransform } from '../transform' ; import { findDir , makeBlock } from '../utils' ; import { createCallExpression , createFunctionExpression , ElementTypes , MemoExpression , NodeTypes , PlainElementNode } from '../ast' ; import { WITH_MEMO } from '../runtimeHelpers' ; const seen = new WeakSet ( ) ; export const transformMemo = ( node , context ) => { if ( node . type === NodeTypes . ELEMENT ) { const dir = findDir ( node , 'memo' ) ; if ( ! dir || seen . has ( node ) ) { return ; } seen . add ( node ) ; return ( ) => { const codegenNode = node . codegenNode || ( context . currentNode as PlainElementNode ) . codegenNode ; if ( codegenNode && codegenNode . type === NodeTypes . VNODE_CALL ) { if ( node . tagType !== ElementTypes . COMPONENT ) { makeBlock ( codegenNode , context ) ; } node . codegenNode = createCallExpression ( context . helper ( WITH_MEMO ) , [ dir . exp ! , createFunctionExpression ( undefined , codegenNode ) , `_cache` , String ( context . cached ++ ) ] ) as MemoExpression ; } } ; } } ;