From 042809d3f7cd0d03e54e2b0b3ba110d1608d6ccf Mon Sep 17 00:00:00 2001 From: Will C Date: Sun, 10 Nov 2024 08:27:21 -0500 Subject: [PATCH 1/8] export use open feature client status with suspense support Signed-off-by: William Chou Signed-off-by: Will Chou --- packages/react/src/provider/index.ts | 1 + .../provider/use-open-feature-client-status.ts | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/react/src/provider/index.ts b/packages/react/src/provider/index.ts index 5d29f9cd3..34a77b53d 100644 --- a/packages/react/src/provider/index.ts +++ b/packages/react/src/provider/index.ts @@ -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'; diff --git a/packages/react/src/provider/use-open-feature-client-status.ts b/packages/react/src/provider/use-open-feature-client-status.ts index 2539f792f..d8fbba535 100644 --- a/packages/react/src/provider/use-open-feature-client-status.ts +++ b/packages/react/src/provider/use-open-feature-client-status.ts @@ -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 /** * 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(client.providerStatus); useEffect(() => { const updateStatus = () => setStatus(client.providerStatus); @@ -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; } From b99e0d9a317d6717478240f58b9db41826fe6ac3 Mon Sep 17 00:00:00 2001 From: William Chou Date: Sun, 10 Nov 2024 08:38:02 -0500 Subject: [PATCH 2/8] lint Signed-off-by: William Chou Signed-off-by: Will Chou --- packages/react/src/provider/use-open-feature-client-status.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/react/src/provider/use-open-feature-client-status.ts b/packages/react/src/provider/use-open-feature-client-status.ts index d8fbba535..a60fbcc80 100644 --- a/packages/react/src/provider/use-open-feature-client-status.ts +++ b/packages/react/src/provider/use-open-feature-client-status.ts @@ -2,13 +2,14 @@ import { useEffect, useState } from 'react'; import { useOpenFeatureClient } from './use-open-feature-client'; import { ProviderStatus } from '@openfeature/web-sdk'; import { ProviderEvents } from '@openfeature/web-sdk'; -import { ReactFlagEvaluationOptions } from '../common'; +import type { ReactFlagEvaluationOptions } from '../common'; import { DEFAULT_OPTIONS, useProviderOptions, normalizeOptions, suspendUntilReady } from '../common'; type Options = Pick /** * Get the {@link ProviderStatus} for the OpenFeatureClient. + * @param options * @returns {ProviderStatus} status of the client for this scope */ export function useOpenFeatureClientStatus(options?: Options) { From e8958d7c2da4a3833aef683da4ba4ccc8627017c Mon Sep 17 00:00:00 2001 From: Will Chou Date: Tue, 31 Dec 2024 11:24:00 -0500 Subject: [PATCH 3/8] undo suspending Signed-off-by: Will Chou --- .../react/src/provider/use-open-feature-client-status.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/react/src/provider/use-open-feature-client-status.ts b/packages/react/src/provider/use-open-feature-client-status.ts index a60fbcc80..9b876dd0b 100644 --- a/packages/react/src/provider/use-open-feature-client-status.ts +++ b/packages/react/src/provider/use-open-feature-client-status.ts @@ -33,12 +33,7 @@ export function useOpenFeatureClientStatus(options?: Options) { 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; } From 9b61b11f4879d64153e2d553fea3a929a69420b9 Mon Sep 17 00:00:00 2001 From: Will Chou Date: Tue, 31 Dec 2024 11:27:28 -0500 Subject: [PATCH 4/8] remove imports, fix types Signed-off-by: Will Chou --- .../react/src/provider/use-open-feature-client-status.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/react/src/provider/use-open-feature-client-status.ts b/packages/react/src/provider/use-open-feature-client-status.ts index 9b876dd0b..9f6293e76 100644 --- a/packages/react/src/provider/use-open-feature-client-status.ts +++ b/packages/react/src/provider/use-open-feature-client-status.ts @@ -1,18 +1,14 @@ import { useEffect, useState } from 'react'; import { useOpenFeatureClient } from './use-open-feature-client'; -import { ProviderStatus } from '@openfeature/web-sdk'; -import { ProviderEvents } from '@openfeature/web-sdk'; -import type { ReactFlagEvaluationOptions } from '../common'; -import { DEFAULT_OPTIONS, useProviderOptions, normalizeOptions, suspendUntilReady } from '../common'; +import { ProviderEvents, type ProviderStatus, } from '@openfeature/web-sdk'; -type Options = Pick /** * Get the {@link ProviderStatus} for the OpenFeatureClient. * @param options * @returns {ProviderStatus} status of the client for this scope */ -export function useOpenFeatureClientStatus(options?: Options) { +export function useOpenFeatureClientStatus() { const client = useOpenFeatureClient(); const [status, setStatus] = useState(client.providerStatus); From 472da460358765a13a9eeb42b6feb7f5fbb137ff Mon Sep 17 00:00:00 2001 From: Will Chou Date: Tue, 31 Dec 2024 11:28:47 -0500 Subject: [PATCH 5/8] fix type error Signed-off-by: Will Chou --- packages/react/src/provider/use-open-feature-client-status.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react/src/provider/use-open-feature-client-status.ts b/packages/react/src/provider/use-open-feature-client-status.ts index 9f6293e76..c55ec4fdb 100644 --- a/packages/react/src/provider/use-open-feature-client-status.ts +++ b/packages/react/src/provider/use-open-feature-client-status.ts @@ -10,7 +10,7 @@ import { ProviderEvents, type ProviderStatus, } from '@openfeature/web-sdk'; */ export function useOpenFeatureClientStatus() { const client = useOpenFeatureClient(); - const [status, setStatus] = useState(client.providerStatus); + const [status, setStatus] = useState(client.providerStatus); useEffect(() => { const updateStatus = () => setStatus(client.providerStatus); From fb435157eb1108077bc68d85c083a84b9718e550 Mon Sep 17 00:00:00 2001 From: Will Chou Date: Tue, 31 Dec 2024 13:23:32 -0500 Subject: [PATCH 6/8] remove unused param in js docs Signed-off-by: Will Chou --- packages/react/src/provider/use-open-feature-client-status.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/react/src/provider/use-open-feature-client-status.ts b/packages/react/src/provider/use-open-feature-client-status.ts index c55ec4fdb..16bc64207 100644 --- a/packages/react/src/provider/use-open-feature-client-status.ts +++ b/packages/react/src/provider/use-open-feature-client-status.ts @@ -5,7 +5,6 @@ import { ProviderEvents, type ProviderStatus, } from '@openfeature/web-sdk'; /** * Get the {@link ProviderStatus} for the OpenFeatureClient. - * @param options * @returns {ProviderStatus} status of the client for this scope */ export function useOpenFeatureClientStatus() { From f3590c54db6b78dcfabbea12a65250b318d6db15 Mon Sep 17 00:00:00 2001 From: Will Chou Date: Fri, 3 Jan 2025 21:49:15 -0500 Subject: [PATCH 7/8] comments Signed-off-by: Will Chou --- .../react/src/provider/use-open-feature-client-status.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/react/src/provider/use-open-feature-client-status.ts b/packages/react/src/provider/use-open-feature-client-status.ts index 16bc64207..95e2a1421 100644 --- a/packages/react/src/provider/use-open-feature-client-status.ts +++ b/packages/react/src/provider/use-open-feature-client-status.ts @@ -1,13 +1,14 @@ import { useEffect, useState } from 'react'; import { useOpenFeatureClient } from './use-open-feature-client'; -import { ProviderEvents, type ProviderStatus, } from '@openfeature/web-sdk'; +import type { ProviderStatus } from '@openfeature/web-sdk'; +import { ProviderEvents } from '@openfeature/web-sdk'; /** * Get the {@link ProviderStatus} for the OpenFeatureClient. * @returns {ProviderStatus} status of the client for this scope */ -export function useOpenFeatureClientStatus() { +export function useOpenFeatureClientStatus(): ProviderStatus { const client = useOpenFeatureClient(); const [status, setStatus] = useState(client.providerStatus); From 7a24d662b7f06f36ae4029f3d9037e232b5602ac Mon Sep 17 00:00:00 2001 From: Will Chou Date: Fri, 3 Jan 2025 21:50:03 -0500 Subject: [PATCH 8/8] extra new lines Signed-off-by: Will Chou --- packages/react/src/provider/use-open-feature-client-status.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/react/src/provider/use-open-feature-client-status.ts b/packages/react/src/provider/use-open-feature-client-status.ts index 95e2a1421..fac4a42b7 100644 --- a/packages/react/src/provider/use-open-feature-client-status.ts +++ b/packages/react/src/provider/use-open-feature-client-status.ts @@ -3,7 +3,6 @@ import { useOpenFeatureClient } from './use-open-feature-client'; import type { ProviderStatus } from '@openfeature/web-sdk'; import { ProviderEvents } from '@openfeature/web-sdk'; - /** * Get the {@link ProviderStatus} for the OpenFeatureClient. * @returns {ProviderStatus} status of the client for this scope @@ -30,6 +29,5 @@ export function useOpenFeatureClientStatus(): ProviderStatus { }; }, [client]); - return status; }