-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathuse-when-provider-ready.ts
29 lines (25 loc) · 1.6 KB
/
use-when-provider-ready.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { ProviderStatus } from '@openfeature/web-sdk';
import { useOpenFeatureClient } from './use-open-feature-client';
import { useOpenFeatureClientStatus } from './use-open-feature-client-status';
import type { ReactFlagEvaluationOptions } from '../options';
import { DEFAULT_OPTIONS, useProviderOptions, normalizeOptions, suspendUntilInitialized } from '../internal';
import { useOpenFeatureProvider } from './use-open-feature-provider';
type Options = Pick<ReactFlagEvaluationOptions, 'suspendUntilReady'>;
/**
* Utility hook that triggers suspense until the provider is {@link ProviderStatus.READY}, without evaluating any flags.
* Especially useful for React v16/17 "Legacy Suspense", in which siblings to suspending components are
* initially mounted and then hidden (see: https://github.com/reactwg/react-18/discussions/7).
* @param {Options} options options for suspense
* @returns {boolean} boolean indicating if provider is {@link ProviderStatus.READY}, useful if suspense is disabled and you want to handle loaders on your own
*/
export function useWhenProviderReady(options?: Options): boolean {
// highest priority > evaluation hook options > provider options > default options > lowest priority
const defaultedOptions = { ...DEFAULT_OPTIONS, ...useProviderOptions(), ...normalizeOptions(options) };
const client = useOpenFeatureClient();
const status = useOpenFeatureClientStatus();
const provider = useOpenFeatureProvider();
if (defaultedOptions.suspendUntilReady && status === ProviderStatus.NOT_READY) {
suspendUntilInitialized(provider, client);
}
return status === ProviderStatus.READY;
}