Skip to content

Commit

Permalink
chore: avoid merge setStyle and setDynamicProps into existing render …
Browse files Browse the repository at this point in the history
…effect
  • Loading branch information
edison1105 committed Dec 12, 2024
1 parent dcb9e94 commit 229acec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/compiler-vapor/src/generators/prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ export function genPropValue(
)
}

function getRuntimeHelper(
export function getRuntimeHelper(
tag: string,
keyName: string,
modifier: '.' | '^' | undefined,
) {
): { helperName: VaporHelper; omitKey: boolean } {
const tagName = tag.toUpperCase()
let helperName: VaporHelper
let omitKey = false
Expand Down
35 changes: 34 additions & 1 deletion packages/compiler-vapor/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ import {
type IRSlots,
type OperationNode,
type RootIRNode,
type SetPropIRNode,
type VaporDirectiveNode,
} from './ir'
import { isConstantExpression } from './utils'
import { newBlock, newDynamic } from './transforms/utils'
import { getRuntimeHelper } from './generators/prop'

export type NodeTransform = (
node: RootNode | TemplateChildNode,
Expand Down Expand Up @@ -156,7 +158,8 @@ export class TransformContext<T extends AllNode = AllNode> {
const existing = this.block.effect.find(e =>
looseEqual(e.identifiers, Array.from(ids)),
)
if (existing) {
const canMerge = isMergeableOperation(operations)
if (existing && canMerge) {
existing.operations.push(...operations)
} else {
this.block.effect.push({
Expand Down Expand Up @@ -314,3 +317,33 @@ function extractIdentifiers(ids: Set<string>, node: ExpressionNode) {
ids.add((node as SimpleExpressionNode).content)
}
}

function getSetPropHelperName(op: SetPropIRNode): string {
const {
prop: { key, modifier },
tag,
} = op

const { helperName } = getRuntimeHelper(tag, key.content, modifier)
return helperName
}

function isMergeableOperation(operations: OperationNode[]): boolean {
// setStyle and setDynamicProps can't be merged into existing render effect,
// because they don't need to cache deps value. the existing render effect may cache deps value.
if (operations.some(op => op.type === IRNodeTypes.SET_DYNAMIC_PROPS)) {
return false
}

if (
operations.some(
op =>
op.type === IRNodeTypes.SET_PROP &&
getSetPropHelperName(op) === 'setStyle',
)
) {
return false
}

return true
}

0 comments on commit 229acec

Please sign in to comment.