Skip to content

Commit 9d1e913

Browse files
committed
Don't call variation when initializing
1 parent c15b2e4 commit 9d1e913

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

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/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
@@ -54,17 +54,16 @@ export const getFlattenedFlagsFromChangeset = (
5454
* @returns an `LDFlagSet` with the current flag values from LaunchDarkly filtered by `targetFlags`.
5555
*/
5656
export const fetchFlags = (ldClient: LDClient, targetFlags?: LDFlagSet) => {
57-
let rawFlags: LDFlagSet = {};
58-
59-
if (targetFlags) {
60-
for (const flag in targetFlags) {
61-
rawFlags[flag] = ldClient.variation(flag, targetFlags[flag]);
62-
}
63-
} else {
64-
rawFlags = ldClient.allFlags();
57+
const allFlags = ldClient.allFlags();
58+
if (!targetFlags) {
59+
return allFlags;
6560
}
6661

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

7069
/**

0 commit comments

Comments
 (0)