Skip to content

Commit

Permalink
fix: ready state when bootstrapping (#218)
Browse files Browse the repository at this point in the history
* fix: ready state when bootstrapping

* refactor: fetched from server internal state
  • Loading branch information
Tymek authored Jun 26, 2024
1 parent e2586b2 commit de95fe4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1756,3 +1756,28 @@ describe('READY event emission', () => {
expect(client.emit).toHaveBeenCalledWith(EVENTS.READY);
});
});

test('should be in ready state if bootstrapping', (done) => {
const config: IConfig = {
url: 'http://localhost/test',
clientKey: '12',
appName: 'web',
bootstrap: [
{
enabled: false,
name: 'test-frontend',
variant: { name: 'some-variant', enabled: false },
impressionData: false,
},
],
fetch: async () => {},
};

const client = new UnleashClient(config);

client.on(EVENTS.READY, () => {
expect(client.isReady()).toBe(true);
client.stop();
done();
});
});
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export class UnleashClient extends TinyEmitter {
private eventsHandler: EventsHandler;
private customHeaders: Record<string, string>;
private readyEventEmitted = false;
private fetchedFromServer = false;
private usePOSTrequests = false;
private started = false;
private sdkState: SdkState;
Expand Down Expand Up @@ -291,7 +292,7 @@ export class UnleashClient extends TinyEmitter {
}

private async updateToggles() {
if (this.timerRef || this.readyEventEmitted) {
if (this.timerRef || this.fetchedFromServer) {
await this.fetchToggles();
} else if (this.started) {
await new Promise<void>((resolve) => {
Expand Down Expand Up @@ -361,6 +362,8 @@ export class UnleashClient extends TinyEmitter {
) {
await this.storage.save(storeKey, this.bootstrap);
this.toggles = this.bootstrap;
this.sdkState = 'healthy';
this.readyEventEmitted = true;
this.emit(EVENTS.READY);
}

Expand Down Expand Up @@ -482,9 +485,10 @@ export class UnleashClient extends TinyEmitter {
this.sdkState = 'healthy';
}

if (!this.readyEventEmitted) {
this.emit(EVENTS.READY);
if (!this.fetchedFromServer) {
this.fetchedFromServer = true;
this.readyEventEmitted = true;
this.emit(EVENTS.READY);
}
} else if (!response.ok && response.status !== 304) {
console.error(
Expand Down

0 comments on commit de95fe4

Please sign in to comment.