@@ -74,18 +74,19 @@ export class DynamicFragment extends VaporFragment {
74
74
75
75
if ( this . fallback ) {
76
76
// set fallback for nested fragments
77
- const isFrag = isFragment ( this . nodes )
78
- if ( isFrag ) {
77
+ const hasNestedFragment = isFragment ( this . nodes )
78
+ if ( hasNestedFragment ) {
79
79
setFragmentFallback ( this . nodes as VaporFragment , this . fallback )
80
80
}
81
81
82
- if ( ! isValidBlock ( this . nodes ) ) {
82
+ const invalidFragment = findInvalidFragment ( this )
83
+ if ( invalidFragment ) {
83
84
parent && remove ( this . nodes , parent )
84
85
const scope = this . scope || ( this . scope = new EffectScope ( ) )
85
86
scope . run ( ( ) => {
86
- if ( isFrag ) {
87
- // render fragment's fallback
88
- renderFragmentFallback ( this . nodes as VaporFragment )
87
+ // for nested fragments, render invalid fragment's fallback
88
+ if ( hasNestedFragment ) {
89
+ renderFragmentFallback ( invalidFragment )
89
90
} else {
90
91
this . nodes = this . fallback ! ( ) || [ ]
91
92
}
@@ -98,13 +99,11 @@ export class DynamicFragment extends VaporFragment {
98
99
}
99
100
}
100
101
101
- function setFragmentFallback (
102
- fragment : VaporFragment ,
103
- fallback : BlockFn | undefined ,
104
- ) : void {
105
- if ( ! fragment . fallback ) {
106
- fragment . fallback = fallback
107
- }
102
+ function setFragmentFallback ( fragment : VaporFragment , fallback : BlockFn ) : void {
103
+ // stop recursion if fragment has its own fallback
104
+ if ( fragment . fallback ) return
105
+
106
+ fragment . fallback = fallback
108
107
if ( isFragment ( fragment . nodes ) ) {
109
108
setFragmentFallback ( fragment . nodes , fallback )
110
109
}
@@ -114,17 +113,20 @@ function renderFragmentFallback(fragment: VaporFragment): void {
114
113
if ( fragment instanceof ForFragment ) {
115
114
fragment . nodes [ 0 ] = [ fragment . fallback ! ( ) || [ ] ] as Block [ ]
116
115
} else if ( fragment instanceof DynamicFragment ) {
117
- const nodes = fragment . nodes
118
- if ( isFragment ( nodes ) ) {
119
- renderFragmentFallback ( nodes )
120
- } else {
121
- fragment . update ( fragment . fallback )
122
- }
116
+ fragment . update ( fragment . fallback )
123
117
} else {
124
118
// vdom slots
125
119
}
126
120
}
127
121
122
+ function findInvalidFragment ( fragment : VaporFragment ) : VaporFragment | null {
123
+ if ( isValidBlock ( fragment . nodes ) ) return null
124
+
125
+ return isFragment ( fragment . nodes )
126
+ ? findInvalidFragment ( fragment . nodes ) || fragment
127
+ : fragment
128
+ }
129
+
128
130
export function isFragment ( val : NonNullable < unknown > ) : val is VaporFragment {
129
131
return val instanceof VaporFragment
130
132
}
0 commit comments