Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 59975ed

Browse files
committed
feat(runtime-vapor): export createBranch
1 parent 3da5ecf commit 59975ed

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

packages/runtime-vapor/src/apiCreateIf.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ import { createComment, createTextNode, insert, remove } from './dom/element'
66
type BlockFn = () => Block
77

88
/*! #__NO_SIDE_EFFECTS__ */
9-
export const createIf = (
10-
condition: () => any,
11-
b1: BlockFn,
12-
b2?: BlockFn,
9+
export function createBranch(
10+
expression: () => any,
11+
render: (value: any) => BlockFn | undefined,
1312
once?: boolean,
13+
commentLabel?: string,
1414
// hydrationNode?: Node,
15-
): Fragment => {
15+
): Fragment {
1616
let newValue: any
1717
let oldValue: any
1818
let branch: BlockFn | undefined
1919
let block: Block | undefined
2020
let scope: EffectScope | undefined
21-
const anchor = __DEV__ ? createComment('if') : createTextNode()
21+
const anchor = __DEV__
22+
? createComment(commentLabel || 'dynamic')
23+
: createTextNode()
2224
const fragment: Fragment = shallowReactive({
2325
nodes: [],
2426
anchor,
@@ -32,9 +34,9 @@ export const createIf = (
3234
// }
3335

3436
if (once) {
35-
doIf()
37+
doChange()
3638
} else {
37-
renderEffect(() => doIf())
39+
renderEffect(() => doChange())
3840
}
3941

4042
// TODO: SSR
@@ -44,14 +46,15 @@ export const createIf = (
4446

4547
return fragment
4648

47-
function doIf() {
48-
if ((newValue = !!condition()) !== oldValue) {
49+
function doChange() {
50+
if ((newValue = expression()) !== oldValue) {
4951
const parent = anchor.parentNode
5052
if (block) {
5153
scope!.stop()
5254
remove(block, parent!)
5355
}
54-
if ((branch = (oldValue = newValue) ? b1 : b2)) {
56+
oldValue = newValue
57+
if ((branch = render(newValue))) {
5558
scope = effectScope()
5659
fragment.nodes = block = scope.run(branch)!
5760
parent && insert(block, parent, anchor)
@@ -62,3 +65,19 @@ export const createIf = (
6265
}
6366
}
6467
}
68+
69+
/*! #__NO_SIDE_EFFECTS__ */
70+
export function createIf(
71+
condition: () => any,
72+
b1: BlockFn,
73+
b2?: BlockFn,
74+
once?: boolean,
75+
// hydrationNode?: Node,
76+
): Fragment {
77+
return createBranch(
78+
() => !!condition(),
79+
value => (value ? b1 : b2),
80+
once,
81+
__DEV__ ? 'if' : undefined,
82+
)
83+
}

packages/runtime-vapor/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export {
126126
type ObjectPlugin,
127127
type FunctionPlugin,
128128
} from './apiCreateVaporApp'
129-
export { createIf } from './apiCreateIf'
129+
export { createBranch, createIf } from './apiCreateIf'
130130
export { createFor, createForSlots } from './apiCreateFor'
131131
export { createComponent } from './apiCreateComponent'
132132
export { createSelector } from './apiCreateSelector'

0 commit comments

Comments
 (0)