Skip to content

Commit a637e8d

Browse files
committed
fix: Handle default flush interval for browser SDK.
1 parent 2c09c3b commit a637e8d

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

packages/sdk/browser/__tests__/options.test.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { jest } from '@jest/globals';
22

33
import { LDLogger } from '@launchdarkly/js-client-sdk-common';
44

5-
import validateOptions, { filterToBaseOptions } from '../src/options';
5+
import validateBrowserOptions, {
6+
filterToBaseOptionsWithDefaults,
7+
withBrowserDefaults,
8+
} from '../src/options';
69

710
let logger: LDLogger;
811

@@ -16,7 +19,7 @@ beforeEach(() => {
1619
});
1720

1821
it('logs no warnings when all configuration is valid', () => {
19-
validateOptions(
22+
validateBrowserOptions(
2023
{
2124
fetchGoals: true,
2225
eventUrlTransformer: (url: string) => url,
@@ -31,7 +34,7 @@ it('logs no warnings when all configuration is valid', () => {
3134
});
3235

3336
it('warns for invalid configuration', () => {
34-
validateOptions(
37+
validateBrowserOptions(
3538
{
3639
// @ts-ignore
3740
fetchGoals: 'yes',
@@ -50,8 +53,8 @@ it('warns for invalid configuration', () => {
5053
);
5154
});
5255

53-
it('applies default options', () => {
54-
const opts = validateOptions({}, logger);
56+
it('applies default browser-specific options', () => {
57+
const opts = validateBrowserOptions({}, logger);
5558

5659
expect(opts.fetchGoals).toBe(true);
5760
expect(opts.eventUrlTransformer).toBeDefined();
@@ -69,9 +72,29 @@ it('filters to base options', () => {
6972
eventUrlTransformer: (url: string) => url,
7073
};
7174

72-
const baseOpts = filterToBaseOptions(opts);
75+
const baseOpts = filterToBaseOptionsWithDefaults(opts);
7376
expect(baseOpts.debug).toBe(false);
7477
expect(Object.keys(baseOpts).length).toEqual(1);
7578
expect(baseOpts).not.toHaveProperty('fetchGoals');
7679
expect(baseOpts).not.toHaveProperty('eventUrlTransformer');
7780
});
81+
82+
it('applies default overrides to common config flushInterval', () => {
83+
const opts = {};
84+
const result = withBrowserDefaults(opts);
85+
expect(result.flushInterval).toEqual(2);
86+
});
87+
88+
it('does not override common config flushInterval if it is set', () => {
89+
const opts = {
90+
flushInterval: 15,
91+
};
92+
const result = withBrowserDefaults(opts);
93+
expect(result.flushInterval).toEqual(15);
94+
});
95+
96+
it('includes overridden defaults after filtering to base options', () => {
97+
const opts = {};
98+
const result = filterToBaseOptionsWithDefaults(withBrowserDefaults(opts));
99+
expect(result.flushInterval).toEqual(2);
100+
});

packages/sdk/browser/src/BrowserClient.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { BrowserIdentifyOptions as LDIdentifyOptions } from './BrowserIdentifyOp
2121
import { registerStateDetection } from './BrowserStateDetector';
2222
import GoalManager from './goals/GoalManager';
2323
import { Goal, isClick } from './goals/Goals';
24-
import validateOptions, { BrowserOptions, filterToBaseOptions } from './options';
24+
import validateBrowserOptions, { BrowserOptions, filterToBaseOptionsWithDefaults } from './options';
2525
import BrowserPlatform from './platform/BrowserPlatform';
2626

2727
/**
@@ -116,13 +116,16 @@ export class BrowserClient extends LDClientImpl implements LDClient {
116116
const baseUrl = options.baseUri ?? 'https://clientsdk.launchdarkly.com';
117117

118118
const platform = overridePlatform ?? new BrowserPlatform(logger);
119-
const validatedBrowserOptions = validateOptions(options, logger);
119+
// Only the browser-specific options are in validatedBrowserOptions.
120+
const validatedBrowserOptions = validateBrowserOptions(options, logger);
121+
// The base options are in baseOptionsWithDefaults.
122+
const baseOptionsWithDefaults = filterToBaseOptionsWithDefaults({ ...options, logger });
120123
const { eventUrlTransformer } = validatedBrowserOptions;
121124
super(
122125
clientSideId,
123126
autoEnvAttributes,
124127
platform,
125-
filterToBaseOptions({ ...options, logger }),
128+
baseOptionsWithDefaults,
126129
(
127130
flagManager: FlagManager,
128131
configuration: Configuration,

packages/sdk/browser/src/options.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,28 @@ const validators: { [Property in keyof BrowserOptions]: TypeValidator | undefine
7373
streaming: TypeValidators.Boolean,
7474
};
7575

76-
export function filterToBaseOptions(opts: BrowserOptions): LDOptionsBase {
77-
const baseOptions: LDOptionsBase = { ...opts };
76+
function withBrowserDefaults(opts: BrowserOptions): BrowserOptions {
77+
const output = { ...opts };
78+
output.flushInterval ??= DEFAULT_FLUSH_INTERVAL_SECONDS;
79+
return output;
80+
}
81+
82+
export function filterToBaseOptionsWithDefaults(opts: BrowserOptions): LDOptionsBase {
83+
const baseOptionsWithDefaults: LDOptionsBase = withBrowserDefaults(opts);
7884

7985
// Remove any browser specific configuration keys so we don't get warnings from
8086
// the base implementation for unknown configuration.
8187
Object.keys(optDefaults).forEach((key) => {
82-
delete (baseOptions as any)[key];
88+
delete (baseOptionsWithDefaults as any)[key];
8389
});
84-
return baseOptions;
85-
}
86-
87-
function applyBrowserDefaults(opts: BrowserOptions) {
88-
// eslint-disable-next-line no-param-reassign
89-
opts.flushInterval ??= DEFAULT_FLUSH_INTERVAL_SECONDS;
90+
return baseOptionsWithDefaults;
9091
}
9192

92-
export default function validateOptions(opts: BrowserOptions, logger: LDLogger): ValidatedOptions {
93+
export default function validateBrowserOptions(
94+
opts: BrowserOptions,
95+
logger: LDLogger,
96+
): ValidatedOptions {
9397
const output: ValidatedOptions = { ...optDefaults };
94-
applyBrowserDefaults(output);
9598

9699
Object.entries(validators).forEach((entry) => {
97100
const [key, validator] = entry as [keyof BrowserOptions, TypeValidator];

0 commit comments

Comments
 (0)