Skip to content

Commit

Permalink
Accept timeout for LD waitForInitialization
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Thomson <RedbackThomson@users.noreply.github.com>
  • Loading branch information
RedbackThomson committed Dec 13, 2024
1 parent e773416 commit ff8d356
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ describe('LaunchDarklyClientProvider', () => {
expect(initialize).toHaveBeenCalledTimes(1);
/* when not set in open feauture LD sdk initialize should be called with the anonymous context*/
expect(initialize).toHaveBeenCalledWith(envKey, { anonymous: true }, { logger });
expect(ldClientMock.waitForInitialization).toHaveBeenCalledWith(undefined);
});

it('should call Ld waitForInitialization with correct arguments', async () => {
const provider = new LaunchDarklyClientProvider(envKey, { logger, initializationTimeout: 5 });
await provider.initialize();
expect(initialize).toHaveBeenCalledTimes(1);
/* when not set in open feauture LD sdk initialize should be called with the anonymous context*/
expect(initialize).toHaveBeenCalledWith(envKey, { anonymous: true }, { logger });
expect(ldClientMock.waitForInitialization).toHaveBeenCalledTimes(1);
expect(ldClientMock.waitForInitialization).toHaveBeenCalledWith(5);
});

it('should set the status to READY if initialization succeeds', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class LaunchDarklyClientProvider implements Provider {

private readonly ldOptions: LDOptions | undefined;
private readonly logger: Logger;
private readonly initializationTimeout?: number;
private _client?: LDClient;

public events = new OpenFeatureEventEmitter();
Expand All @@ -62,13 +63,14 @@ export class LaunchDarklyClientProvider implements Provider {

constructor(
private readonly envKey: string,
{ logger, ...ldOptions }: LaunchDarklyProviderOptions,
{ logger, initializationTimeout, ...ldOptions }: LaunchDarklyProviderOptions,
) {
if (logger) {
this.logger = logger;
} else {
this.logger = basicLogger({ level: 'info' });
}
this.initializationTimeout = initializationTimeout;
this.ldOptions = { ...ldOptions, logger: this.logger };
}

Expand All @@ -92,7 +94,7 @@ export class LaunchDarklyClientProvider implements Provider {
}

try {
await this._client.waitForInitialization();
await this._client.waitForInitialization(this.initializationTimeout);
this.status = ProviderStatus.READY;
} catch {
this.status = ProviderStatus.ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,16 @@ export interface LaunchDarklyProviderOptions extends LDOptions {
* disclaimer: this logger instance will be used by the launch-darkly sdk
*/
logger?: LDLogger | Logger;

/**
* Configures the amount of time, in seconds, to wait for initialization when
* connecting to the LaunchDarkly service.
*
* Using a large timeout is not recommended as network delays may cause your
* application to wait a long time before continuing execution.
*
* See the launchdarkly-js-client-sdk docs for more details:
* {@link https://launchdarkly.github.io/js-client-sdk/interfaces/LDClient.html#waitForInitialization}
*/
initializationTimeout?: number;
}

0 comments on commit ff8d356

Please sign in to comment.