Skip to content

Commit

Permalink
export use open feature client status with suspense support
Browse files Browse the repository at this point in the history
Signed-off-by: William Chou <[email protected]>
  • Loading branch information
wichopy committed Nov 10, 2024
1 parent 1ba149d commit f1ff607
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/react/src/provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './provider';
export * from './use-open-feature-client';
export * from './use-when-provider-ready';
export * from './use-open-feature-client-status';
export * from './test-provider';
16 changes: 13 additions & 3 deletions packages/react/src/provider/use-open-feature-client-status.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { useEffect, useState } from 'react';
import { useOpenFeatureClient } from './use-open-feature-client';
import type { ProviderStatus } from '@openfeature/web-sdk';
import { ProviderStatus } from '@openfeature/web-sdk';
import { ProviderEvents } from '@openfeature/web-sdk';
import { ReactFlagEvaluationOptions } from '../common';
import { DEFAULT_OPTIONS, useProviderOptions, normalizeOptions, suspendUntilReady } from '../common';

type Options = Pick<ReactFlagEvaluationOptions, 'suspendUntilReady'>

/**
* Get the {@link ProviderStatus} for the OpenFeatureClient.
* @returns {ProviderStatus} status of the client for this scope
*/
export function useOpenFeatureClientStatus(): ProviderStatus {
export function useOpenFeatureClientStatus(options?: Options) {
const client = useOpenFeatureClient();
const [status, setStatus] = useState(client.providerStatus);
const [status, setStatus] = useState<typeof ProviderStatus>(client.providerStatus);

useEffect(() => {
const updateStatus = () => setStatus(client.providerStatus);
Expand All @@ -28,6 +32,12 @@ export function useOpenFeatureClientStatus(): ProviderStatus {
client.removeHandler(ProviderEvents.Reconciling, updateStatus);
};
}, [client]);
// highest priority > evaluation hook options > provider options > default options > lowest priority
const defaultedOptions = { ...DEFAULT_OPTIONS, ...useProviderOptions(), ...normalizeOptions(options) };

if (defaultedOptions.suspendUntilReady && status === ProviderStatus.NOT_READY) {
suspendUntilReady(client);
}

return status;
}

0 comments on commit f1ff607

Please sign in to comment.