Skip to content

Commit 47b3f51

Browse files
committed
feat: expose setConnectionMode on browser SDK and restrict automatic mode switching
Remove the automatic connection mode option from the browser SDK's public interface since it has no impact. Add setConnectionMode as an EAP method on the browser LDClient, giving users direct control over the connection mode with higher priority than setStreaming.
1 parent fe8bd37 commit 47b3f51

5 files changed

Lines changed: 70 additions & 2 deletions

File tree

packages/sdk/browser/src/BrowserClient.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Configuration,
88
createDefaultSourceFactoryProvider,
99
createFDv2DataManagerBase,
10+
FDv2ConnectionMode,
1011
FlagManager,
1112
Hook,
1213
internal,
@@ -318,6 +319,10 @@ class BrowserClientImpl extends LDClientImpl {
318319
return this._startPromise;
319320
}
320321

322+
setConnectionMode(mode?: FDv2ConnectionMode): void {
323+
this.dataManager.setConnectionMode?.(mode);
324+
}
325+
321326
setStreaming(streaming?: boolean): void {
322327
this.dataManager.setForcedStreaming?.(streaming);
323328
}
@@ -376,6 +381,7 @@ export function makeClient(
376381
on: (key: LDEmitterEventName, callback: (...args: any[]) => void) => impl.on(key, callback),
377382
off: (key: LDEmitterEventName, callback: (...args: any[]) => void) => impl.off(key, callback),
378383
flush: () => impl.flush(),
384+
setConnectionMode: (mode?: FDv2ConnectionMode) => impl.setConnectionMode(mode),
379385
setStreaming: (streaming?: boolean) => impl.setStreaming(streaming),
380386
identify: (pristineContext: LDContext, identifyOptions?: LDIdentifyOptions) =>
381387
impl.identifyResult(pristineContext, identifyOptions),

packages/sdk/browser/src/LDClient.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
FDv2ConnectionMode,
23
LDClient as CommonClient,
34
LDContext,
45
LDIdentifyResult,
@@ -34,15 +35,34 @@ export interface LDStartOptions extends LDWaitForInitializationOptions {
3435

3536
export type LDClient = Omit<
3637
CommonClient,
37-
'setConnectionMode' | 'getConnectionMode' | 'getOffline' | 'identify'
38+
'getConnectionMode' | 'getOffline' | 'identify'
3839
> & {
3940
/**
4041
* @ignore
41-
* Implementation Note: We are not supporting dynamically setting the connection mode on the LDClient.
4242
* Implementation Note: The SDK does not support offline mode. Instead bootstrap data can be used.
4343
* Implementation Note: The browser SDK has different identify options, so omits the base implementation
4444
* from the interface.
4545
*/
46+
/**
47+
* Sets the connection mode for the SDK's data system.
48+
*
49+
* When set, this mode is used exclusively, overriding all automatic mode
50+
* selection (including {@link setStreaming}). The {@link setStreaming} state
51+
* is not modified -- {@link setConnectionMode} acts as a higher-priority
52+
* override.
53+
*
54+
* Pass `undefined` (or call with no arguments) to clear the override and
55+
* return to automatic mode selection.
56+
*
57+
* This method is not stable, and not subject to any backwards compatibility
58+
* guarantees or semantic versioning. It is in early access. If you want access
59+
* to this feature please join the EAP.
60+
* https://launchdarkly.com/docs/sdk/features/data-saving-mode
61+
*
62+
* @param mode The connection mode to use, or `undefined` to clear the override.
63+
*/
64+
setConnectionMode(mode?: FDv2ConnectionMode): void;
65+
4666
/**
4767
* Specifies whether or not to open a streaming connection to LaunchDarkly for live flag updates.
4868
*

packages/sdk/browser/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { BrowserOptions as LDOptions } from './options';
1818

1919
export * from './common';
2020
export type { LDClient, LDOptions, LDStartOptions };
21+
export type { BrowserDataSystemOptions } from './options';
2122
export type { LDPlugin } from './LDPlugin';
2223

2324
/**

packages/sdk/browser/src/options.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {
2+
LDClientDataSystemOptions,
23
LDLogger,
34
LDOptions as LDOptionsBase,
5+
ManualModeSwitching,
46
OptionMessages,
57
TypeValidator,
68
TypeValidators,
@@ -10,10 +12,39 @@ import { LDPlugin } from './LDPlugin';
1012

1113
const DEFAULT_FLUSH_INTERVAL_SECONDS = 2;
1214

15+
/**
16+
* Data system options for the browser SDK.
17+
*
18+
* The browser SDK does not support automatic mode switching (lifecycle or
19+
* network-based). Use `false` (the default) to disable automatic switching,
20+
* or {@link ManualModeSwitching} to specify an explicit initial connection mode.
21+
*
22+
* This interface is not stable, and not subject to any backwards compatibility
23+
* guarantees or semantic versioning. It is in early access. If you want access
24+
* to this feature please join the EAP.
25+
* https://launchdarkly.com/docs/sdk/features/data-saving-mode
26+
*/
27+
export interface BrowserDataSystemOptions
28+
extends Omit<LDClientDataSystemOptions, 'automaticModeSwitching'> {
29+
automaticModeSwitching?: false | ManualModeSwitching;
30+
}
31+
1332
/**
1433
* Initialization options for the LaunchDarkly browser SDK.
1534
*/
1635
export interface BrowserOptions extends Omit<LDOptionsBase, 'initialConnectionMode'> {
36+
/**
37+
* @internal
38+
*
39+
* Configuration for the FDv2 data system. When present, the SDK uses
40+
* the FDv2 protocol for flag delivery instead of the default FDv1
41+
* protocol.
42+
*
43+
* The browser SDK restricts `automaticModeSwitching` to `false` or
44+
* {@link ManualModeSwitching} only — automatic switching has no effect
45+
* in browser environments.
46+
*/
47+
dataSystem?: BrowserDataSystemOptions;
1748
/**
1849
* Whether the client should make a request to LaunchDarkly for Experimentation metrics (goals).
1950
*

packages/shared/sdk-client/src/DataManager.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
subsystem,
1111
} from '@launchdarkly/js-sdk-common';
1212

13+
import type FDv2ConnectionMode from './api/datasource/FDv2ConnectionMode';
1314
import { LDIdentifyOptions } from './api/LDIdentifyOptions';
1415
import { Configuration } from './configuration/Configuration';
1516
import {
@@ -73,6 +74,15 @@ export interface DataManager {
7374
*/
7475
setAutomaticStreamingState?(streaming: boolean): void;
7576

77+
/**
78+
* Set an explicit connection mode override. When set, only this mode is
79+
* used, bypassing all automatic behavior. Pass undefined to clear the
80+
* override.
81+
*
82+
* Optional — only FDv2 data managers implement this.
83+
*/
84+
setConnectionMode?(mode?: FDv2ConnectionMode): void;
85+
7686
/**
7787
* Set a callback to flush pending analytics events. Called immediately
7888
* (not debounced) when the lifecycle transitions to background.

0 commit comments

Comments
 (0)