1- import { Client , EvaluationDetails , FlagEvaluationOptions , FlagValue , JsonValue , ProviderEvents , ProviderStatus } from '@openfeature/web-sdk' ;
1+ import {
2+ Client ,
3+ EvaluationDetails ,
4+ FlagEvaluationOptions ,
5+ FlagValue ,
6+ JsonValue ,
7+ ProviderEvents ,
8+ ProviderStatus ,
9+ } from '@openfeature/web-sdk' ;
210import { Dispatch , SetStateAction , useEffect , useState } from 'react' ;
3- import { useOpenFeatureClient } from './provider' ;
11+ import { useOpenFeatureClient } from '.. /provider' ;
412
513type ReactFlagEvaluationOptions = {
614 /**
715 * Suspend flag evaluations while the provider is not ready.
816 * Set to false if you don't want to show suspense fallbacks until the provider is initialized.
917 * Defaults to true.
1018 */
11- suspendUntilReady ?: boolean ,
19+ suspendUntilReady ?: boolean ;
1220 /**
1321 * Suspend flag evaluations while the provider's context is being reconciled.
1422 * Set to true if you want to show suspense fallbacks while flags are re-evaluated after context changes.
1523 * Defaults to false.
1624 */
17- suspendWhileReconciling ?: boolean ,
25+ suspendWhileReconciling ?: boolean ;
1826 /**
1927 * Update the component if the provider emits a ConfigurationChanged event.
2028 * Set to false to prevent components from re-rendering when flag value changes
2129 * are received by the associated provider.
2230 * Defaults to true.
2331 */
24- updateOnConfigurationChanged ?: boolean ,
32+ updateOnConfigurationChanged ?: boolean ;
2533 /**
2634 * Update the component when the OpenFeature context changes.
27- * Set to false to prevent components from re-rendering when attributes which
35+ * Set to false to prevent components from re-rendering when attributes which
2836 * may be factors in flag evaluation change.
2937 * Defaults to true.
3038 */
31- updateOnContextChanged ?: boolean ,
39+ updateOnContextChanged ?: boolean ;
3240} & FlagEvaluationOptions ;
3341
3442const DEFAULT_OPTIONS : ReactFlagEvaluationOptions = {
@@ -41,7 +49,7 @@ const DEFAULT_OPTIONS: ReactFlagEvaluationOptions = {
4149enum SuspendState {
4250 Pending ,
4351 Success ,
44- Error
52+ Error ,
4553}
4654
4755/**
@@ -52,7 +60,11 @@ enum SuspendState {
5260 * @param {ReactFlagEvaluationOptions } options options for this evaluation
5361 * @returns { boolean } a EvaluationDetails object for this evaluation
5462 */
55- export function useBooleanFlagValue ( flagKey : string , defaultValue : boolean , options ?: ReactFlagEvaluationOptions ) : boolean {
63+ export function useBooleanFlagValue (
64+ flagKey : string ,
65+ defaultValue : boolean ,
66+ options ?: ReactFlagEvaluationOptions ,
67+ ) : boolean {
5668 return useBooleanFlagDetails ( flagKey , defaultValue , options ) . value ;
5769}
5870
@@ -64,10 +76,19 @@ export function useBooleanFlagValue(flagKey: string, defaultValue: boolean, opti
6476 * @param {ReactFlagEvaluationOptions } options options for this evaluation
6577 * @returns { EvaluationDetails<boolean> } a EvaluationDetails object for this evaluation
6678 */
67- export function useBooleanFlagDetails ( flagKey : string , defaultValue : boolean , options ?: ReactFlagEvaluationOptions ) : EvaluationDetails < boolean > {
68- return attachHandlersAndResolve ( flagKey , defaultValue , ( client ) => {
69- return client . getBooleanDetails ;
70- } , options ) ;
79+ export function useBooleanFlagDetails (
80+ flagKey : string ,
81+ defaultValue : boolean ,
82+ options ?: ReactFlagEvaluationOptions ,
83+ ) : EvaluationDetails < boolean > {
84+ return attachHandlersAndResolve (
85+ flagKey ,
86+ defaultValue ,
87+ ( client ) => {
88+ return client . getBooleanDetails ;
89+ } ,
90+ options ,
91+ ) ;
7192}
7293
7394/**
@@ -79,7 +100,11 @@ export function useBooleanFlagDetails(flagKey: string, defaultValue: boolean, op
79100 * @param {ReactFlagEvaluationOptions } options options for this evaluation
80101 * @returns { boolean } a EvaluationDetails object for this evaluation
81102 */
82- export function useStringFlagValue < T extends string = string > ( flagKey : string , defaultValue : T , options ?: ReactFlagEvaluationOptions ) : T {
103+ export function useStringFlagValue < T extends string = string > (
104+ flagKey : string ,
105+ defaultValue : T ,
106+ options ?: ReactFlagEvaluationOptions ,
107+ ) : T {
83108 return useStringFlagDetails ( flagKey , defaultValue , options ) . value ;
84109}
85110
@@ -92,10 +117,19 @@ export function useStringFlagValue<T extends string = string>(flagKey: string, d
92117 * @param {ReactFlagEvaluationOptions } options options for this evaluation
93118 * @returns { EvaluationDetails<string> } a EvaluationDetails object for this evaluation
94119 */
95- export function useStringFlagDetails < T extends string = string > ( flagKey : string , defaultValue : T , options ?: ReactFlagEvaluationOptions ) : EvaluationDetails < T > {
96- return attachHandlersAndResolve ( flagKey , defaultValue , ( client ) => {
97- return client . getStringDetails < T > ;
98- } , options ) ;
120+ export function useStringFlagDetails < T extends string = string > (
121+ flagKey : string ,
122+ defaultValue : T ,
123+ options ?: ReactFlagEvaluationOptions ,
124+ ) : EvaluationDetails < T > {
125+ return attachHandlersAndResolve (
126+ flagKey ,
127+ defaultValue ,
128+ ( client ) => {
129+ return client . getStringDetails < T > ;
130+ } ,
131+ options ,
132+ ) ;
99133}
100134
101135/**
@@ -107,7 +141,11 @@ export function useStringFlagDetails<T extends string = string>(flagKey: string,
107141 * @param {ReactFlagEvaluationOptions } options options for this evaluation
108142 * @returns { boolean } a EvaluationDetails object for this evaluation
109143 */
110- export function useNumberFlagValue < T extends number = number > ( flagKey : string , defaultValue : T , options ?: ReactFlagEvaluationOptions ) : T {
144+ export function useNumberFlagValue < T extends number = number > (
145+ flagKey : string ,
146+ defaultValue : T ,
147+ options ?: ReactFlagEvaluationOptions ,
148+ ) : T {
111149 return useNumberFlagDetails ( flagKey , defaultValue , options ) . value ;
112150}
113151
@@ -120,10 +158,19 @@ export function useNumberFlagValue<T extends number = number>(flagKey: string, d
120158 * @param {ReactFlagEvaluationOptions } options options for this evaluation
121159 * @returns { EvaluationDetails<number> } a EvaluationDetails object for this evaluation
122160 */
123- export function useNumberFlagDetails < T extends number = number > ( flagKey : string , defaultValue : T , options ?: ReactFlagEvaluationOptions ) : EvaluationDetails < T > {
124- return attachHandlersAndResolve ( flagKey , defaultValue , ( client ) => {
125- return client . getNumberDetails < T > ;
126- } , options ) ;
161+ export function useNumberFlagDetails < T extends number = number > (
162+ flagKey : string ,
163+ defaultValue : T ,
164+ options ?: ReactFlagEvaluationOptions ,
165+ ) : EvaluationDetails < T > {
166+ return attachHandlersAndResolve (
167+ flagKey ,
168+ defaultValue ,
169+ ( client ) => {
170+ return client . getNumberDetails < T > ;
171+ } ,
172+ options ,
173+ ) ;
127174}
128175
129176/**
@@ -135,7 +182,11 @@ export function useNumberFlagDetails<T extends number = number>(flagKey: string,
135182 * @param {ReactFlagEvaluationOptions } options options for this evaluation
136183 * @returns { boolean } a EvaluationDetails object for this evaluation
137184 */
138- export function useObjectFlagValue < T extends JsonValue = JsonValue > ( flagKey : string , defaultValue : T , options ?: ReactFlagEvaluationOptions ) : T {
185+ export function useObjectFlagValue < T extends JsonValue = JsonValue > (
186+ flagKey : string ,
187+ defaultValue : T ,
188+ options ?: ReactFlagEvaluationOptions ,
189+ ) : T {
139190 return useObjectFlagDetails < T > ( flagKey , defaultValue , options ) . value ;
140191}
141192
@@ -148,21 +199,41 @@ export function useObjectFlagValue<T extends JsonValue = JsonValue>(flagKey: str
148199 * @param {ReactFlagEvaluationOptions } options options for this evaluation
149200 * @returns { EvaluationDetails<T> } a EvaluationDetails object for this evaluation
150201 */
151- export function useObjectFlagDetails < T extends JsonValue = JsonValue > ( flagKey : string , defaultValue : T , options ?: ReactFlagEvaluationOptions ) : EvaluationDetails < T > {
152- return attachHandlersAndResolve ( flagKey , defaultValue , ( client ) => {
153- return client . getObjectDetails < T > ;
154- } , options ) ;
202+ export function useObjectFlagDetails < T extends JsonValue = JsonValue > (
203+ flagKey : string ,
204+ defaultValue : T ,
205+ options ?: ReactFlagEvaluationOptions ,
206+ ) : EvaluationDetails < T > {
207+ return attachHandlersAndResolve (
208+ flagKey ,
209+ defaultValue ,
210+ ( client ) => {
211+ return client . getObjectDetails < T > ;
212+ } ,
213+ options ,
214+ ) ;
155215}
156216
157- function attachHandlersAndResolve < T extends FlagValue > ( flagKey : string , defaultValue : T , resolver : ( client : Client ) => ( flagKey : string , defaultValue : T ) => EvaluationDetails < T > , options ?: ReactFlagEvaluationOptions ) : EvaluationDetails < T > {
217+ function attachHandlersAndResolve < T extends FlagValue > (
218+ flagKey : string ,
219+ defaultValue : T ,
220+ resolver : ( client : Client ) => ( flagKey : string , defaultValue : T ) => EvaluationDetails < T > ,
221+ options ?: ReactFlagEvaluationOptions ,
222+ ) : EvaluationDetails < T > {
158223 const defaultedOptions = { ...DEFAULT_OPTIONS , ...options } ;
159224 const [ , updateState ] = useState < object | undefined > ( ) ;
160225 const client = useOpenFeatureClient ( ) ;
161226 const forceUpdate = ( ) => {
162227 updateState ( { } ) ;
163228 } ;
164229 const suspendRef = ( ) => {
165- suspend ( client , updateState , ProviderEvents . ContextChanged , ProviderEvents . ConfigurationChanged , ProviderEvents . Ready ) ;
230+ suspend (
231+ client ,
232+ updateState ,
233+ ProviderEvents . ContextChanged ,
234+ ProviderEvents . ConfigurationChanged ,
235+ ProviderEvents . Ready ,
236+ ) ;
166237 } ;
167238
168239 useEffect ( ( ) => {
@@ -188,7 +259,7 @@ function attachHandlersAndResolve<T extends FlagValue>(flagKey: string, defaultV
188259 client . removeHandler ( ProviderEvents . Reconciling , suspendRef ) ;
189260 } ;
190261 } , [ ] ) ;
191-
262+
192263 useEffect ( ( ) => {
193264 if ( defaultedOptions . updateOnConfigurationChanged ) {
194265 // update when the provider configuration changes
@@ -209,10 +280,13 @@ function attachHandlersAndResolve<T extends FlagValue>(flagKey: string, defaultV
209280 * @param {Function } updateState the state update function
210281 * @param {ProviderEvents[] } resumeEvents list of events which will resume the suspend
211282 */
212- function suspend ( client : Client , updateState : Dispatch < SetStateAction < object | undefined > > , ...resumeEvents : ProviderEvents [ ] ) {
213-
283+ function suspend (
284+ client : Client ,
285+ updateState : Dispatch < SetStateAction < object | undefined > > ,
286+ ...resumeEvents : ProviderEvents [ ]
287+ ) {
214288 let suspendResolver : ( ) => void ;
215-
289+
216290 const suspendPromise = new Promise < void > ( ( resolve ) => {
217291 suspendResolver = ( ) => {
218292 resolve ( ) ;
@@ -235,20 +309,19 @@ function suspend(client: Client, updateState: Dispatch<SetStateAction<object | u
235309 * @template T flag type
236310 * @returns {Function } suspense-compliant lambda
237311 */
238- function suspenseWrapper < T > ( promise : Promise < T > ) {
312+ function suspenseWrapper < T > ( promise : Promise < T > ) {
239313 let status : SuspendState = SuspendState . Pending ;
240314 let result : T ;
241315
242- const suspended = promise . then (
243- ( value ) => {
316+ const suspended = promise
317+ . then ( ( value ) => {
244318 status = SuspendState . Success ;
245319 result = value ;
246- } ,
247- ( error ) => {
320+ } )
321+ . catch ( ( error ) => {
248322 status = SuspendState . Error ;
249323 result = error ;
250- }
251- ) ;
324+ } ) ;
252325
253326 return ( ) => {
254327 switch ( status ) {
@@ -262,4 +335,4 @@ function suspenseWrapper <T>(promise: Promise<T>) {
262335 throw new Error ( 'Suspending promise is in an unknown state.' ) ;
263336 }
264337 } ;
265- } ;
338+ }
0 commit comments