Skip to content

Commit

Permalink
Only emit Ready event once the first fetch was successful
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-unleash committed Jul 13, 2022
1 parent 089d939 commit d31e1bd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 35 deletions.
23 changes: 0 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ unleash.on('update', () => {
- **initialized** - emitted after the SDK has read local cached data in the storageProvider.
- **ready** - emitted after the SDK has successfully started and performed the initial fetch towards the Unleash Proxy.
- **update** - emitted every time the Unleash Proxy return a new feature toggle configuration. The SDK will emit this event as part of the initial fetch from the SDK.
- **synchronized** - emitted when the proxy-client is synchronized with the proxy (features fetched)

> PS! Please remember that you should always register your event listeners before your call `unleash.start()`. If you register them after you have started the SDK you risk loosing important events.
Expand Down Expand Up @@ -213,28 +212,6 @@ console.log(isEnabled);
```
*index.mjs*

#### Block until the proxy client has synchronized

You can also use the `startSynced` function for the SDK to have fully synchronized
with the unleash-proxy-api. This allows you to secure that the client is not operating on locally and
potential stale feature toggle configuration.
```js
import fetch from 'node-fetch';
import { UnleashClient, InMemoryStorageProvider } from 'unleash-proxy-client';

const unleash = new UnleashClient({
url: 'https://app.unleash-hosted.com/demo/proxy',
clientKey: 'proxy-123',
appName: 'nodejs-proxy',
storageProvider: new InMemoryStorageProvider(),
fetch,
});

await unleash.startSynced();
const isEnabled = unleash.isEnabled('proxy.demo');
console.log(isEnabled);
```

## How to use the client via CDN.

```html
Expand Down
28 changes: 28 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1077,3 +1077,31 @@ test('Should call getVariant event when impressionData is true', (done) => {
done();
});
});

test('Should publish ready only when the first fetch was successful', async () => {
fetchMock.mockResponse(JSON.stringify(data));
const config: IConfig = {
url: 'http://localhost/test',
clientKey: '12',
appName: 'web',
refreshInterval: 1,
};
const client = new UnleashClient(config);
await client.start();

let readyCount = 0;

client.on(EVENTS.READY, () => {
const isEnabled = client.isEnabled('simpleToggle');
expect(isEnabled).toBe(true);
readyCount++;
client.stop();
expect(readyCount).toEqual(1);
});

jest.advanceTimersByTime(1001);
jest.advanceTimersByTime(1001);

expect(fetchMock).toHaveBeenCalledTimes(3);

});
20 changes: 8 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ export class UnleashClient extends TinyEmitter {
private etag: string = '';
private metrics: Metrics;
private ready: Promise<void>;
private synchronized: Promise<void>;
private fetch: any;
private bootstrap?: IToggle[];
private bootstrapOverride: boolean;
private headerName: string;
private eventsHandler: EventsHandler;
private customHeaders: Record<string, string>;
private readyEventEmitted = false;

constructor({
storageProvider,
Expand Down Expand Up @@ -154,12 +154,6 @@ export class UnleashClient extends TinyEmitter {
resolve();
});

this.synchronized = new Promise(async (resolve) => {
await this.fetchToggles();
this.emit(EVENTS.SYNCHRONIZED);
resolve();
});

if (!fetch) {
// tslint:disable-next-line
console.error(
Expand Down Expand Up @@ -290,11 +284,7 @@ export class UnleashClient extends TinyEmitter {
this.metrics.start();
const interval = this.refreshInterval;

await this.synchronized;

if (!this.bootstrap) {
this.emit(EVENTS.READY);
}
await this.fetchToggles();

if (interval > 0) {
this.timerRef = setInterval(() => this.fetchToggles(), interval);
Expand Down Expand Up @@ -378,6 +368,12 @@ export class UnleashClient extends TinyEmitter {
this.etag = response.headers.get('ETag') || '';
const data = await response.json();
await this.storeToggles(data.toggles);

if (!this.bootstrap && !this.readyEventEmitted) {
this.emit(EVENTS.READY);
this.readyEventEmitted = true;
}

}
} catch (e) {
// tslint:disable-next-line
Expand Down

0 comments on commit d31e1bd

Please sign in to comment.