mirror of https://github.com/vuejs/core.git
pref(runtime-vapor): refactor apiCreateIf
This commit is contained in:
parent
ef6986fbc3
commit
8ca43b5c93
|
@ -13,7 +13,7 @@ export function genIf(
|
||||||
const { condition, positive, negative, once } = oper
|
const { condition, positive, negative, once } = oper
|
||||||
const [frag, push] = buildCodeFragment()
|
const [frag, push] = buildCodeFragment()
|
||||||
|
|
||||||
const conditionExpr: CodeFragment[] = [
|
const codes: CodeFragment[] = [
|
||||||
'() => (',
|
'() => (',
|
||||||
...genExpression(condition, context),
|
...genExpression(condition, context),
|
||||||
')',
|
')',
|
||||||
|
@ -23,23 +23,27 @@ export function genIf(
|
||||||
let negativeArg: false | CodeFragment[] = false
|
let negativeArg: false | CodeFragment[] = false
|
||||||
|
|
||||||
if (negative) {
|
if (negative) {
|
||||||
|
positiveArg.unshift(' ? ')
|
||||||
|
negativeArg = [' : ']
|
||||||
if (negative.type === IRNodeTypes.BLOCK) {
|
if (negative.type === IRNodeTypes.BLOCK) {
|
||||||
negativeArg = genBlock(negative, context)
|
negativeArg.push(...genBlock(negative, context))
|
||||||
} else {
|
} else {
|
||||||
negativeArg = ['() => ', ...genIf(negative!, context, true)]
|
negativeArg.push(...genIf(negative!, context, true))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
positiveArg.unshift(' && (')
|
||||||
|
positiveArg.push(')')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isNested) push(NEWLINE, `const n${oper.id} = `)
|
codes.push(...positiveArg)
|
||||||
push(
|
if (negativeArg) codes.push(...negativeArg)
|
||||||
...genCall(
|
|
||||||
helper('createIf'),
|
if (isNested) {
|
||||||
conditionExpr,
|
push(...codes)
|
||||||
positiveArg,
|
} else {
|
||||||
negativeArg,
|
push(NEWLINE, `const n${oper.id} = `)
|
||||||
once && 'true',
|
push(...genCall(helper('createIf'), codes, once && 'true'))
|
||||||
),
|
}
|
||||||
)
|
|
||||||
|
|
||||||
return frag
|
return frag
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,15 @@ import { type BlockFn, DynamicFragment } from './block'
|
||||||
import { renderEffect } from './renderEffect'
|
import { renderEffect } from './renderEffect'
|
||||||
|
|
||||||
export function createIf(
|
export function createIf(
|
||||||
condition: () => any,
|
ifBlockFn: () => BlockFn,
|
||||||
b1: BlockFn,
|
|
||||||
b2?: BlockFn,
|
|
||||||
once?: boolean,
|
once?: boolean,
|
||||||
// hydrationNode?: Node,
|
// hydrationNode?: Node,
|
||||||
): DynamicFragment {
|
): DynamicFragment {
|
||||||
const frag = __DEV__ ? new DynamicFragment('if') : new DynamicFragment()
|
const frag = __DEV__ ? new DynamicFragment('if') : new DynamicFragment()
|
||||||
if (once) {
|
if (once) {
|
||||||
frag.update(condition() ? b1 : b2)
|
frag.update(ifBlockFn())
|
||||||
} else {
|
} else {
|
||||||
renderEffect(() => frag.update(condition() ? b1 : b2))
|
renderEffect(() => frag.update(ifBlockFn()))
|
||||||
}
|
}
|
||||||
return frag
|
return frag
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue