Skip to content

Commit

Permalink
Added option to start the client after fetching toggles (synchronized)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-unleash committed Jul 13, 2022
1 parent 92f9828 commit a68de04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ 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 @@ -212,6 +213,28 @@ 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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const EVENTS = {
READY: 'ready',
UPDATE: 'update',
IMPRESSION: 'impression',
SYNCHRONIZED: 'synchronized',
};

const IMPRESSION_EVENTS = {
Expand Down Expand Up @@ -97,6 +98,7 @@ 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;
Expand Down Expand Up @@ -152,6 +154,11 @@ export class UnleashClient extends TinyEmitter {
resolve();
});

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

if (!fetch) {
// tslint:disable-next-line
console.error(
Expand Down Expand Up @@ -271,7 +278,12 @@ export class UnleashClient extends TinyEmitter {
this.emit(EVENTS.INIT);
}

public async start(): Promise<void> {
public async startSynced(): Promise<void> {
return this.start(true);
}


public async start(blockUntilSynced = false): Promise<void> {
if (this.timerRef) {
console.error(
'Unleash SDK has already started, if you want to restart the SDK you should call client.stop() before starting again.'
Expand All @@ -281,7 +293,8 @@ export class UnleashClient extends TinyEmitter {
await this.ready;
this.metrics.start();
const interval = this.refreshInterval;
await this.fetchToggles();

blockUntilSynced ? await this.synchronized : await this.fetchToggles();

if (!this.bootstrap) {
this.emit(EVENTS.READY);
Expand Down Expand Up @@ -319,7 +332,7 @@ export class UnleashClient extends TinyEmitter {
'Content-Type': 'application/json',
'If-None-Match': this.etag
}
Object.entries(this.customHeaders).filter(notNullOrUndefined).forEach(([name, value]) =>
Object.entries(this.customHeaders).filter(notNullOrUndefined).forEach(([name, value]) =>
headers[name] = value);
return headers;

Expand Down Expand Up @@ -369,6 +382,7 @@ export class UnleashClient extends TinyEmitter {
this.etag = response.headers.get('ETag') || '';
const data = await response.json();
await this.storeToggles(data.toggles);
this.emit(EVENTS.SYNCHRONIZED);
}
} catch (e) {
// tslint:disable-next-line
Expand Down

0 comments on commit a68de04

Please sign in to comment.