feat(v-on): cache handlers
This commit is contained in:
@@ -16,7 +16,8 @@ import {
|
||||
SimpleExpressionNode,
|
||||
FunctionExpression,
|
||||
SequenceExpression,
|
||||
ConditionalExpression
|
||||
ConditionalExpression,
|
||||
CacheExpression
|
||||
} from './ast'
|
||||
import { SourceMapGenerator, RawSourceMap } from 'source-map'
|
||||
import {
|
||||
@@ -218,6 +219,7 @@ export function generate(
|
||||
}
|
||||
}
|
||||
genHoists(ast.hoists, context)
|
||||
genCached(ast.cached, context)
|
||||
newline()
|
||||
push(`return `)
|
||||
} else {
|
||||
@@ -226,6 +228,7 @@ export function generate(
|
||||
push(`import { ${ast.helpers.map(helper).join(', ')} } from "vue"\n`)
|
||||
}
|
||||
genHoists(ast.hoists, context)
|
||||
genCached(ast.cached, context)
|
||||
newline()
|
||||
push(`export default `)
|
||||
}
|
||||
@@ -315,6 +318,18 @@ function genHoists(hoists: JSChildNode[], context: CodegenContext) {
|
||||
})
|
||||
}
|
||||
|
||||
function genCached(cached: number, context: CodegenContext) {
|
||||
if (cached > 0) {
|
||||
context.newline()
|
||||
context.push(`let `)
|
||||
for (let i = 0; i < cached; i++) {
|
||||
context.push(`_cached_${i + 1}`)
|
||||
if (i !== cached - 1) context.push(`, `)
|
||||
}
|
||||
context.newline()
|
||||
}
|
||||
}
|
||||
|
||||
function isText(n: string | CodegenNode) {
|
||||
return (
|
||||
isString(n) ||
|
||||
@@ -419,6 +434,9 @@ function genNode(node: CodegenNode | symbol | string, context: CodegenContext) {
|
||||
case NodeTypes.JS_CONDITIONAL_EXPRESSION:
|
||||
genConditionalExpression(node, context)
|
||||
break
|
||||
case NodeTypes.JS_CACHE_EXPRESSION:
|
||||
genCacheExpression(node, context)
|
||||
break
|
||||
/* istanbul ignore next */
|
||||
default:
|
||||
if (__DEV__) {
|
||||
@@ -612,3 +630,9 @@ function genSequenceExpression(
|
||||
genNodeList(node.expressions, context)
|
||||
context.push(`)`)
|
||||
}
|
||||
|
||||
function genCacheExpression(node: CacheExpression, context: CodegenContext) {
|
||||
context.push(`_cached_${node.index} || (_cached_${node.index} = `)
|
||||
genNode(node.value, context)
|
||||
context.push(`)`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user