Skip to content

Commit 8b6c951

Browse files
fix: extract handlers and add cleanup function (#96)
This commit fixes an issue where refs where not set on ready event, and adds cleanup of the event listeners for the useEffect.
1 parent f9adfed commit 8b6c951

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

src/FlagProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as React from 'react';
44
import { IConfig, UnleashClient } from 'unleash-proxy-client';
55
import FlagContext, { IFlagContextValue } from './FlagContext';
66

7-
interface IFlagProvider {
7+
export interface IFlagProvider {
88
config?: IConfig;
99
unleashClient?: UnleashClient;
1010
startClient?: boolean;

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import useVariant from './useVariant';
2323
import useUnleashContext from './useUnleashContext';
2424
import useUnleashClient from './useUnleashClient';
2525

26+
import { IFlagProvider } from './FlagProvider';
27+
2628
export {
2729
FlagContext,
2830
FlagProvider,
@@ -34,4 +36,6 @@ export {
3436
useUnleashClient,
3537
};
3638

39+
export type { IFlagProvider };
40+
3741
export default FlagProvider;

src/useFlag.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,28 @@ const useFlag = (name: string) => {
99

1010
useEffect(() => {
1111
if (!client) return;
12-
client.on('update', () => {
12+
13+
const updateHandler = () => {
1314
const enabled = isEnabled(name);
1415
if (enabled !== flagRef.current) {
1516
flagRef.current = enabled;
1617
setFlag(!!enabled);
1718
}
18-
});
19+
};
1920

20-
client.on('ready', () => {
21+
const readyHandler = () => {
2122
const enabled = isEnabled(name);
23+
flagRef.current = enabled;
2224
setFlag(enabled);
23-
});
25+
};
26+
27+
client.on('update', updateHandler);
28+
client.on('ready', readyHandler);
29+
30+
() => {
31+
client.off('update', updateHandler);
32+
client.off('ready', readyHandler);
33+
};
2434
}, [client]);
2535

2636
return flag;

src/useVariant.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const useVariant = (name: string): Partial<IVariant> => {
1111

1212
useEffect(() => {
1313
if (!client) return;
14-
client.on('update', () => {
14+
15+
const updateHandler = () => {
1516
const newVariant = getVariant(name);
1617
if (
1718
variantRef.current.name !== newVariant.name ||
@@ -20,12 +21,22 @@ const useVariant = (name: string): Partial<IVariant> => {
2021
setVariant(newVariant);
2122
variantRef.current = newVariant;
2223
}
23-
});
24+
};
2425

25-
client.on('ready', () => {
26+
const readyHandler = () => {
2627
const variant = getVariant(name);
28+
variantRef.current.name = variant.name;
29+
variantRef.current.enabled = variant.enabled;
2730
setVariant(variant);
28-
});
31+
};
32+
33+
client.on('update', updateHandler);
34+
client.on('ready', readyHandler);
35+
36+
() => {
37+
client.off('update', updateHandler);
38+
client.off('ready', readyHandler);
39+
};
2940
}, [client]);
3041

3142
return variant || {};

0 commit comments

Comments
 (0)