@@ -22,6 +22,8 @@ import {
22
22
} from '@/utils/shared' ;
23
23
import { opcodeFor } from '@/utils/vm' ;
24
24
25
+ export type IfFunction = ( ) => boolean ;
26
+
25
27
export class IfCondition {
26
28
isDestructorRunning = false ;
27
29
prevComponent : GenericReturnType | null = null ;
@@ -33,15 +35,15 @@ export class IfCondition {
33
35
placeholder : Comment ;
34
36
throwedError : Error | null = null ;
35
37
destroyPromise : Promise < any > | null = null ;
36
- trueBranch : ( ifContext : Component < any > ) => GenericReturnType ;
37
- falseBranch : ( ifContext : Component < any > ) => GenericReturnType ;
38
+ trueBranch : ( ifContext : IfCondition ) => GenericReturnType ;
39
+ falseBranch : ( ifContext : IfCondition ) => GenericReturnType ;
38
40
constructor (
39
41
parentContext : Component < any > ,
40
- maybeCondition : Cell < boolean > ,
42
+ maybeCondition : Cell < boolean > | IfFunction | MergedCell ,
41
43
target : DocumentFragment | HTMLElement ,
42
44
placeholder : Comment ,
43
- trueBranch : ( ifContext : Component < any > ) => GenericReturnType ,
44
- falseBranch : ( ifContext : Component < any > ) => GenericReturnType ,
45
+ trueBranch : ( ifContext : IfCondition ) => GenericReturnType ,
46
+ falseBranch : ( ifContext : IfCondition ) => GenericReturnType ,
45
47
) {
46
48
this . target = target ;
47
49
this . placeholder = placeholder ;
@@ -111,7 +113,7 @@ export class IfCondition {
111
113
this . renderBranch ( nextBranch , this . runNumber ) ;
112
114
}
113
115
renderBranch (
114
- nextBranch : ( ifContext : Component < any > ) => GenericReturnType ,
116
+ nextBranch : ( ifContext : IfCondition ) => GenericReturnType ,
115
117
runNumber : number ,
116
118
) {
117
119
if ( this . destroyPromise ) {
@@ -162,11 +164,11 @@ export class IfCondition {
162
164
this . destroyPromise = destroyElement ( branch ) ;
163
165
await this . destroyPromise ;
164
166
}
165
- renderState ( nextBranch : ( ifContext : Component < any > ) => GenericReturnType ) {
167
+ renderState ( nextBranch : ( ifContext : IfCondition ) => GenericReturnType ) {
166
168
if ( IS_DEV_MODE ) {
167
169
$DEBUG_REACTIVE_CONTEXTS . push ( `if:${ String ( this . lastValue ) } ` ) ;
168
170
}
169
- this . prevComponent = nextBranch ( this as unknown as Component < any > ) ;
171
+ this . prevComponent = nextBranch ( this ) ;
170
172
if ( IS_DEV_MODE ) {
171
173
$DEBUG_REACTIVE_CONTEXTS . pop ( ) ;
172
174
}
@@ -178,6 +180,9 @@ export class IfCondition {
178
180
return ;
179
181
}
180
182
async destroy ( ) {
183
+ if ( this . isDestructorRunning ) {
184
+ throw new Error ( 'Already destroying' ) ;
185
+ }
181
186
this . isDestructorRunning = true ;
182
187
if ( this . placeholder . isConnected ) {
183
188
// should be handled on the top level
@@ -186,7 +191,7 @@ export class IfCondition {
186
191
await this . destroyBranch ( ) ;
187
192
await Promise . all ( this . destructors . map ( ( destroyFn ) => destroyFn ( ) ) ) ;
188
193
}
189
- setupCondition ( maybeCondition : Cell < boolean > ) {
194
+ setupCondition ( maybeCondition : Cell < boolean > | IfFunction | MergedCell ) {
190
195
if ( isFn ( maybeCondition ) ) {
191
196
this . condition = formula (
192
197
( ) => {
0 commit comments