Skip to content

Commit 9de061f

Browse files
authored
Merge pull request #77 from launchdarkly/christie/sc-164657/no-variation-call-on-init
No variation call on init
2 parents 06ad141 + 4abd3fe commit 9de061f

File tree

6 files changed

+26
-27
lines changed

6 files changed

+26
-27
lines changed

src/getFlagsProxy.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ test('filters to target flags', () => {
5050

5151
expect(flags).toEqual({ fooBar: 'foobar' });
5252
});
53+
54+
test('does not use proxy if option is false', () => {
55+
const { flags } = getFlagsProxy(ldClient, rawFlags, { sendEventsOnFlagRead: false });
56+
57+
expect(flags['foo-bar']).toBe('foobar');
58+
59+
expect(variation).not.toHaveBeenCalled();
60+
});

src/getFlagsProxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function getFlagsProxy(
2626
}
2727

2828
return {
29-
flags: toFlagsProxy(ldClient, flags, flagKeyMap),
29+
flags: reactOptions.sendEventsOnFlagRead ? toFlagsProxy(ldClient, flags, flagKeyMap) : flags,
3030
flagKeyMap,
3131
};
3232
}

src/initLDClient.test.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,6 @@ describe('initLDClient', () => {
5858
expect(mockLDClient.variation).toHaveBeenCalledTimes(0);
5959
});
6060

61-
test('initialise should call variation if flags are specified', async () => {
62-
const customUser = { key: '[email protected]' };
63-
const targetFlags = { 'lonely-flag': false, 'lonelier-flag': false };
64-
65-
const flagsClient = await initLDClient(clientSideID, customUser, options, targetFlags);
66-
67-
expect(mockLDClient.variation).toHaveBeenCalledTimes(2);
68-
expect(mockLDClient.variation).toHaveBeenNthCalledWith(1, 'lonely-flag', false);
69-
expect(mockLDClient.variation).toHaveBeenNthCalledWith(2, 'lonelier-flag', false);
70-
expect(flagsClient).toEqual({ flags: { 'lonely-flag': true, 'lonelier-flag': true }, ldClient: mockLDClient });
71-
});
72-
7361
test('may explicity set sendEventsOnlyForVariation to false', async () => {
7462
const anonUser: LDUser = { anonymous: true };
7563
await initLDClient(clientSideID, undefined, { ...options, sendEventsOnlyForVariation: false });

src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ export interface LDReactOptions {
2121
* @see https://docs.launchdarkly.com/sdk/client-side/react/react-web#flag-keys
2222
*/
2323
useCamelCaseFlagKeys?: boolean;
24+
25+
/**
26+
* Whether to send flag evaluation events when a flag is read from the `flags` object
27+
* retured by the `useFlags` hook. This is true by default, meaning flag evaluation
28+
* events will be sent by default.
29+
*/
30+
sendEventsOnFlagRead?: boolean;
2431
}
2532

2633
/**
2734
* Contains default values for the `reactOptions` object.
2835
*/
29-
export const defaultReactOptions = { useCamelCaseFlagKeys: true };
36+
export const defaultReactOptions = { useCamelCaseFlagKeys: true, sendEventsOnFlagRead: true };
3037

3138
/**
3239
* Configuration object used to initialise LaunchDarkly's JS client.

src/utils.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,17 @@ describe('Utils', () => {
8585
};
8686
});
8787

88-
test('should return variations for the target flags', () => {
88+
test('should return only the target flags', () => {
8989
const targetFlags = { 'target-one': true, 'target-two': true, 'target-three': false };
9090
const flagSet = fetchFlags(mockLDClient as LDClient, targetFlags);
9191

92-
expect(mockLDClient.allFlags).toBeCalledTimes(0);
93-
expect(mockLDClient.variation).toBeCalledTimes(3);
9492
expect(flagSet).toEqual({ 'target-one': true, 'target-three': false, 'target-two': true });
9593
});
9694

9795
test('should return all flags when target flags is not defined', () => {
9896
const flagSet = fetchFlags(mockLDClient as LDClient, undefined);
9997

10098
expect(mockLDClient.allFlags).toBeCalledTimes(1);
101-
expect(mockLDClient.variation).toBeCalledTimes(0);
10299
expect(flagSet).toEqual({ 'example-flag': true, 'test-example': false });
103100
});
104101
});

src/utils.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,16 @@ export const getFlattenedFlagsFromChangeset = (
5353
* @returns an `LDFlagSet` with the current flag values from LaunchDarkly filtered by `targetFlags`.
5454
*/
5555
export const fetchFlags = (ldClient: LDClient, targetFlags?: LDFlagSet) => {
56-
let rawFlags: LDFlagSet = {};
57-
58-
if (targetFlags) {
59-
for (const flag in targetFlags) {
60-
rawFlags[flag] = ldClient.variation(flag, targetFlags[flag]);
61-
}
62-
} else {
63-
rawFlags = ldClient.allFlags();
56+
const allFlags = ldClient.allFlags();
57+
if (!targetFlags) {
58+
return allFlags;
6459
}
6560

66-
return rawFlags;
61+
return Object.keys(targetFlags).reduce<LDFlagSet>((acc, key) => {
62+
acc[key] = Object.prototype.hasOwnProperty.call(allFlags, key) ? allFlags[key] : targetFlags[key];
63+
64+
return acc;
65+
}, {});
6766
};
6867

6968
/**

0 commit comments

Comments
 (0)