@@ -5,11 +5,20 @@ declare module 'react' {
5
5
export function use < T > ( x : Promise < T > ) : T | undefined ;
6
6
}
7
7
8
+ export const useLoop = ( ) => {
9
+ const [ state , update ] = useReducer ( ( s ) => ! s , false ) ;
10
+
11
+ useEffect ( ( ) => {
12
+ if ( ( window as any ) . PLEASE_STOP ) return ; // Emergency stop looping
13
+ setTimeout ( ( ) => flushSync ( ( ) => update ( ) ) , 50 ) ;
14
+ } , [ state ] ) ;
15
+ }
16
+
8
17
export const PickChild : FC < { n : number ; children : ReactNode } > = ( { n, children } ) =>
9
18
Children . toArray ( children ) [ n ] ;
10
19
11
20
export const IfElse : FC < { condition : boolean ; children : [ ReactNode , ReactNode ] } > = ( { condition, children } ) =>
12
- < PickChild n = { + ! condition } > { children } </ PickChild > ;
21
+ < PickChild n = { Number ( ! condition ) } > { children } </ PickChild > ;
13
22
14
23
export const CallFunction : FC < { fn : ( ) => void } > = ( { fn } ) => {
15
24
const fnRef = useRef ( fn ) ;
@@ -50,12 +59,11 @@ export const CallElement: FC<{
50
59
export const EvaluateAll : FC < { fns : ReactNode [ ] } > = ( { fns } ) => {
51
60
const onReturn = useContext ( Return ) ;
52
61
const resultList = useRef < any [ ] > ( [ ] ) ;
53
- const resultCount = useRef ( 0 ) ;
54
62
55
63
const resolve = useCallback ( ( result : any , i : number ) => {
56
64
resultList . current [ i ] = result ;
57
- resultCount . current ++ ;
58
- if ( resultCount . current === fns . length ) {
65
+ const resultCount = resultList . current . filter ( _ => true ) . length ;
66
+ if ( resultCount === fns . length ) {
59
67
onReturn ( resultList . current ) ;
60
68
}
61
69
} , [ fns . length , onReturn ] )
@@ -71,11 +79,3 @@ export const EvaluateAll: FC<{ fns: ReactNode[] }> = ({ fns }) => {
71
79
)
72
80
}
73
81
74
- export const useLoop = ( ) => {
75
- const [ state , update ] = useReducer ( ( s ) => ! s , false ) ;
76
-
77
- useEffect ( ( ) => {
78
- if ( ( window as any ) . PLEASE_STOP ) return ;
79
- setTimeout ( ( ) => flushSync ( ( ) => update ( ) ) , 50 ) ;
80
- } , [ state ] ) ;
81
- }
0 commit comments