-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathuse-open-feature-client-status.ts
29 lines (26 loc) · 1.3 KB
/
use-open-feature-client-status.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 { useEffect, useState } from 'react';
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
*/
export function useOpenFeatureClientStatus(): ProviderStatus {
const client = useOpenFeatureClient();
const [status, setStatus] = useState<ProviderStatus>(client.providerStatus);
const controller = new AbortController();
useEffect(() => {
const updateStatus = () => setStatus(client.providerStatus);
client.addHandler(ProviderEvents.ConfigurationChanged, updateStatus, { signal: controller.signal });
client.addHandler(ProviderEvents.ContextChanged, updateStatus, { signal: controller.signal });
client.addHandler(ProviderEvents.Error, updateStatus, { signal: controller.signal });
client.addHandler(ProviderEvents.Ready, updateStatus, { signal: controller.signal });
client.addHandler(ProviderEvents.Stale, updateStatus, { signal: controller.signal });
client.addHandler(ProviderEvents.Reconciling, updateStatus, { signal: controller.signal });
return () => {
controller.abort();
};
}, [client]);
return status;
}