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

Update context waits for start #130

Merged
merged 7 commits into from Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/index.test.ts
Expand Up @@ -48,7 +48,7 @@ test('Should perform an initial fetch as POST', async () => {
};
const client = new UnleashClient(config);
await client.start();

const request = getTypeSafeRequest(fetchMock, 0);
const body = JSON.parse(request.body as string);

Expand All @@ -65,7 +65,7 @@ test('Should perform an initial fetch as GET', async () => {
};
const client = new UnleashClient(config);
await client.start();

const request = getTypeSafeRequest(fetchMock, 0);

expect(request.method).toBe('GET');
Expand Down Expand Up @@ -852,6 +852,27 @@ test('Should update context fields on request', async () => {
expect(url.searchParams.get('environment')).toEqual('prod');
});

test('Updating context should wait on asynchronous start', async () => {
fetchMock.mockResponses(
[JSON.stringify(data), { status: 200 }],
[JSON.stringify(data), { status: 200 }]
);
const config: IConfig = {
url: 'http://localhost/test',
clientKey: '12',
appName: 'web',
environment: 'prod',
};
const client = new UnleashClient(config);

client.start();
await client.updateContext({
userId: '123'
});

expect(fetchMock).toBeCalledTimes(2);
});

test('Should not replace sessionId when updating context', async () => {
fetchMock.mockResponses(
[JSON.stringify(data), { status: 200 }],
Expand Down
16 changes: 14 additions & 2 deletions src/index.ts
Expand Up @@ -210,7 +210,7 @@ export class UnleashClient extends TinyEmitter {
const toggle = this.toggles.find((t) => t.name === toggleName);
const enabled = toggle?.enabled || false;
const variant = toggle ? toggle.variant : defaultVariant;

this.metrics.count(toggleName, true);
if (toggle?.impressionData || this.impressionDataAll) {
const event = this.eventsHandler.createImpressionEvent(
Expand Down Expand Up @@ -240,9 +240,21 @@ export class UnleashClient extends TinyEmitter {
sessionId: this.context.sessionId,
};
this.context = { ...staticContext, ...context };

if (this.timerRef) {
await this.fetchToggles();
}
else {
await new Promise<void>(resolve => {
const listener = () => {
this.fetchToggles().then(() => {
this.off(EVENTS.READY, listener);
resolve();
});
};
this.once(EVENTS.READY, listener);
});
}
}

public getContext() {
Expand Down Expand Up @@ -344,7 +356,7 @@ export class UnleashClient extends TinyEmitter {
const url = isPOST ? this.url : urlWithContextAsQuery(this.url, this.context);
const method = isPOST ? 'POST' : 'GET';
const body = isPOST ? JSON.stringify({context: this.context}) : undefined;

const response = await this.fetch(url.toString(), {
method,
cache: 'no-cache',
Expand Down