1
1
import { useEffect , useState } from 'react' ;
2
2
import { useOpenFeatureClient } from './use-open-feature-client' ;
3
- import type { ProviderStatus } from '@openfeature/web-sdk' ;
3
+ import { ProviderStatus } from '@openfeature/web-sdk' ;
4
4
import { ProviderEvents } from '@openfeature/web-sdk' ;
5
+ import { ReactFlagEvaluationOptions } from '../common' ;
6
+ import { DEFAULT_OPTIONS , useProviderOptions , normalizeOptions , suspendUntilReady } from '../common' ;
7
+
8
+ type Options = Pick < ReactFlagEvaluationOptions , 'suspendUntilReady' >
5
9
6
10
/**
7
11
* Get the {@link ProviderStatus} for the OpenFeatureClient.
8
12
* @returns {ProviderStatus } status of the client for this scope
9
13
*/
10
- export function useOpenFeatureClientStatus ( ) : ProviderStatus {
14
+ export function useOpenFeatureClientStatus ( options ?: Options ) {
11
15
const client = useOpenFeatureClient ( ) ;
12
- const [ status , setStatus ] = useState ( client . providerStatus ) ;
16
+ const [ status , setStatus ] = useState < typeof ProviderStatus > ( client . providerStatus ) ;
13
17
14
18
useEffect ( ( ) => {
15
19
const updateStatus = ( ) => setStatus ( client . providerStatus ) ;
@@ -28,6 +32,12 @@ export function useOpenFeatureClientStatus(): ProviderStatus {
28
32
client . removeHandler ( ProviderEvents . Reconciling , updateStatus ) ;
29
33
} ;
30
34
} , [ client ] ) ;
35
+ // highest priority > evaluation hook options > provider options > default options > lowest priority
36
+ const defaultedOptions = { ...DEFAULT_OPTIONS , ...useProviderOptions ( ) , ...normalizeOptions ( options ) } ;
37
+
38
+ if ( defaultedOptions . suspendUntilReady && status === ProviderStatus . NOT_READY ) {
39
+ suspendUntilReady ( client ) ;
40
+ }
31
41
32
42
return status ;
33
43
}
0 commit comments