@@ -18,17 +18,24 @@ export type Block =
18
18
19
19
export type BlockFn = ( ...args : any [ ] ) => Block
20
20
21
- export class VaporFragment {
22
- nodes : Block
21
+ export class VaporFragment < T extends Block = Block > {
22
+ nodes : T
23
23
anchor ?: Node
24
24
insert ?: ( parent : ParentNode , anchor : Node | null ) => void
25
25
remove ?: ( parent ?: ParentNode ) => void
26
+ fallback ?: BlockFn
26
27
27
- constructor ( nodes : Block ) {
28
+ constructor ( nodes : T ) {
28
29
this . nodes = nodes
29
30
}
30
31
}
31
32
33
+ export class ForFragment extends VaporFragment < Block [ ] > {
34
+ constructor ( nodes : Block [ ] ) {
35
+ super ( nodes )
36
+ }
37
+ }
38
+
32
39
export class DynamicFragment extends VaporFragment {
33
40
anchor : Node
34
41
scope : EffectScope | undefined
@@ -67,34 +74,38 @@ export class DynamicFragment extends VaporFragment {
67
74
68
75
if ( this . fallback && ! isValidBlock ( this . nodes ) ) {
69
76
parent && remove ( this . nodes , parent )
70
- // handle nested dynamic fragment
71
- if ( isFragment ( this . nodes ) ) {
72
- renderFallback ( this . nodes , this . fallback , key )
73
- } else {
74
- this . nodes =
75
- ( this . scope || ( this . scope = new EffectScope ( ) ) ) . run ( this . fallback ) ||
76
- [ ]
77
- }
77
+ const scope = this . scope || ( this . scope = new EffectScope ( ) )
78
+ scope . run ( ( ) => {
79
+ // handle nested fragment
80
+ if ( isFragment ( this . nodes ) ) {
81
+ renderFallback ( this . nodes , this . fallback ! )
82
+ } else {
83
+ this . nodes = this . fallback ! ( ) || [ ]
84
+ }
85
+ } )
86
+
78
87
parent && insert ( this . nodes , parent , this . anchor )
79
88
}
80
89
81
90
setActiveSub ( prevSub )
82
91
}
83
92
}
84
93
85
- function renderFallback (
86
- fragment : VaporFragment ,
87
- fallback : BlockFn ,
88
- key : any ,
89
- ) : void {
94
+ function renderFallback ( fragment : VaporFragment , fallback : BlockFn ) : void {
95
+ if ( ! fragment . fallback ) fragment . fallback = fallback
96
+
90
97
if ( fragment instanceof DynamicFragment ) {
91
98
const nodes = fragment . nodes
92
99
if ( isFragment ( nodes ) ) {
93
- renderFallback ( nodes , fallback , key )
100
+ renderFallback ( nodes , fallback )
94
101
} else {
95
- if ( ! fragment . fallback ) fragment . fallback = fallback
96
- fragment . update ( fragment . fallback , key )
102
+ fragment . update ( fragment . fallback )
97
103
}
104
+ } else if ( fragment instanceof ForFragment ) {
105
+ // TODO handle nested ForFragment
106
+ fragment . nodes [ 0 ] = fallback ( ) || [ ]
107
+ } else {
108
+ // vdom slots
98
109
}
99
110
}
100
111
0 commit comments