Skip to content

Commit 2bba887

Browse files
[FSSDK-9563] [React] Fix onready segments (#206)
* Fix qualified segments onready * Fix onready return
1 parent c96680d commit 2bba887

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/client.spec.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ jest.mock('@optimizely/optimizely-sdk');
1717
jest.mock('./logger', () => {
1818
return {
1919
logger: {
20-
warn: jest.fn(() => () => {}),
21-
info: jest.fn(() => () => {}),
22-
error: jest.fn(() => () => {}),
23-
debug: jest.fn(() => () => {}),
20+
warn: jest.fn(() => () => { }),
21+
info: jest.fn(() => () => { }),
22+
error: jest.fn(() => () => { }),
23+
debug: jest.fn(() => () => { }),
2424
},
2525
};
2626
});
@@ -251,19 +251,35 @@ describe('ReactSDKClient', () => {
251251
expect(onUserUpdateListener).toBeCalledTimes(1);
252252
});
253253

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

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

262+
expect(instance.fetchQualifiedSegments).toBeCalledTimes(0);
263+
});
264+
265+
it('calls fetchqualifiedsegements internally on each setuser call after onready', async () => {
266+
const instance = createInstance(config);
267+
jest.spyOn(instance, 'fetchQualifiedSegments').mockImplementation(async () => true);
268+
269+
await instance.setUser({
270+
id: 'xxfueaojfe8&86',
271+
});
272+
await instance.onReady()
273+
274+
await instance.setUser({
275+
id: 'xxfueaojfe8&87',
276+
});
277+
262278
await instance.setUser({
263279
id: 'xxfueaojfe8&87',
264280
});
265281

266-
expect(instance.fetchQualifiedSegments).toBeCalledTimes(2);
282+
expect(instance.fetchQualifiedSegments).toBeCalledTimes(3);
267283
});
268284

269285
describe('pre-set user and user overrides', () => {

src/client.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
219219
*/
220220
constructor(config: optimizely.Config) {
221221
this.initialConfig = config;
222-
this.userPromiseResolver = () => {};
222+
this.userPromiseResolver = () => { };
223223

224224
const configWithClientInfo = {
225225
...config,
@@ -244,9 +244,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
244244
});
245245

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

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

312-
return Promise.race([this.dataReadyPromise, timeoutPromise]).then(res => {
309+
return Promise.race([this.dataReadyPromise, timeoutPromise]).then(async res => {
313310
clearTimeout(timeoutId);
311+
if (res.success) {
312+
const isSegmentsFetched = await this.fetchQualifiedSegments();
313+
if (!isSegmentsFetched) {
314+
return {
315+
success: false,
316+
reason: 'USER_NOT_READY',
317+
message: 'Failed to fetch qualified segments',
318+
}
319+
}
320+
}
314321
return res;
315322
});
316323
}
@@ -357,7 +364,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
357364
}
358365

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

363369
//reset user info
@@ -377,16 +383,13 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
377383
if (userInfo.attributes) {
378384
this.user.attributes = userInfo.attributes;
379385
}
380-
const isSegmentsFetched = await this.fetchQualifiedSegments();
381386

382-
const segmentsResult: ResolveResult = { success: isSegmentsFetched };
383-
if (!isSegmentsFetched) {
384-
segmentsResult.reason = 'USER_NOT_READY';
385-
segmentsResult.message = 'Failed to fetch qualified segments';
387+
if (this.getIsReadyPromiseFulfilled()) {
388+
await this.fetchQualifiedSegments();
386389
}
387390

388391
if (!this.isUserPromiseResolved) {
389-
this.userPromiseResolver(segmentsResult);
392+
this.userPromiseResolver({ success: true });
390393
this.isUserPromiseResolved = true;
391394
}
392395

0 commit comments

Comments
 (0)