2025-02-12 08:58:22 +08:00
|
|
|
import { type Block, type BlockFn, DynamicFragment } from './block'
|
2024-12-14 20:37:43 +08:00
|
|
|
import { renderEffect } from './renderEffect'
|
2024-12-11 11:50:17 +08:00
|
|
|
|
|
|
|
export function createIf(
|
|
|
|
condition: () => any,
|
|
|
|
b1: BlockFn,
|
|
|
|
b2?: BlockFn,
|
|
|
|
once?: boolean,
|
|
|
|
// hydrationNode?: Node,
|
2025-02-12 08:58:22 +08:00
|
|
|
): Block {
|
2024-12-14 20:37:43 +08:00
|
|
|
if (once) {
|
2025-02-12 08:58:22 +08:00
|
|
|
return condition() ? b1() : b2 ? b2() : []
|
2024-12-14 20:37:43 +08:00
|
|
|
} else {
|
2025-02-12 08:58:22 +08:00
|
|
|
const frag = __DEV__ ? new DynamicFragment('if') : new DynamicFragment()
|
2024-12-14 20:37:43 +08:00
|
|
|
renderEffect(() => frag.update(condition() ? b1 : b2))
|
2025-02-12 08:58:22 +08:00
|
|
|
return frag
|
2024-12-14 20:37:43 +08:00
|
|
|
}
|
2024-12-11 11:50:17 +08:00
|
|
|
}
|