Skip to content

Commit

Permalink
fix(react-bindings): multiple client async start handling while loading
Browse files Browse the repository at this point in the history
  • Loading branch information
nadavshatz committed Dec 24, 2024
1 parent e674819 commit 3b7b748
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
29 changes: 15 additions & 14 deletions packages/react-bindings/src/useStatsigInternalClientFactoryAsync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo, useRef, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';

import { Log, StatsigUser, _getInstance } from '@statsig/client-core';
import { StatsigClient, StatsigOptions } from '@statsig/js-client';
Expand All @@ -14,24 +14,25 @@ export function useStatsigInternalClientFactoryAsync<T extends StatsigClient>(
args: FactoryArgs,
): { isLoading: boolean; client: T } {
const [isLoading, setIsLoading] = useState(true);
const clientRef = useRef<T | null>(_getInstance(args.sdkKey) as T | null);

const client = useMemo(() => {
if (clientRef.current) {
return clientRef.current;
const clientRef = useRef<T>(
(_getInstance(args.sdkKey) as T | null) ?? factory(args),
);

useEffect(() => {
// already initializing or initialized
if (clientRef.current.loadingStatus !== 'Uninitialized') {
setIsLoading(false);
return;
}

const inst = factory(args);

clientRef.current = inst;

inst
clientRef.current
.initializeAsync()
.catch(Log.error)
.finally(() => setIsLoading(false));

return inst;
}, []);

return { client, isLoading };
return {
client: clientRef.current,
isLoading,
};
}
7 changes: 6 additions & 1 deletion samples/react/src/__tests__/HomePage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render, waitFor } from '@testing-library/react';
import fetchMock from 'jest-fetch-mock';
import { StrictMode } from 'react';
import { InitResponse } from 'statsig-test-helpers';

import HomePage from '../HomePage';
Expand All @@ -10,7 +11,11 @@ describe('App', () => {
});

it('renders the Passing value', async () => {
const { getByText } = render(<HomePage />);
const { getByText } = render(
<StrictMode>
<HomePage />
</StrictMode>,
);

const result = await waitFor(() => getByText('Passing'));
expect(result).toBeDefined();
Expand Down
8 changes: 6 additions & 2 deletions samples/react/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '@fontsource/roboto/400.css';
import '@fontsource/roboto/500.css';
import '@fontsource/roboto/700.css';
import { Box } from '@mui/material';
import { ReactNode, lazy } from 'react';
import { ReactNode, StrictMode, lazy } from 'react';
import * as ReactDOM from 'react-dom/client';
import {
RouteObject,
Expand Down Expand Up @@ -89,4 +89,8 @@ function App() {
);
}

root.render(<App />);
root.render(
<StrictMode>
<App />
</StrictMode>,
);

0 comments on commit 3b7b748

Please sign in to comment.