Skip to content

Commit

Permalink
fix(compiler-core): generate updateEffect for nested v-for (#171)
Browse files Browse the repository at this point in the history
Co-authored-by: 三咲智子 Kevin Deng <[email protected]>
  • Loading branch information
Jevon617 and sxzz authored Mar 29, 2024
1 parent ba17fb9 commit 9f8bf4f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
6 changes: 5 additions & 1 deletion packages/compiler-vapor/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class CodegenContext {
delegates = new Set<string>()

identifiers: Record<string, string[]> = Object.create(null)

genEffects: Array<
(effects: IREffect[], context: CodegenContext) => CodeFragment[]
> = []

withId = <T>(fn: () => T, map: Record<string, string | null>): T => {
const { identifiers } = this
const ids = Object.keys(map)
Expand All @@ -55,7 +60,6 @@ export class CodegenContext {

return ret
}
genEffect?: (effects: IREffect[]) => CodeFragment[]

constructor(
public ir: RootIRNode,
Expand Down
6 changes: 5 additions & 1 deletion packages/compiler-vapor/src/generators/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export function genBlockContent(
}

push(...genOperations(operation, context))
push(...(context.genEffect || genEffects)(effect, context))
push(
...(context.genEffects.length
? context.genEffects[context.genEffects.length - 1]
: genEffects)(effect, context),
)

push(NEWLINE, `return `)

Expand Down
7 changes: 4 additions & 3 deletions packages/compiler-vapor/src/generators/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function genFor(
oper: ForIRNode,
context: CodegenContext,
): CodeFragment[] {
const { vaporHelper } = context
const { vaporHelper, genEffects } = context
const { source, value, key, index, render, keyProp } = oper

const rawValue = value && value.content
Expand All @@ -27,7 +27,8 @@ export function genFor(

const sourceExpr = ['() => (', ...genExpression(source, context), ')']
let updateFn = '_updateEffect'
context.genEffect = genEffectInFor

genEffects.push(genEffectInFor)

const idMap: Record<string, string> = {}
if (rawValue) idMap[rawValue] = `_block.s[0]`
Expand Down Expand Up @@ -65,7 +66,7 @@ export function genFor(
]
}

context.genEffect = undefined
genEffects.pop()

return [
NEWLINE,
Expand Down

0 comments on commit 9f8bf4f

Please sign in to comment.