Skip to content

Commit

Permalink
[FSSDK-10120] setForcedDecision does not reflects in Optimizely clien…
Browse files Browse the repository at this point in the history
…t 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
  • Loading branch information
junaed-optimizely authored Aug 15, 2024
1 parent 2950991 commit 023d301
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
return null;
}

if (this.userContext && areUsersEqual(userInfo, this.user)) {
// Important: We need to return the existing user context instance if the user info is the same
// new context misses the forced variation set on the existing context
return this.userContext;
}

return this._client.createUserContext(userInfo.id || undefined, userInfo.attributes);
}

Expand Down
21 changes: 6 additions & 15 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,18 @@ export function areUsersEqual(user1: UserInfo, user2: UserInfo): boolean {
return false;
}

const user1keys = Object.keys(user1.attributes || {});
const user2keys = Object.keys(user2.attributes || {});
user1keys.sort();
user2keys.sort();

const user1Attributes = user1.attributes || {};
const user2Attributes = user2.attributes || {};

const areKeysLenEqual = user1keys.length === user2keys.length;
if (!areKeysLenEqual) {
const user1Keys = Object.keys(user1Attributes);
const user2Keys = Object.keys(user2Attributes);

if (user1Keys.length !== user2Keys.length) {
return false;
}

for (let i = 0; i < user1keys.length; i++) {
const key1 = user1keys[i];
const key2 = user2keys[i];
if (key1 !== key2) {
return false;
}

if (user1Attributes[key1] !== user2Attributes[key2]) {
for (const key of user1Keys) {
if (user1Attributes[key] !== user2Attributes[key]) {
return false;
}
}
Expand Down

0 comments on commit 023d301

Please sign in to comment.