Skip to content

Commit 8172213

Browse files
committed
export use open feature client status with suspense support
Signed-off-by: William Chou <[email protected]>
1 parent 1ba149d commit 8172213

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

packages/react/src/provider/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './provider';
22
export * from './use-open-feature-client';
33
export * from './use-when-provider-ready';
4+
export * from './use-open-feature-client-status';
45
export * from './test-provider';

packages/react/src/provider/use-open-feature-client-status.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { useEffect, useState } from 'react';
22
import { useOpenFeatureClient } from './use-open-feature-client';
3-
import type { ProviderStatus } from '@openfeature/web-sdk';
3+
import { ProviderStatus } from '@openfeature/web-sdk';
44
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'>
59

610
/**
711
* Get the {@link ProviderStatus} for the OpenFeatureClient.
812
* @returns {ProviderStatus} status of the client for this scope
913
*/
10-
export function useOpenFeatureClientStatus(): ProviderStatus {
14+
export function useOpenFeatureClientStatus(options?: Options) {
1115
const client = useOpenFeatureClient();
12-
const [status, setStatus] = useState(client.providerStatus);
16+
const [status, setStatus] = useState<typeof ProviderStatus>(client.providerStatus);
1317

1418
useEffect(() => {
1519
const updateStatus = () => setStatus(client.providerStatus);
@@ -28,6 +32,12 @@ export function useOpenFeatureClientStatus(): ProviderStatus {
2832
client.removeHandler(ProviderEvents.Reconciling, updateStatus);
2933
};
3034
}, [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+
}
3141

3242
return status;
3343
}

0 commit comments

Comments
 (0)