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

[FSSDK-9563] [React] Fix onready segments #206

Merged
merged 4 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 22 additions & 6 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jest.mock('@optimizely/optimizely-sdk');
jest.mock('./logger', () => {
return {
logger: {
warn: jest.fn(() => () => {}),
info: jest.fn(() => () => {}),
error: jest.fn(() => () => {}),
debug: jest.fn(() => () => {}),
warn: jest.fn(() => () => { }),
info: jest.fn(() => () => { }),
error: jest.fn(() => () => { }),
debug: jest.fn(() => () => { }),
},
};
});
Expand Down Expand Up @@ -251,19 +251,35 @@ describe('ReactSDKClient', () => {
expect(onUserUpdateListener).toBeCalledTimes(1);
});

it('calls fetchqualifiedsegements internally on each setuser call', async () => {
it('does not call fetchqualifiedsegements on setUser if onready is not calleed initially', async () => {
const instance = createInstance(config);
jest.spyOn(instance, 'fetchQualifiedSegments').mockImplementation(async () => true);

await instance.setUser({
id: 'xxfueaojfe8&86',
});

expect(instance.fetchQualifiedSegments).toBeCalledTimes(0);
});

it('calls fetchqualifiedsegements internally on each setuser call after onready', async () => {
const instance = createInstance(config);
jest.spyOn(instance, 'fetchQualifiedSegments').mockImplementation(async () => true);

await instance.setUser({
id: 'xxfueaojfe8&86',
});
await instance.onReady()

await instance.setUser({
id: 'xxfueaojfe8&87',
});

await instance.setUser({
id: 'xxfueaojfe8&87',
});

expect(instance.fetchQualifiedSegments).toBeCalledTimes(2);
expect(instance.fetchQualifiedSegments).toBeCalledTimes(3);
rafinutshaw-optimizely marked this conversation as resolved.
Show resolved Hide resolved
});

describe('pre-set user and user overrides', () => {
Expand Down
27 changes: 15 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
*/
constructor(config: optimizely.Config) {
this.initialConfig = config;
this.userPromiseResolver = () => {};
this.userPromiseResolver = () => { };

const configWithClientInfo = {
...config,
Expand All @@ -244,9 +244,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
});

this.dataReadyPromise = Promise.all([this.userPromise, this._client!.onReady()]).then(res => {
if (!res[0].success) {
return res[0];
}

// Client and user can become ready synchronously and/or asynchronously. This flag specifically indicates that they became ready asynchronously.
this.isReadyPromiseFulfilled = true;
Expand Down Expand Up @@ -309,8 +306,18 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
}, timeout) as any;
});

return Promise.race([this.dataReadyPromise, timeoutPromise]).then(res => {
return Promise.race([this.dataReadyPromise, timeoutPromise]).then(async res => {
clearTimeout(timeoutId);
if (res.success) {
const isSegmentsFetched = await this.fetchQualifiedSegments();
rafinutshaw-optimizely marked this conversation as resolved.
Show resolved Hide resolved
if (!isSegmentsFetched) {
return {
success: false,
reason: 'USER_NOT_READY',
message: 'Failed to fetch qualified segments',
}
}
}
return res;
});
}
Expand Down Expand Up @@ -357,7 +364,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
}

public async setUser(userInfo: UserInfo): Promise<void> {
this.isUserPromiseResolved = false;
this.isUserReady = true;

//reset user info
Expand All @@ -377,16 +383,13 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
if (userInfo.attributes) {
this.user.attributes = userInfo.attributes;
}
const isSegmentsFetched = await this.fetchQualifiedSegments();

const segmentsResult: ResolveResult = { success: isSegmentsFetched };
if (!isSegmentsFetched) {
segmentsResult.reason = 'USER_NOT_READY';
segmentsResult.message = 'Failed to fetch qualified segments';
if (this.getIsReadyPromiseFulfilled()) {
await this.fetchQualifiedSegments();
}

if (!this.isUserPromiseResolved) {
this.userPromiseResolver(segmentsResult);
this.userPromiseResolver({ success: true });
this.isUserPromiseResolved = true;
}

Expand Down