2025-04-25 15:08:19 +08:00
|
|
|
import { IF_ANCHOR_LABEL } from '@vue/shared'
|
2025-03-26 21:40:13 +08:00
|
|
|
import { type Block, type BlockFn, insert } from './block'
|
2025-03-11 15:27:51 +08:00
|
|
|
import { isHydrating, locateHydrationNode } from './dom/hydration'
|
2025-04-25 15:08:19 +08:00
|
|
|
import {
|
|
|
|
insertionAnchor,
|
|
|
|
insertionParent,
|
|
|
|
resetInsertionState,
|
|
|
|
} from './insertionState'
|
2024-12-14 20:37:43 +08:00
|
|
|
import { renderEffect } from './renderEffect'
|
2025-03-26 21:40:13 +08:00
|
|
|
import { DynamicFragment } from './fragment'
|
2024-12-11 11:50:17 +08:00
|
|
|
|
|
|
|
export function createIf(
|
|
|
|
condition: () => any,
|
|
|
|
b1: BlockFn,
|
|
|
|
b2?: BlockFn,
|
|
|
|
once?: boolean,
|
2025-02-12 08:58:22 +08:00
|
|
|
): Block {
|
2025-03-11 15:27:51 +08:00
|
|
|
const _insertionParent = insertionParent
|
|
|
|
const _insertionAnchor = insertionAnchor
|
|
|
|
if (isHydrating) {
|
2025-06-16 10:53:21 +08:00
|
|
|
locateHydrationNode(true)
|
2025-04-18 11:36:37 +08:00
|
|
|
} else {
|
|
|
|
resetInsertionState()
|
2025-03-11 15:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let frag: Block
|
2024-12-14 20:37:43 +08:00
|
|
|
if (once) {
|
2025-03-11 15:27:51 +08:00
|
|
|
frag = condition() ? b1() : b2 ? b2() : []
|
2024-12-14 20:37:43 +08:00
|
|
|
} else {
|
2025-04-28 15:32:23 +08:00
|
|
|
frag =
|
|
|
|
isHydrating || __DEV__
|
|
|
|
? new DynamicFragment(IF_ANCHOR_LABEL)
|
|
|
|
: new DynamicFragment()
|
2025-03-11 15:27:51 +08:00
|
|
|
renderEffect(() => (frag as DynamicFragment).update(condition() ? b1 : b2))
|
2024-12-14 20:37:43 +08:00
|
|
|
}
|
2025-03-11 15:27:51 +08:00
|
|
|
|
|
|
|
if (!isHydrating && _insertionParent) {
|
|
|
|
insert(frag, _insertionParent, _insertionAnchor)
|
|
|
|
}
|
|
|
|
|
|
|
|
return frag
|
2024-12-11 11:50:17 +08:00
|
|
|
}
|