Skip to content

Commit

Permalink
fix: allow start client to be used for config (#162)
Browse files Browse the repository at this point in the history
fix: allow start client to be used for config
  • Loading branch information
FredrikOseberg committed Feb 27, 2024
1 parent f53a33e commit 80d8bdc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ Upgrading should be as easy as running yarn again with the new version, but we m

## Upgrade path from v3 -> v4

`startClient` option has been simpilfied. Now it will also work if you don't pass custom client with it, and in SSR (when `typeof window === 'undefined'`) it defaults to `false`.
`startClient` option has been simplified. Now it will also work if you don't pass custom client with it. It defaults to `true`.

#### Note on v4.0.0:
The major release is driven by Node14 end of life and represents no other changes. From this version onwards we do not guarantee that this library will work server side with Node 14.
Expand Down
24 changes: 24 additions & 0 deletions src/FlagProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,27 @@ test('should not start client if startClient is false', () => {
expect(localMock).not.toHaveBeenCalled();
expect(stopMock).not.toHaveBeenCalled();
});

test('should not start client if startClient is false when passing config', () => {
const localMock = vi.fn();
const stopMock = vi.fn();
UnleashClientSpy.mockReturnValue({
getVariant: getVariantMock,
updateContext: updateContextMock,
start: localMock,
stop: stopMock,
isEnabled: isEnabledMock,
on: onMock,
off: offMock,
});


render(
<FlagProvider config={givenConfig} startClient={false}>
<div>Hi</div>
</FlagProvider>
);

expect(localMock).not.toHaveBeenCalled();
expect(stopMock).not.toHaveBeenCalled();
});
3 changes: 1 addition & 2 deletions src/FlagProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ const FlagProvider: React.FC<React.PropsWithChildren<IFlagProvider>> = ({
client.current.on('error', errorCallback);
client.current.on('recovered', clearErrorCallback);

const shouldStartClient = startClient || !unleashClient;
if (shouldStartClient) {
if (startClient) {
// defensively stop the client first
client.current.stop();
// start the client
Expand Down

0 comments on commit 80d8bdc

Please sign in to comment.