From: daiwei Date: Mon, 30 Dec 2024 09:20:07 +0000 (+0800) Subject: pref(runtime-vapor): refactor apiCreateIf X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8ca43b5c931867dc34efa8dafd7d28113b9c9e10;p=thirdparty%2Fvuejs%2Fcore.git pref(runtime-vapor): refactor apiCreateIf --- diff --git a/packages/compiler-vapor/src/generators/if.ts b/packages/compiler-vapor/src/generators/if.ts index f4a3e599fa..d59d866181 100644 --- a/packages/compiler-vapor/src/generators/if.ts +++ b/packages/compiler-vapor/src/generators/if.ts @@ -13,7 +13,7 @@ export function genIf( const { condition, positive, negative, once } = oper const [frag, push] = buildCodeFragment() - const conditionExpr: CodeFragment[] = [ + const codes: CodeFragment[] = [ '() => (', ...genExpression(condition, context), ')', @@ -23,23 +23,27 @@ export function genIf( let negativeArg: false | CodeFragment[] = false if (negative) { + positiveArg.unshift(' ? ') + negativeArg = [' : '] if (negative.type === IRNodeTypes.BLOCK) { - negativeArg = genBlock(negative, context) + negativeArg.push(...genBlock(negative, context)) } 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} = `) - push( - ...genCall( - helper('createIf'), - conditionExpr, - positiveArg, - negativeArg, - once && 'true', - ), - ) + codes.push(...positiveArg) + if (negativeArg) codes.push(...negativeArg) + + if (isNested) { + push(...codes) + } else { + push(NEWLINE, `const n${oper.id} = `) + push(...genCall(helper('createIf'), codes, once && 'true')) + } return frag } diff --git a/packages/runtime-vapor/src/apiCreateIf.ts b/packages/runtime-vapor/src/apiCreateIf.ts index e403531393..bcf01e96ea 100644 --- a/packages/runtime-vapor/src/apiCreateIf.ts +++ b/packages/runtime-vapor/src/apiCreateIf.ts @@ -2,17 +2,15 @@ import { type BlockFn, DynamicFragment } from './block' import { renderEffect } from './renderEffect' export function createIf( - condition: () => any, - b1: BlockFn, - b2?: BlockFn, + ifBlockFn: () => BlockFn, once?: boolean, // hydrationNode?: Node, ): DynamicFragment { const frag = __DEV__ ? new DynamicFragment('if') : new DynamicFragment() if (once) { - frag.update(condition() ? b1 : b2) + frag.update(ifBlockFn()) } else { - renderEffect(() => frag.update(condition() ? b1 : b2)) + renderEffect(() => frag.update(ifBlockFn())) } return frag }