diff --git a/.changeset/small-panthers-rule.md b/.changeset/small-panthers-rule.md new file mode 100644 index 00000000..4b64e277 --- /dev/null +++ b/.changeset/small-panthers-rule.md @@ -0,0 +1,5 @@ +--- +'@flags-sdk/statsig': patch +--- + +If using an Edge Config adapter, reduce minimum sync delay for config specs from 5000ms->1000ms diff --git a/packages/adapter-statsig/src/edge-runtime-hooks.ts b/packages/adapter-statsig/src/edge-runtime-hooks.ts index 645da39d..0efa0e49 100644 --- a/packages/adapter-statsig/src/edge-runtime-hooks.ts +++ b/packages/adapter-statsig/src/edge-runtime-hooks.ts @@ -38,7 +38,9 @@ export async function createEdgeConfigDataAdapter(options: { * Statsig syncs config specs outside of the request context, * so we will support it in triggering config spec synchronization in this case. */ -export const createSyncingHandler = (): null | (() => void) => { +export const createSyncingHandler = ( + minSyncDelayMs: number, +): null | (() => void) => { // Syncing both in Edge Runtime and Node.js for now, as the sync is otherwise // not working during local development. // @@ -50,17 +52,15 @@ export const createSyncingHandler = (): null | (() => void) => { // - the broken syncing due to issues in Date.now in Edge Runtime would be irrelevant // // if (typeof EdgeRuntime === 'undefined') return null; - - const timerInterval = 5_000; let isSyncingConfigSpecs = false; - let nextConfigSpecSyncTime = Date.now() + timerInterval; + let nextConfigSpecSyncTime = Date.now() + minSyncDelayMs; return (): void => { if (Date.now() >= nextConfigSpecSyncTime && !isSyncingConfigSpecs) { try { isSyncingConfigSpecs = true; const sync = Statsig.syncConfigSpecs().finally(() => { isSyncingConfigSpecs = false; - nextConfigSpecSyncTime = Date.now() + timerInterval; + nextConfigSpecSyncTime = Date.now() + minSyncDelayMs; }); import('@vercel/functions').then(({ waitUntil }) => { waitUntil(sync); diff --git a/packages/adapter-statsig/src/index.ts b/packages/adapter-statsig/src/index.ts index 0150110b..be432fec 100644 --- a/packages/adapter-statsig/src/index.ts +++ b/packages/adapter-statsig/src/index.ts @@ -100,7 +100,8 @@ export function createStatsigAdapter(options: { return user != null && typeof user === 'object'; }; - const syncHandler = createSyncingHandler(); + const minSyncDelayMs = options.edgeConfig ? 1_000 : 5_000; + const syncHandler = createSyncingHandler(minSyncDelayMs); async function predecide(user?: StatsigUser): Promise { await initialize();