Skip to content

Commit 2693832

Browse files
authored
fix: make setContextField and removeContextField be async (#254)
1 parent e8ac895 commit 2693832

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/index.test.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ test('Should setContextField with userId', async () => {
11741174
appName: 'web',
11751175
};
11761176
const client = new UnleashClient(config);
1177-
client.setContextField('userId', userId);
1177+
await client.setContextField('userId', userId);
11781178
const context = client.getContext();
11791179
expect(context.userId).toBe(userId);
11801180
});
@@ -1188,17 +1188,28 @@ test('Should removeContextField', async () => {
11881188
appName: 'web',
11891189
};
11901190
const client = new UnleashClient(config);
1191-
client.setContextField('userId', userId);
1191+
await client.setContextField('userId', userId);
11921192
client.setContextField('customField', customValue);
1193+
const context1 = client.getContext();
1194+
expect(context1).toEqual({
1195+
appName: 'web',
1196+
environment: 'default',
1197+
properties: {
1198+
customField: customValue,
1199+
},
1200+
sessionId: expect.any(String),
1201+
userId: userId,
1202+
});
11931203

1194-
client.removeContextField('userId');
1195-
client.removeContextField('customField');
1196-
const context = client.getContext();
1204+
await client.removeContextField('userId');
1205+
await client.removeContextField('customField');
1206+
const context2 = client.getContext();
11971207

1198-
expect(context).toEqual({
1208+
expect(context2).toEqual({
11991209
appName: 'web',
12001210
environment: 'default',
12011211
properties: {},
1212+
sessionId: expect.any(String),
12021213
});
12031214
});
12041215

@@ -1210,7 +1221,7 @@ test('Should setContextField with sessionId', async () => {
12101221
appName: 'web',
12111222
};
12121223
const client = new UnleashClient(config);
1213-
client.setContextField('sessionId', sessionId);
1224+
await client.setContextField('sessionId', sessionId);
12141225
const context = client.getContext();
12151226
expect(context.sessionId).toBe(sessionId);
12161227
});

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,25 +373,25 @@ export class UnleashClient extends TinyEmitter {
373373
return { ...this.context };
374374
}
375375

376-
public setContextField(field: string, value: string) {
376+
public async setContextField(field: string, value: string): Promise<void> {
377377
if (isDefinedContextField(field)) {
378378
this.context = { ...this.context, [field]: value };
379379
} else {
380380
const properties = { ...this.context.properties, [field]: value };
381381
this.context = { ...this.context, properties };
382382
}
383383

384-
this.updateToggles();
384+
await this.updateToggles();
385385
}
386386

387-
public removeContextField(field: string): void {
387+
public async removeContextField(field: string): Promise<void> {
388388
if (isDefinedContextField(field)) {
389389
this.context = { ...this.context, [field]: undefined };
390390
} else if (typeof this.context.properties === 'object') {
391391
delete this.context.properties[field];
392392
}
393393

394-
this.updateToggles();
394+
await this.updateToggles();
395395
}
396396

397397
private setReady() {

0 commit comments

Comments
 (0)