Skip to content

Commit

Permalink
refactor: prevValue parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Dec 4, 2024
1 parent 6c4f689 commit 43743b8
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions packages/compiler-vapor/src/generators/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function genSetProp(
oper: SetPropIRNode,
context: CodegenContext,
): CodeFragment[] {
const { vaporHelper, shouldGenEffectDeps } = context
const { vaporHelper, shouldGenEffectDeps, currentRenderEffect } = context
const {
prop: { key, values, modifier },
tag,
Expand All @@ -51,11 +51,25 @@ export function genSetProp(
let propValue = genPropValue(values, context)

if (shouldGenEffectDeps()) {
processValues(
context,
[propValue],
helperNeedPrevValue.includes(helperName),
)
processValues(context, [propValue])

const { varNamesToDeclare } = currentRenderEffect!
// need prevValue parameter
if (
varNamesToDeclare.size > 0 &&
helperNeedPrevValue.includes(helperName)
) {
const prevValueName = [...varNamesToDeclare].join('')
varNamesToDeclare.add(prevValueName)
const needReCacheValue = varNamesToDeclare.size > 1
propValue.unshift(
...[
`${prevValueName}, `, // prevValue parameter
needReCacheValue ? `(${prevValueName} = ` : undefined, // cache value to prevValue
],
)
needReCacheValue && propValue.push(')')
}
}

return [
Expand Down Expand Up @@ -253,11 +267,10 @@ const getSpecialHelper = (
export function processValues(
context: CodegenContext,
values: CodeFragment[][],
needPrevValue = false,
): string[] {
const conditions: string[] = []
values.forEach(value => {
const condition = processValue(context, value, needPrevValue)
const condition = processValue(context, value)
if (condition) conditions.push(...condition, ' && ')
})

Expand All @@ -269,14 +282,12 @@ export function processValues(
function processValue(
context: CodegenContext,
values: CodeFragment[],
needPrevValue = false,
): string[] | undefined {
const { currentRenderEffect, renderEffectSeemNames } = context
const { varNamesToDeclare, varNamesOverwritten, conditions, operations } =
currentRenderEffect!

const isMultiLine = operations.length > 1
let prevValueName = ''
for (const frag of values) {
if (!isArray(frag)) continue
// [code, newlineIndex, loc, name] -> [(_name = code), newlineIndex, loc, name]
Expand All @@ -301,25 +312,9 @@ function processValue(
// replace the original code fragment with the assignment expression
frag[0] = `(${name} = ${newName})`
}
prevValueName += `${name}`
}
}

if (needPrevValue && prevValueName) {
const needNewPrevName = varNamesToDeclare.size > 1
prevValueName = needNewPrevName
? `_prev${prevValueName}`
: [...varNamesToDeclare][0]
varNamesToDeclare.add(prevValueName)
values.unshift(
...[
`${prevValueName}, `,
needNewPrevName ? `(${prevValueName} = ` : undefined,
],
)
needNewPrevName && values.push(')')
}

if (conditions.length > 0) {
return [[...new Set(conditions)].join(' && ')]
}
Expand Down

0 comments on commit 43743b8

Please sign in to comment.