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

Modify Provider to keep the latest flags even when it is remounted #238

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions src/asyncWithLDProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,31 @@ describe('asyncWithLDProvider', () => {

expect(receivedNode).toHaveTextContent('{"testFlag":false}');
});

test('Provider keeps the latest flags even when it is remounted', async () => {
mockInitLDClient.mockImplementation(() => ({
ldClient: mockLDClient,
flags: rawFlags,
}));
mockLDClient.on.mockImplementationOnce((e: string, cb: (c: LDFlagChangeset) => void) => {
cb({ 'test-flag': { current: false, previous: true }, 'another-test-flag': { current: false, previous: true } });
});
const options: LDOptions = {};
const LDProvider = await asyncWithLDProvider({ clientSideID, context, options });

const { unmount } = render(
<LDProvider>
<Consumer>{(value) => <span>Received: {JSON.stringify(value.flags)}</span>}</Consumer>
</LDProvider>,
);
unmount();
const { getByText } = render(
<LDProvider>
<Consumer>{(value) => <span>Received: {JSON.stringify(value.flags)}</span>}</Consumer>
</LDProvider>,
);
const receivedNode = getByText(/^Received:/);

expect(receivedNode).toHaveTextContent('{"testFlag":false,"anotherTestFlag":false}');
});
});
14 changes: 7 additions & 7 deletions src/asyncWithLDProvider.tsx
bufferings marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, ReactNode } from 'react';
import React, { ReactNode, useEffect, useState } from 'react';
import { LDFlagChangeset } from 'launchdarkly-js-client-sdk';
import { AsyncProviderConfig, defaultReactOptions } from './types';
import { Provider } from './context';
Expand Down Expand Up @@ -40,24 +40,24 @@ export default async function asyncWithLDProvider(config: AsyncProviderConfig) {
targetFlags,
);

const initialFlags = options?.bootstrap && options.bootstrap !== 'localStorage' ? options.bootstrap : fetchedFlags;
let currentFlags = options?.bootstrap && options.bootstrap !== 'localStorage' ? options.bootstrap : fetchedFlags;

const LDProvider = ({ children }: { children: ReactNode }) => {
const [ldData, setLDData] = useState(() => ({
unproxiedFlags: initialFlags,
...getFlagsProxy(ldClient, initialFlags, reactOptions, targetFlags),
unproxiedFlags: currentFlags,
...getFlagsProxy(ldClient, currentFlags, reactOptions, targetFlags),
kinyoklion marked this conversation as resolved.
Show resolved Hide resolved
}));

useEffect(() => {
function onChange(changes: LDFlagChangeset) {
const updates = getFlattenedFlagsFromChangeset(changes, targetFlags);
if (Object.keys(updates).length > 0) {
setLDData(({ unproxiedFlags }) => {
const updatedUnproxiedFlags = { ...unproxiedFlags, ...updates };
currentFlags = { ...unproxiedFlags, ...updates };

return {
unproxiedFlags: updatedUnproxiedFlags,
...getFlagsProxy(ldClient, updatedUnproxiedFlags, reactOptions, targetFlags),
unproxiedFlags: currentFlags,
...getFlagsProxy(ldClient, currentFlags, reactOptions, targetFlags),
};
});
}
Expand Down