@@ -6,19 +6,21 @@ import { createComment, createTextNode, insert, remove } from './dom/element'
66type 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+ }
0 commit comments