Skip to content

Commit c6a9c83

Browse files
committed
chore: removing setInitialContext
1 parent a2cddfd commit c6a9c83

5 files changed

Lines changed: 36 additions & 39 deletions

File tree

packages/sdk/browser/src/BrowserClient.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class BrowserClientImpl extends LDClientImpl {
4545

4646
constructor(
4747
clientSideId: string,
48+
initialContext: LDContext,
4849
autoEnvAttributes: AutoEnvAttributes,
4950
options: BrowserOptions = {},
5051
overridePlatform?: Platform,
@@ -150,6 +151,7 @@ class BrowserClientImpl extends LDClientImpl {
150151
internal.safeGetHooks(logger, environmentMetadata, validatedBrowserOptions.plugins),
151152
credentialType: 'clientSideId',
152153
requiresStart: true,
154+
initialContext,
153155
});
154156

155157
this.setEventSendingEnabled(true, false);
@@ -291,8 +293,13 @@ export function makeClient(
291293
options: BrowserOptions = {},
292294
overridePlatform?: Platform,
293295
): LDClient {
294-
const impl = new BrowserClientImpl(clientSideId, autoEnvAttributes, options, overridePlatform);
295-
impl.setInitialContext(initialContext);
296+
const impl = new BrowserClientImpl(
297+
clientSideId,
298+
initialContext,
299+
autoEnvAttributes,
300+
options,
301+
overridePlatform,
302+
);
296303

297304
// Return a PIMPL style implementation. This decouples the interface from the interface of the implementation.
298305
// In the future we should consider updating the common SDK code to not use inheritance and instead compose

packages/sdk/electron/src/ElectronClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export class ElectronClient extends LDClientImpl {
8787
internal.safeGetHooks(logger, _environmentMetadata, validatedElectronOptions.plugins),
8888
credentialType: useClientSideId ? 'clientSideId' : 'mobileKey',
8989
requiresStart: true,
90+
initialContext,
9091
};
9192

9293
const platform = new ElectronPlatform(logger, options);
@@ -119,7 +120,6 @@ export class ElectronClient extends LDClientImpl {
119120
internalOptions,
120121
);
121122

122-
this.setInitialContext(initialContext);
123123
this._plugins = validatedElectronOptions.plugins;
124124
this.setEventSendingEnabled(!this.isOffline(), false);
125125

packages/shared/sdk-client/__tests__/LDClientImpl.start.test.ts

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function setupClient(
2222
requiresStart?: boolean;
2323
disableNetwork?: boolean;
2424
sendEvents?: boolean;
25+
initialContext?: LDContext;
2526
},
2627
) {
2728
const logger = options?.logger ?? {
@@ -46,6 +47,7 @@ function setupClient(
4647
getImplementationHooks: () => [],
4748
credentialType: 'clientSideId',
4849
requiresStart: options?.requiresStart ?? true,
50+
initialContext: options?.initialContext,
4951
},
5052
);
5153

@@ -97,8 +99,7 @@ describe('LDClientImpl.start()', () => {
9799

98100
it('returns the same promise when called multiple times', async () => {
99101
const mockPlatform = setupStreamingPlatform();
100-
const { ldc } = setupClient(mockPlatform);
101-
ldc.setInitialContext(context);
102+
const { ldc } = setupClient(mockPlatform, { initialContext: context });
102103

103104
const promise1 = ldc.start();
104105
const promise2 = ldc.start();
@@ -115,8 +116,7 @@ describe('LDClientImpl.start()', () => {
115116

116117
it('returns cached result after initialization completes', async () => {
117118
const mockPlatform = setupStreamingPlatform();
118-
const { ldc } = setupClient(mockPlatform);
119-
ldc.setInitialContext(context);
119+
const { ldc } = setupClient(mockPlatform, { initialContext: context });
120120

121121
const result1 = await ldc.start();
122122
expect(result1.status).toBe('complete');
@@ -127,17 +127,15 @@ describe('LDClientImpl.start()', () => {
127127

128128
it('resolves with complete status on successful identify', async () => {
129129
const mockPlatform = setupStreamingPlatform();
130-
const { ldc } = setupClient(mockPlatform);
131-
ldc.setInitialContext(context);
130+
const { ldc } = setupClient(mockPlatform, { initialContext: context });
132131

133132
const result = await ldc.start();
134133
expect(result.status).toBe('complete');
135134
});
136135

137136
it('sets the active context after start completes', async () => {
138137
const mockPlatform = setupStreamingPlatform();
139-
const { ldc } = setupClient(mockPlatform);
140-
ldc.setInitialContext(context);
138+
const { ldc } = setupClient(mockPlatform, { initialContext: context });
141139

142140
expect(ldc.getContext()).toBeUndefined();
143141
await ldc.start();
@@ -147,8 +145,7 @@ describe('LDClientImpl.start()', () => {
147145
describe('bootstrap data', () => {
148146
it('presets flags from bootstrap in identifyOptions', async () => {
149147
const mockPlatform = createBasicPlatform();
150-
const { ldc } = setupClient(mockPlatform, { disableNetwork: true });
151-
ldc.setInitialContext(context);
148+
const { ldc } = setupClient(mockPlatform, { disableNetwork: true, initialContext: context });
152149

153150
await ldc.start({
154151
identifyOptions: { bootstrap: goodBootstrapData },
@@ -162,8 +159,7 @@ describe('LDClientImpl.start()', () => {
162159

163160
it('presets flags from top-level bootstrap option', async () => {
164161
const mockPlatform = createBasicPlatform();
165-
const { ldc } = setupClient(mockPlatform, { disableNetwork: true });
166-
ldc.setInitialContext(context);
162+
const { ldc } = setupClient(mockPlatform, { disableNetwork: true, initialContext: context });
167163

168164
await ldc.start({ bootstrap: goodBootstrapData });
169165

@@ -174,8 +170,7 @@ describe('LDClientImpl.start()', () => {
174170

175171
it('makes flags available synchronously before identify completes', async () => {
176172
const mockPlatform = createBasicPlatform();
177-
const { ldc } = setupClient(mockPlatform, { disableNetwork: true });
178-
ldc.setInitialContext(context);
173+
const { ldc } = setupClient(mockPlatform, { disableNetwork: true, initialContext: context });
179174

180175
const startPromise = ldc.start({
181176
identifyOptions: { bootstrap: goodBootstrapDataWithReasons },
@@ -190,8 +185,7 @@ describe('LDClientImpl.start()', () => {
190185

191186
it('supports bootstrap data with evaluation reasons', async () => {
192187
const mockPlatform = createBasicPlatform();
193-
const { ldc } = setupClient(mockPlatform, { disableNetwork: true });
194-
ldc.setInitialContext(context);
188+
const { ldc } = setupClient(mockPlatform, { disableNetwork: true, initialContext: context });
195189

196190
await ldc.start({
197191
identifyOptions: { bootstrap: goodBootstrapDataWithReasons },
@@ -206,8 +200,7 @@ describe('LDClientImpl.start()', () => {
206200

207201
it('prefers identifyOptions.bootstrap over top-level bootstrap', async () => {
208202
const mockPlatform = createBasicPlatform();
209-
const { ldc } = setupClient(mockPlatform, { disableNetwork: true });
210-
ldc.setInitialContext(context);
203+
const { ldc } = setupClient(mockPlatform, { disableNetwork: true, initialContext: context });
211204

212205
const differentBootstrap = {
213206
'other-flag': true,
@@ -229,8 +222,7 @@ describe('LDClientImpl.start()', () => {
229222
const readFlagsFromBootstrapSpy = jest.spyOn(bootstrapModule, 'readFlagsFromBootstrap');
230223

231224
const mockPlatform = createBasicPlatform();
232-
const { ldc } = setupClient(mockPlatform, { disableNetwork: true });
233-
ldc.setInitialContext(context);
225+
const { ldc } = setupClient(mockPlatform, { disableNetwork: true, initialContext: context });
234226

235227
await ldc.start({
236228
identifyOptions: { bootstrap: goodBootstrapDataWithReasons },
@@ -249,8 +241,10 @@ describe('LDClientImpl.start()', () => {
249241
describe('requiresStart guard', () => {
250242
it('blocks identify before start when requiresStart is true', async () => {
251243
const mockPlatform = setupStreamingPlatform();
252-
const { ldc, logger } = setupClient(mockPlatform, { requiresStart: true });
253-
ldc.setInitialContext(context);
244+
const { ldc, logger } = setupClient(mockPlatform, {
245+
requiresStart: true,
246+
initialContext: context,
247+
});
254248

255249
const result = await ldc.identifyResult({ kind: 'user', key: 'other-user' });
256250

@@ -265,8 +259,7 @@ describe('LDClientImpl.start()', () => {
265259

266260
it('allows identify after start when requiresStart is true', async () => {
267261
const mockPlatform = setupStreamingPlatform();
268-
const { ldc } = setupClient(mockPlatform, { requiresStart: true });
269-
ldc.setInitialContext(context);
262+
const { ldc } = setupClient(mockPlatform, { requiresStart: true, initialContext: context });
270263

271264
await ldc.start();
272265

@@ -284,8 +277,7 @@ describe('LDClientImpl.start()', () => {
284277

285278
it('defaults sheddable to true for post-start identifies when requiresStart is true', async () => {
286279
const mockPlatform = setupStreamingPlatform();
287-
const { ldc } = setupClient(mockPlatform, { requiresStart: true });
288-
ldc.setInitialContext(context);
280+
const { ldc } = setupClient(mockPlatform, { requiresStart: true, initialContext: context });
289281

290282
const startPromise = ldc.start();
291283
const promise1 = ldc.identifyResult({ kind: 'user', key: 'user-1' });
@@ -324,8 +316,7 @@ describe('LDClientImpl.start()', () => {
324316
describe('waitForInitialization integration', () => {
325317
it('resolves waitForInitialization when start completes', async () => {
326318
const mockPlatform = setupStreamingPlatform();
327-
const { ldc } = setupClient(mockPlatform);
328-
ldc.setInitialContext(context);
319+
const { ldc } = setupClient(mockPlatform, { initialContext: context });
329320

330321
const waitPromise = ldc.waitForInitialization({ timeout: 10 });
331322
const startPromise = ldc.start();

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export default class LDClientImpl implements LDClient, LDClientIdentifyResult {
129129
this._config = new ConfigurationImpl(options, internalOptions);
130130
this.logger = this._config.logger;
131131
this._requiresStart = internalOptions?.requiresStart ?? false;
132+
this.initialContext = internalOptions?.initialContext;
132133

133134
this._baseHeaders = defaultHeaders(
134135
this.sdkKey,
@@ -275,14 +276,6 @@ export default class LDClientImpl implements LDClient, LDClientIdentifyResult {
275276
this._flagManager.presetFlags(newFlags);
276277
}
277278

278-
/**
279-
* Sets the initial context for the client. This must be called before `start()`.
280-
* @param context The initial context.
281-
*/
282-
setInitialContext(context: LDContext): void {
283-
this.initialContext = context;
284-
}
285-
286279
/**
287280
* Starts the client and returns a promise that resolves to the initialization result.
288281
*

packages/shared/sdk-client/src/configuration/Configuration.ts

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

1212
import { Hook, type LDOptions } from '../api';
13+
import { LDContext } from '../api/LDContext';
1314
import { LDInspection } from '../api/LDInspection';
1415
import type {
1516
InternalDataSystemOptions,
@@ -32,6 +33,11 @@ export interface LDClientInternalOptions extends internal.LDInternalOptions {
3233
* Set this value to `true` to use the new initialization pattern.
3334
*/
3435
requiresStart?: boolean;
36+
37+
/**
38+
* The initial context to use when starting the client.
39+
*/
40+
initialContext?: LDContext;
3541
}
3642

3743
export interface Configuration {

0 commit comments

Comments
 (0)