@@ -5,7 +5,7 @@ import JsonViewer from "../components/JsonViewer.tsx";
5
5
import type { Block , BlockModule , InstanceOf } from "../engine/block.ts" ;
6
6
import { isDeferred } from "../engine/core/resolver.ts" ;
7
7
import { type Device , deviceOf } from "../utils/userAgent.ts" ;
8
- import { isAwaitable } from "../utils/mod.ts" ;
8
+
9
9
export type Flag = InstanceOf < typeof flagBlock , "#/root/flags" > ;
10
10
11
11
export interface FlagObj < TVariant = unknown > {
@@ -76,26 +76,14 @@ const flagBlock: Block<BlockModule<FlagFunc>> = {
76
76
} ,
77
77
} ;
78
78
if ( isMultivariate ( flag ) ) {
79
- let match : Variant < unknown > | null = null ;
80
-
81
- for ( const variant of flag . variants || [ ] ) {
82
- if ( typeof variant ?. rule !== "function" ) {
83
- continue ;
84
- }
85
- let ruleResult = variant ?. rule ( ctx ) ;
86
- // the rule can sometimes be a promise and we need to await it to check if it's truthy or not
87
- if ( isAwaitable ( ruleResult ) ) {
88
- ruleResult = await ruleResult ;
89
- }
90
- if ( ruleResult ) {
91
- match = variant ;
92
- break ;
93
- }
94
- }
79
+ const variants = flag . variants || [ ] ;
95
80
96
- if ( ! match ) {
97
- match = flag . variants [ flag . variants . length - 1 ] ;
98
- }
81
+ const results = await Promise . all (
82
+ variants . map ( ( variant ) =>
83
+ typeof variant ?. rule === "function" ? variant . rule ( ctx ) : false
84
+ ) ,
85
+ ) ;
86
+ const match = variants . find ( ( _ , index ) => results [ index ] ) || variants [ 0 ] ;
99
87
100
88
return isDeferred ( match ?. value ) ? match ?. value ( ) : match ?. value ;
101
89
}
0 commit comments