Skip to content

fix: extract handlers and add cleanup function #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/FlagProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react';
import { IConfig, UnleashClient } from 'unleash-proxy-client';
import FlagContext, { IFlagContextValue } from './FlagContext';

interface IFlagProvider {
export interface IFlagProvider {
config?: IConfig;
unleashClient?: UnleashClient;
startClient?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import useVariant from './useVariant';
import useUnleashContext from './useUnleashContext';
import useUnleashClient from './useUnleashClient';

import { IFlagProvider } from './FlagProvider';

export {
FlagContext,
FlagProvider,
Expand All @@ -34,4 +36,6 @@ export {
useUnleashClient,
};

export type { IFlagProvider };

export default FlagProvider;
18 changes: 14 additions & 4 deletions src/useFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@ const useFlag = (name: string) => {

useEffect(() => {
if (!client) return;
client.on('update', () => {

const updateHandler = () => {
const enabled = isEnabled(name);
if (enabled !== flagRef.current) {
flagRef.current = enabled;
setFlag(!!enabled);
}
});
};

client.on('ready', () => {
const readyHandler = () => {
const enabled = isEnabled(name);
flagRef.current = enabled;
setFlag(enabled);
});
};

client.on('update', updateHandler);
client.on('ready', readyHandler);

() => {
client.off('update', updateHandler);
client.off('ready', readyHandler);
};
}, [client]);

return flag;
Expand Down
19 changes: 15 additions & 4 deletions src/useVariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const useVariant = (name: string): Partial<IVariant> => {

useEffect(() => {
if (!client) return;
client.on('update', () => {

const updateHandler = () => {
const newVariant = getVariant(name);
if (
variantRef.current.name !== newVariant.name ||
Expand All @@ -20,12 +21,22 @@ const useVariant = (name: string): Partial<IVariant> => {
setVariant(newVariant);
variantRef.current = newVariant;
}
});
};

client.on('ready', () => {
const readyHandler = () => {
const variant = getVariant(name);
variantRef.current.name = variant.name;
variantRef.current.enabled = variant.enabled;
setVariant(variant);
});
};

client.on('update', updateHandler);
client.on('ready', readyHandler);

() => {
client.off('update', updateHandler);
client.off('ready', readyHandler);
};
}, [client]);

return variant || {};
Expand Down