Skip to content

fix: Handle default flush interval for browser SDK. #822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions packages/sdk/browser/__tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { jest } from '@jest/globals';

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

import validateOptions, { filterToBaseOptions } from '../src/options';
import validateBrowserOptions, { filterToBaseOptionsWithDefaults } from '../src/options';

let logger: LDLogger;

Expand All @@ -16,7 +16,7 @@ beforeEach(() => {
});

it('logs no warnings when all configuration is valid', () => {
validateOptions(
validateBrowserOptions(
{
fetchGoals: true,
eventUrlTransformer: (url: string) => url,
Expand All @@ -31,7 +31,7 @@ it('logs no warnings when all configuration is valid', () => {
});

it('warns for invalid configuration', () => {
validateOptions(
validateBrowserOptions(
{
// @ts-ignore
fetchGoals: 'yes',
Expand All @@ -50,8 +50,8 @@ it('warns for invalid configuration', () => {
);
});

it('applies default options', () => {
const opts = validateOptions({}, logger);
it('applies default browser-specific options', () => {
const opts = validateBrowserOptions({}, logger);

expect(opts.fetchGoals).toBe(true);
expect(opts.eventUrlTransformer).toBeDefined();
Expand All @@ -69,9 +69,24 @@ it('filters to base options', () => {
eventUrlTransformer: (url: string) => url,
};

const baseOpts = filterToBaseOptions(opts);
const baseOpts = filterToBaseOptionsWithDefaults(opts);
expect(baseOpts.debug).toBe(false);
expect(Object.keys(baseOpts).length).toEqual(1);
expect(Object.keys(baseOpts).length).toEqual(2);
expect(baseOpts).not.toHaveProperty('fetchGoals');
expect(baseOpts).not.toHaveProperty('eventUrlTransformer');
expect(baseOpts.flushInterval).toEqual(2);
});

it('applies default overrides to common config flushInterval', () => {
const opts = {};
const result = filterToBaseOptionsWithDefaults(opts);
expect(result.flushInterval).toEqual(2);
});

it('does not override common config flushInterval if it is set', () => {
const opts = {
flushInterval: 15,
};
const result = filterToBaseOptionsWithDefaults(opts);
expect(result.flushInterval).toEqual(15);
});
9 changes: 6 additions & 3 deletions packages/sdk/browser/src/BrowserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { BrowserIdentifyOptions as LDIdentifyOptions } from './BrowserIdentifyOp
import { registerStateDetection } from './BrowserStateDetector';
import GoalManager from './goals/GoalManager';
import { Goal, isClick } from './goals/Goals';
import validateOptions, { BrowserOptions, filterToBaseOptions } from './options';
import validateBrowserOptions, { BrowserOptions, filterToBaseOptionsWithDefaults } from './options';
import BrowserPlatform from './platform/BrowserPlatform';

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

const platform = overridePlatform ?? new BrowserPlatform(logger);
const validatedBrowserOptions = validateOptions(options, logger);
// Only the browser-specific options are in validatedBrowserOptions.
const validatedBrowserOptions = validateBrowserOptions(options, logger);
// The base options are in baseOptionsWithDefaults.
const baseOptionsWithDefaults = filterToBaseOptionsWithDefaults({ ...options, logger });
const { eventUrlTransformer } = validatedBrowserOptions;
super(
clientSideId,
autoEnvAttributes,
platform,
filterToBaseOptions({ ...options, logger }),
baseOptionsWithDefaults,
(
flagManager: FlagManager,
configuration: Configuration,
Expand Down
21 changes: 12 additions & 9 deletions packages/sdk/browser/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ const validators: { [Property in keyof BrowserOptions]: TypeValidator | undefine
streaming: TypeValidators.Boolean,
};

export function filterToBaseOptions(opts: BrowserOptions): LDOptionsBase {
const baseOptions: LDOptionsBase = { ...opts };
function withBrowserDefaults(opts: BrowserOptions): BrowserOptions {
const output = { ...opts };
output.flushInterval ??= DEFAULT_FLUSH_INTERVAL_SECONDS;
return output;
}

export function filterToBaseOptionsWithDefaults(opts: BrowserOptions): LDOptionsBase {
const baseOptions: LDOptionsBase = withBrowserDefaults(opts);

// Remove any browser specific configuration keys so we don't get warnings from
// the base implementation for unknown configuration.
Expand All @@ -84,14 +90,11 @@ export function filterToBaseOptions(opts: BrowserOptions): LDOptionsBase {
return baseOptions;
}

function applyBrowserDefaults(opts: BrowserOptions) {
// eslint-disable-next-line no-param-reassign
opts.flushInterval ??= DEFAULT_FLUSH_INTERVAL_SECONDS;
}

export default function validateOptions(opts: BrowserOptions, logger: LDLogger): ValidatedOptions {
export default function validateBrowserOptions(
opts: BrowserOptions,
logger: LDLogger,
): ValidatedOptions {
const output: ValidatedOptions = { ...optDefaults };
applyBrowserDefaults(output);

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