Skip to content
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

Fix/error when using update context #93

Merged
merged 8 commits into from
Nov 16, 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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unleash/proxy-client-react",
"version": "3.4.0",
"version": "3.4.1-beta.0",
"description": "React interface for working with unleash",
"main": "./dist/index.js",
"browser": "./dist/index.browser.js",
Expand Down
5 changes: 5 additions & 0 deletions src/FlagProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const updateContextMock = jest.fn();
const startClientMock = jest.fn();
const stopClientMock = jest.fn();
const onMock = jest.fn().mockReturnValue('subscribed');
const offMock = jest.fn();
const isEnabledMock = jest.fn().mockReturnValue(true);
const UnleashClientSpy: jest.SpyInstance = jest.spyOn(
UnleashClientModule,
Expand All @@ -49,6 +50,7 @@ UnleashClientSpy.mockReturnValue({
stop: stopClientMock,
isEnabled: isEnabledMock,
on: onMock,
off: offMock,
});

const noop = () => {};
Expand Down Expand Up @@ -232,6 +234,7 @@ test('should update when ready event is sent', () => {
stop: stopClientMock,
isEnabled: isEnabledMock,
on: localMock,
off: offMock,
});

const providerProps = {
Expand Down Expand Up @@ -264,6 +267,7 @@ test('should register error when error event is sent', () => {
stop: stopClientMock,
isEnabled: isEnabledMock,
on: localMock,
off: offMock,
});

const providerProps = {
Expand Down Expand Up @@ -297,6 +301,7 @@ test('should not start client if startClient is false', () => {
stop: stopMock,
isEnabled: isEnabledMock,
on: onMock,
off: offMock,
});

const providerProps = {
Expand Down
25 changes: 20 additions & 5 deletions src/FlagProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const FlagProvider: React.FC<React.PropsWithChildren<IFlagProvider>> = ({
const client = React.useRef<UnleashClient>(unleashClient);
const [flagsReady, setFlagsReady] = React.useState(false);
const [flagsError, setFlagsError] = React.useState(null);
const flagsErrorRef = React.useRef(null);
const callbackRegisteredRef = React.useRef(null);

if (!config && !unleashClient) {
console.warn(
Expand All @@ -31,13 +33,24 @@ const FlagProvider: React.FC<React.PropsWithChildren<IFlagProvider>> = ({
client.current = new UnleashClient(config);
}

client.current.on('ready', () => {
const errorCallback = (e: any) => {
// Use a ref because regular event handlers are closing over state
// with stale values:
flagsErrorRef.current = e;

if (flagsErrorRef.current === null) {
setFlagsError(e);
}
};
const readyCallback = () => {
setFlagsReady(true);
});
};

client.current.on('error', (e: any) => {
setFlagsError(e);
});
if (!callbackRegisteredRef.current) {
client.current.on('ready', readyCallback);
client.current.on('error', errorCallback);
callbackRegisteredRef.current = 'set';
}

React.useEffect(() => {
const shouldStartClient = startClient || !unleashClient;
Expand All @@ -51,6 +64,8 @@ const FlagProvider: React.FC<React.PropsWithChildren<IFlagProvider>> = ({
// stop unleash client on unmount
return function cleanup() {
if (client.current) {
client.current.off('error', errorCallback);
client.current.off('ready', readyCallback);
client.current.stop();
}
};
Expand Down