From a939b2afa7a859cb4e809ca4f63870225a6dc6f9 Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Wed, 8 Nov 2023 15:35:25 -0800 Subject: [PATCH] fix: fetch on start by default --- __tests__/client.test.ts | 4 ++-- src/experimentClient.ts | 33 ++++----------------------------- src/types/config.ts | 7 +++---- 3 files changed, 9 insertions(+), 35 deletions(-) diff --git a/__tests__/client.test.ts b/__tests__/client.test.ts index dc90279..ee2b7b7 100644 --- a/__tests__/client.test.ts +++ b/__tests__/client.test.ts @@ -922,7 +922,7 @@ describe('start', () => { await client.start(); expect(fetchSpy).toBeCalledTimes(1); }, 10000); - test('with local evaluation only, does not call fetch', async () => { + test('with local evaluation only, calls fetch', async () => { const client = new ExperimentClient(API_KEY, {}); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -931,7 +931,7 @@ describe('start', () => { }; const fetchSpy = jest.spyOn(client, 'fetch'); await client.start(); - expect(fetchSpy).toBeCalledTimes(0); + expect(fetchSpy).toBeCalledTimes(1); }); test('with local evaluation only, fetchOnStart enabled, calls fetch', async () => { diff --git a/src/experimentClient.ts b/src/experimentClient.ts index 5f3d4e1..1b0681d 100644 --- a/src/experimentClient.ts +++ b/src/experimentClient.ts @@ -34,7 +34,6 @@ import { isLocalEvaluationMode, isNullOrUndefined, isNullUndefinedOrEmpty, - isRemoteEvaluationMode, } from './util'; import { Backoff } from './util/backoff'; import { @@ -149,12 +148,8 @@ export class ExperimentClient implements Client { * local flag configurations have been updated, and the {@link fetch()} * result has been received (if the request was made). * - * This function determines whether to {@link fetch()} based on the result of - * the flag configurations cached locally or received in the initial flag - * configuration response. - * - * To explicitly force this request to fetch or not, set the - * {@link fetchOnStart} configuration option when initializing the SDK. + * To force this function not to fetch variants, set the {@link fetchOnStart} + * configuration option to `false` when initializing the SDK. * * Finally, this function will start polling for flag configurations at a * fixed interval. To disable polling, set the {@link pollOnStart} @@ -173,32 +168,12 @@ export class ExperimentClient implements Client { this.isRunning = true; } this.setUser(user); - // Get flag configurations, and simultaneously check the local cache for - // remote evaluation flags. const flagsReadyPromise = this.doFlags(); - let remoteFlags = - this.config.fetchOnStart ?? - Object.values(this.flags.getAll()).some((flag) => - isRemoteEvaluationMode(flag), - ); - if (remoteFlags) { - // We already have remote flags in our flag cache, so we know we need to - // evaluate remotely even before we've updated our flags. + const fetchOnStart = this.config.fetchOnStart ?? true; + if (fetchOnStart) { await Promise.all([this.fetch(user), flagsReadyPromise]); } else { - // We don't know if remote evaluation is required, await the flag promise, - // and recheck for remote flags. await flagsReadyPromise; - remoteFlags = - this.config.fetchOnStart ?? - Object.values(this.flags.getAll()).some((flag) => - isRemoteEvaluationMode(flag), - ); - if (remoteFlags) { - // We already have remote flags in our flag cache, so we know we need to - // evaluate remotely even before we've updated our flags. - await this.fetch(user); - } } if (this.config.pollOnStart) { this.poller.start(); diff --git a/src/types/config.ts b/src/types/config.ts index e1da363..0a0a074 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -87,8 +87,7 @@ export interface ExperimentConfig { * * - `true`: fetch will always be called on start. * - `false`: fetch will never be called on start. - * - `undefined`: the SDK will determine whether to call fetch based on the - * flags returned in the result. + * - `undefined`: fetch will always be called on start. */ fetchOnStart?: boolean; @@ -145,7 +144,7 @@ export interface ExperimentConfig { | **retryFailedAssignment** | `true` | | **automaticExposureTracking** | `true` | | **pollOnStart** | `true` | - | **fetchOnStart** | `undefined` | + | **fetchOnStart** | `true` | | **automaticFetchOnAmplitudeIdentityChange** | `false` | | **userProvider** | `null` | | **analyticsProvider** | `null` | @@ -167,7 +166,7 @@ export const Defaults: ExperimentConfig = { retryFetchOnFailure: true, automaticExposureTracking: true, pollOnStart: true, - fetchOnStart: undefined, + fetchOnStart: true, automaticFetchOnAmplitudeIdentityChange: false, userProvider: null, exposureTrackingProvider: null,