Skip to content

Commit 023d301

Browse files
[FSSDK-10120] setForcedDecision does not reflects in Optimizely client instance (#274)
* [FSSDK-10544] hook init subscription code refactor * [FSSDK-10544] useExperiment code + test refactor * [FSSDK-10544] useFeature code + test refactor * [FSSDK-10544] useDecision + useTrackEvent update * [FSSDK-10544] hook log improvement * [FSSDK-10544] test comments cleanup * [FSSDK-10120] forced decision bug fix
1 parent 2950991 commit 023d301

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/client.ts

+6
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
361361
return null;
362362
}
363363

364+
if (this.userContext && areUsersEqual(userInfo, this.user)) {
365+
// Important: We need to return the existing user context instance if the user info is the same
366+
// new context misses the forced variation set on the existing context
367+
return this.userContext;
368+
}
369+
364370
return this._client.createUserContext(userInfo.id || undefined, userInfo.attributes);
365371
}
366372

src/utils.tsx

+6-15
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,18 @@ export function areUsersEqual(user1: UserInfo, user2: UserInfo): boolean {
3232
return false;
3333
}
3434

35-
const user1keys = Object.keys(user1.attributes || {});
36-
const user2keys = Object.keys(user2.attributes || {});
37-
user1keys.sort();
38-
user2keys.sort();
39-
4035
const user1Attributes = user1.attributes || {};
4136
const user2Attributes = user2.attributes || {};
4237

43-
const areKeysLenEqual = user1keys.length === user2keys.length;
44-
if (!areKeysLenEqual) {
38+
const user1Keys = Object.keys(user1Attributes);
39+
const user2Keys = Object.keys(user2Attributes);
40+
41+
if (user1Keys.length !== user2Keys.length) {
4542
return false;
4643
}
4744

48-
for (let i = 0; i < user1keys.length; i++) {
49-
const key1 = user1keys[i];
50-
const key2 = user2keys[i];
51-
if (key1 !== key2) {
52-
return false;
53-
}
54-
55-
if (user1Attributes[key1] !== user2Attributes[key2]) {
45+
for (const key of user1Keys) {
46+
if (user1Attributes[key] !== user2Attributes[key]) {
5647
return false;
5748
}
5849
}

0 commit comments

Comments
 (0)