@@ -6,19 +6,21 @@ import { createComment, createTextNode, insert, remove } from './dom/element'
6
6
type BlockFn = ( ) => Block
7
7
8
8
/*! #__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 ,
13
12
once ?: boolean ,
13
+ commentLabel ?: string ,
14
14
// hydrationNode?: Node,
15
- ) : Fragment => {
15
+ ) : Fragment {
16
16
let newValue : any
17
17
let oldValue : any
18
18
let branch : BlockFn | undefined
19
19
let block : Block | undefined
20
20
let scope : EffectScope | undefined
21
- const anchor = __DEV__ ? createComment ( 'if' ) : createTextNode ( )
21
+ const anchor = __DEV__
22
+ ? createComment ( commentLabel || 'dynamic' )
23
+ : createTextNode ( )
22
24
const fragment : Fragment = shallowReactive ( {
23
25
nodes : [ ] ,
24
26
anchor,
@@ -32,9 +34,9 @@ export const createIf = (
32
34
// }
33
35
34
36
if ( once ) {
35
- doIf ( )
37
+ doChange ( )
36
38
} else {
37
- renderEffect ( ( ) => doIf ( ) )
39
+ renderEffect ( ( ) => doChange ( ) )
38
40
}
39
41
40
42
// TODO: SSR
@@ -44,14 +46,15 @@ export const createIf = (
44
46
45
47
return fragment
46
48
47
- function doIf ( ) {
48
- if ( ( newValue = ! ! condition ( ) ) !== oldValue ) {
49
+ function doChange ( ) {
50
+ if ( ( newValue = expression ( ) ) !== oldValue ) {
49
51
const parent = anchor . parentNode
50
52
if ( block ) {
51
53
scope ! . stop ( )
52
54
remove ( block , parent ! )
53
55
}
54
- if ( ( branch = ( oldValue = newValue ) ? b1 : b2 ) ) {
56
+ oldValue = newValue
57
+ if ( ( branch = render ( newValue ) ) ) {
55
58
scope = effectScope ( )
56
59
fragment . nodes = block = scope . run ( branch ) !
57
60
parent && insert ( block , parent , anchor )
@@ -62,3 +65,19 @@ export const createIf = (
62
65
}
63
66
}
64
67
}
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