Skip to content

Commit be7851c

Browse files
feat: Accept timeout for LD waitForInitialization (#1117)
Signed-off-by: Nicholas Thomson <[email protected]>
1 parent e773416 commit be7851c

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

libs/providers/launchdarkly-client/src/lib/launchdarkly-client-provider.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ describe('LaunchDarklyClientProvider', () => {
8080
expect(initialize).toHaveBeenCalledTimes(1);
8181
/* when not set in open feauture LD sdk initialize should be called with the anonymous context*/
8282
expect(initialize).toHaveBeenCalledWith(envKey, { anonymous: true }, { logger });
83+
expect(ldClientMock.waitForInitialization).toHaveBeenCalledWith(undefined);
84+
});
85+
86+
it('should call Ld waitForInitialization with correct arguments', async () => {
87+
const provider = new LaunchDarklyClientProvider(envKey, { logger, initializationTimeout: 5 });
88+
await provider.initialize();
89+
expect(initialize).toHaveBeenCalledTimes(1);
90+
/* when not set in open feauture LD sdk initialize should be called with the anonymous context*/
91+
expect(initialize).toHaveBeenCalledWith(envKey, { anonymous: true }, { logger });
92+
expect(ldClientMock.waitForInitialization).toHaveBeenCalledTimes(1);
93+
expect(ldClientMock.waitForInitialization).toHaveBeenCalledWith(5);
8394
});
8495

8596
it('should set the status to READY if initialization succeeds', async () => {

libs/providers/launchdarkly-client/src/lib/launchdarkly-client-provider.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class LaunchDarklyClientProvider implements Provider {
4242

4343
private readonly ldOptions: LDOptions | undefined;
4444
private readonly logger: Logger;
45+
private readonly initializationTimeout?: number;
4546
private _client?: LDClient;
4647

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

6364
constructor(
6465
private readonly envKey: string,
65-
{ logger, ...ldOptions }: LaunchDarklyProviderOptions,
66+
{ logger, initializationTimeout, ...ldOptions }: LaunchDarklyProviderOptions,
6667
) {
6768
if (logger) {
6869
this.logger = logger;
6970
} else {
7071
this.logger = basicLogger({ level: 'info' });
7172
}
73+
this.initializationTimeout = initializationTimeout;
7274
this.ldOptions = { ...ldOptions, logger: this.logger };
7375
}
7476

@@ -92,7 +94,7 @@ export class LaunchDarklyClientProvider implements Provider {
9294
}
9395

9496
try {
95-
await this._client.waitForInitialization();
97+
await this._client.waitForInitialization(this.initializationTimeout);
9698
this.status = ProviderStatus.READY;
9799
} catch {
98100
this.status = ProviderStatus.ERROR;

libs/providers/launchdarkly-client/src/lib/launchdarkly-provider-options.ts

+12
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,16 @@ export interface LaunchDarklyProviderOptions extends LDOptions {
3636
* disclaimer: this logger instance will be used by the launch-darkly sdk
3737
*/
3838
logger?: LDLogger | Logger;
39+
40+
/**
41+
* Configures the amount of time, in seconds, to wait for initialization when
42+
* connecting to the LaunchDarkly service.
43+
*
44+
* Using a large timeout is not recommended as network delays may cause your
45+
* application to wait a long time before continuing execution.
46+
*
47+
* See the launchdarkly-js-client-sdk docs for more details:
48+
* {@link https://launchdarkly.github.io/js-client-sdk/interfaces/LDClient.html#waitForInitialization}
49+
*/
50+
initializationTimeout?: number;
3951
}

0 commit comments

Comments
 (0)