|
@@ -3,7 +3,7 @@ import { genBlockFunction } from './block'
|
|
|
import { genExpression } from './expression'
|
|
import { genExpression } from './expression'
|
|
|
import type { CodegenContext } from '../generate'
|
|
import type { CodegenContext } from '../generate'
|
|
|
import type { ForIRNode, IREffect } from '../ir'
|
|
import type { ForIRNode, IREffect } from '../ir'
|
|
|
-import { genOperation } from './operation'
|
|
|
|
|
|
|
+import { genOperations } from './operation'
|
|
|
import {
|
|
import {
|
|
|
type CodeFragment,
|
|
type CodeFragment,
|
|
|
INDENT_END,
|
|
INDENT_END,
|
|
@@ -24,14 +24,19 @@ export function genFor(
|
|
|
const rawKey = key && key.content
|
|
const rawKey = key && key.content
|
|
|
|
|
|
|
|
const sourceExpr = ['() => (', ...genExpression(source, context), ')']
|
|
const sourceExpr = ['() => (', ...genExpression(source, context), ')']
|
|
|
|
|
+ let updateFn = '_updateEffect'
|
|
|
context.genEffect = genEffectInFor
|
|
context.genEffect = genEffectInFor
|
|
|
|
|
|
|
|
const idMap: Record<string, string> = {}
|
|
const idMap: Record<string, string> = {}
|
|
|
if (rawValue) idMap[rawValue] = `_block.s[0]`
|
|
if (rawValue) idMap[rawValue] = `_block.s[0]`
|
|
|
if (rawKey) idMap[rawKey] = `_block.s[1]`
|
|
if (rawKey) idMap[rawKey] = `_block.s[1]`
|
|
|
|
|
|
|
|
|
|
+ const blockRet = (): CodeFragment[] => [
|
|
|
|
|
+ `[n${render.dynamic.id!}, ${updateFn}]`,
|
|
|
|
|
+ ]
|
|
|
|
|
+
|
|
|
const blockFn = context.withId(
|
|
const blockFn = context.withId(
|
|
|
- () => genBlockFunction(render, context, ['_block']),
|
|
|
|
|
|
|
+ () => genBlockFunction(render, context, ['_block'], blockRet),
|
|
|
idMap,
|
|
idMap,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -63,16 +68,15 @@ export function genFor(
|
|
|
]
|
|
]
|
|
|
|
|
|
|
|
function genEffectInFor(effects: IREffect[]): CodeFragment[] {
|
|
function genEffectInFor(effects: IREffect[]): CodeFragment[] {
|
|
|
- const [frag, push] = buildCodeFragment()
|
|
|
|
|
-
|
|
|
|
|
- const idMap: Record<string, string | null> = {}
|
|
|
|
|
- if (value) idMap[value.content] = null
|
|
|
|
|
- if (key) idMap[key.content] = null
|
|
|
|
|
|
|
+ if (!effects.length) {
|
|
|
|
|
+ updateFn = '() => {}'
|
|
|
|
|
+ return []
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- let statement: CodeFragment[] = []
|
|
|
|
|
|
|
+ const [frag, push] = buildCodeFragment(INDENT_START)
|
|
|
|
|
+ // const [value, key] = _block.s
|
|
|
if (rawValue || rawKey) {
|
|
if (rawValue || rawKey) {
|
|
|
- // const [value, key] = _block.s
|
|
|
|
|
- statement = [
|
|
|
|
|
|
|
+ push(
|
|
|
NEWLINE,
|
|
NEWLINE,
|
|
|
'const ',
|
|
'const ',
|
|
|
'[',
|
|
'[',
|
|
@@ -80,22 +84,28 @@ export function genFor(
|
|
|
rawKey && ', ',
|
|
rawKey && ', ',
|
|
|
rawKey && [rawKey, NewlineType.None, key.loc],
|
|
rawKey && [rawKey, NewlineType.None, key.loc],
|
|
|
'] = _block.s',
|
|
'] = _block.s',
|
|
|
- ]
|
|
|
|
|
|
|
+ )
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ const idMap: Record<string, string | null> = {}
|
|
|
|
|
+ if (value) idMap[value.content] = null
|
|
|
|
|
+ if (key) idMap[key.content] = null
|
|
|
context.withId(() => {
|
|
context.withId(() => {
|
|
|
- for (const { operations } of effects) {
|
|
|
|
|
- push(
|
|
|
|
|
- NEWLINE,
|
|
|
|
|
- `${vaporHelper('renderEffect')}(() => {`,
|
|
|
|
|
- INDENT_START,
|
|
|
|
|
- ...statement,
|
|
|
|
|
- )
|
|
|
|
|
- operations.forEach(op => push(...genOperation(op, context)))
|
|
|
|
|
- push(INDENT_END, NEWLINE, '})')
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ effects.forEach(effect =>
|
|
|
|
|
+ push(...genOperations(effect.operations, context)),
|
|
|
|
|
+ )
|
|
|
}, idMap)
|
|
}, idMap)
|
|
|
|
|
|
|
|
- return frag
|
|
|
|
|
|
|
+ push(INDENT_END)
|
|
|
|
|
+
|
|
|
|
|
+ return [
|
|
|
|
|
+ NEWLINE,
|
|
|
|
|
+ `const ${updateFn} = () => {`,
|
|
|
|
|
+ ...frag,
|
|
|
|
|
+ NEWLINE,
|
|
|
|
|
+ '}',
|
|
|
|
|
+ NEWLINE,
|
|
|
|
|
+ `${vaporHelper('renderEffect')}(${updateFn})`,
|
|
|
|
|
+ ]
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|