Skip to content

Commit

Permalink
Implement overrides store functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
yfrancis committed Dec 16, 2024
1 parent 3db2205 commit 78ed0d9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/configuration-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
MemoryOnlyConfigurationStore,
MemoryStore,
PrecomputedFlag,
Variation,

Check failure on line 8 in src/configuration-factory.ts

View workflow job for this annotation

GitHub Actions / typecheck (18)

'"@eppo/js-client-sdk-common"' has no exported member named 'Variation'. Did you mean 'validation'?

Check failure on line 8 in src/configuration-factory.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (18)

'"@eppo/js-client-sdk-common"' has no exported member named 'Variation'. Did you mean 'validation'?

Check failure on line 8 in src/configuration-factory.ts

View workflow job for this annotation

GitHub Actions / typecheck (20)

'"@eppo/js-client-sdk-common"' has no exported member named 'Variation'. Did you mean 'validation'?

Check failure on line 8 in src/configuration-factory.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (20)

'"@eppo/js-client-sdk-common"' has no exported member named 'Variation'. Did you mean 'validation'?

Check failure on line 8 in src/configuration-factory.ts

View workflow job for this annotation

GitHub Actions / typecheck (22)

'"@eppo/js-client-sdk-common"' has no exported member named 'Variation'. Did you mean 'validation'?

Check failure on line 8 in src/configuration-factory.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (22)

'"@eppo/js-client-sdk-common"' has no exported member named 'Variation'. Did you mean 'validation'?

Check failure on line 8 in src/configuration-factory.ts

View workflow job for this annotation

GitHub Actions / typecheck (23)

'"@eppo/js-client-sdk-common"' has no exported member named 'Variation'. Did you mean 'validation'?

Check failure on line 8 in src/configuration-factory.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (23)

'"@eppo/js-client-sdk-common"' has no exported member named 'Variation'. Did you mean 'validation'?
} from '@eppo/js-client-sdk-common';

import ChromeStorageAsyncMap from './cache/chrome-storage-async-map';
Expand All @@ -14,6 +15,7 @@ import {
ServingStoreUpdateStrategy,
} from './isolatable-hybrid.store';
import { LocalStorageEngine } from './local-storage-engine';
import { OVERRIDES_SUFFIX_KEY } from './storage-key-constants';
import { StringValuedAsyncStore } from './string-valued.store';

export function precomputedFlagsStorageFactory(): IConfigurationStore<PrecomputedFlag> {
Expand Down Expand Up @@ -79,6 +81,33 @@ export function configurationStorageFactory(
return new MemoryOnlyConfigurationStore();
}

export function overridesStorageFactory(
{
hasWindowLocalStorage = false,
forceMemoryOnly = false,
}: {
hasWindowLocalStorage?: boolean;
forceMemoryOnly?: boolean;
},
{
windowLocalStorage,
}: {
windowLocalStorage?: Storage;
} = {},
): IConfigurationStore<Variation> {
if (forceMemoryOnly) {
return new MemoryOnlyConfigurationStore();
} else if (hasWindowLocalStorage && windowLocalStorage) {
const localStorageEngine = new LocalStorageEngine(windowLocalStorage, OVERRIDES_SUFFIX_KEY);
return new IsolatableHybridConfigurationStore(
new MemoryStore<Variation>(),
new StringValuedAsyncStore<Variation>(localStorageEngine),
'always',
);
}
return new MemoryOnlyConfigurationStore();
}

export function hasChromeStorage(): boolean {
return typeof chrome !== 'undefined' && !!chrome.storage;
}
Expand Down
34 changes: 34 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
hasChromeStorage,
hasWindowLocalStorage,
localStorageIfAvailable,
overridesStorageFactory,
} from './configuration-factory';
import BrowserNetworkStatusListener from './events/browser-network-status-listener';
import LocalStorageBackedNamedEventQueue from './events/local-storage-backed-named-event-queue';
Expand Down Expand Up @@ -67,6 +68,16 @@ const flagConfigurationStore = configurationStorageFactory({
forceMemoryOnly: true,
});

// Create the overrides store
const overridesStore = overridesStorageFactory(
{
hasWindowLocalStorage: hasWindowLocalStorage(),
},
{
windowLocalStorage: localStorageIfAvailable(),
},
);

// Instantiate the precomputed flgas store with memory-only implementation.
const memoryOnlyPrecomputedFlagsStore = precomputedFlagsStorageFactory();

Expand All @@ -81,6 +92,7 @@ export class EppoJSClient extends EppoClient {
public static instance = new EppoJSClient({
flagConfigurationStore,
isObfuscated: true,
overridesStore,

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (18)

Argument of type '{ flagConfigurationStore: IConfigurationStore<Flag>; isObfuscated: true; overridesStore: IConfigurationStore<Variation>; }' is not assignable to parameter of type '{ eventDispatcher?: EventDispatcher | undefined; flagConfigurationStore: IConfigurationStore<Flag | ObfuscatedFlag>; banditVariationConfigurationStore?: IConfigurationStore<...> | undefined; banditModelConfigurationStore?: IConfigurationStore<...> | undefined; configurationRequestParameters?: FlagConfigurationReques...'.

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (18)

Argument of type '{ flagConfigurationStore: IConfigurationStore<Flag>; isObfuscated: true; overridesStore: IConfigurationStore<Variation>; }' is not assignable to parameter of type '{ eventDispatcher?: EventDispatcher | undefined; flagConfigurationStore: IConfigurationStore<Flag | ObfuscatedFlag>; banditVariationConfigurationStore?: IConfigurationStore<...> | undefined; banditModelConfigurationStore?: IConfigurationStore<...> | undefined; configurationRequestParameters?: FlagConfigurationReques...'.

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (20)

Argument of type '{ flagConfigurationStore: IConfigurationStore<Flag>; isObfuscated: true; overridesStore: IConfigurationStore<Variation>; }' is not assignable to parameter of type '{ eventDispatcher?: EventDispatcher | undefined; flagConfigurationStore: IConfigurationStore<Flag | ObfuscatedFlag>; banditVariationConfigurationStore?: IConfigurationStore<...> | undefined; banditModelConfigurationStore?: IConfigurationStore<...> | undefined; configurationRequestParameters?: FlagConfigurationReques...'.

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (20)

Argument of type '{ flagConfigurationStore: IConfigurationStore<Flag>; isObfuscated: true; overridesStore: IConfigurationStore<Variation>; }' is not assignable to parameter of type '{ eventDispatcher?: EventDispatcher | undefined; flagConfigurationStore: IConfigurationStore<Flag | ObfuscatedFlag>; banditVariationConfigurationStore?: IConfigurationStore<...> | undefined; banditModelConfigurationStore?: IConfigurationStore<...> | undefined; configurationRequestParameters?: FlagConfigurationReques...'.

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (22)

Argument of type '{ flagConfigurationStore: IConfigurationStore<Flag>; isObfuscated: true; overridesStore: IConfigurationStore<Variation>; }' is not assignable to parameter of type '{ eventDispatcher?: EventDispatcher | undefined; flagConfigurationStore: IConfigurationStore<Flag | ObfuscatedFlag>; banditVariationConfigurationStore?: IConfigurationStore<...> | undefined; banditModelConfigurationStore?: IConfigurationStore<...> | undefined; configurationRequestParameters?: FlagConfigurationReques...'.

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (22)

Argument of type '{ flagConfigurationStore: IConfigurationStore<Flag>; isObfuscated: true; overridesStore: IConfigurationStore<Variation>; }' is not assignable to parameter of type '{ eventDispatcher?: EventDispatcher | undefined; flagConfigurationStore: IConfigurationStore<Flag | ObfuscatedFlag>; banditVariationConfigurationStore?: IConfigurationStore<...> | undefined; banditModelConfigurationStore?: IConfigurationStore<...> | undefined; configurationRequestParameters?: FlagConfigurationReques...'.

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (23)

Argument of type '{ flagConfigurationStore: IConfigurationStore<Flag>; isObfuscated: true; overridesStore: IConfigurationStore<Variation>; }' is not assignable to parameter of type '{ eventDispatcher?: EventDispatcher | undefined; flagConfigurationStore: IConfigurationStore<Flag | ObfuscatedFlag>; banditVariationConfigurationStore?: IConfigurationStore<...> | undefined; banditModelConfigurationStore?: IConfigurationStore<...> | undefined; configurationRequestParameters?: FlagConfigurationReques...'.

Check failure on line 95 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (23)

Argument of type '{ flagConfigurationStore: IConfigurationStore<Flag>; isObfuscated: true; overridesStore: IConfigurationStore<Variation>; }' is not assignable to parameter of type '{ eventDispatcher?: EventDispatcher | undefined; flagConfigurationStore: IConfigurationStore<Flag | ObfuscatedFlag>; banditVariationConfigurationStore?: IConfigurationStore<...> | undefined; banditModelConfigurationStore?: IConfigurationStore<...> | undefined; configurationRequestParameters?: FlagConfigurationReques...'.
});
public static initialized = false;

Expand Down Expand Up @@ -278,6 +290,17 @@ export function offlineInit(config: IClientConfigSync): EppoClient {
);
EppoJSClient.instance.setFlagConfigurationStore(memoryOnlyConfigurationStore);

// Create and set up the overrides store
const overridesStore = overridesStorageFactory(
{
hasWindowLocalStorage: hasWindowLocalStorage(),
},
{
windowLocalStorage: localStorageIfAvailable(),
},
);
EppoJSClient.instance.setOverridesStore(overridesStore);

Check failure on line 302 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (18)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 302 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (18)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 302 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (20)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 302 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (20)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 302 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (22)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 302 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (22)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 302 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (23)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 302 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (23)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

// Allow the caller to override the default obfuscated mode, which is false
// since the purpose of this method is to bootstrap the SDK from an external source,
// which is likely a server that has not-obfuscated flag values.
Expand Down Expand Up @@ -380,6 +403,17 @@ export async function init(config: IClientConfig): Promise<EppoClient> {
);
instance.setFlagConfigurationStore(configurationStore);

const overridesStore = overridesStorageFactory(
{
hasWindowLocalStorage: hasWindowLocalStorage(),
},
{
windowLocalStorage: localStorageIfAvailable(),
},
);
instance.setOverridesStore(overridesStore);

Check failure on line 414 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (18)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 414 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (18)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 414 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (20)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 414 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (20)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 414 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (22)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 414 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (22)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 414 in src/index.ts

View workflow job for this annotation

GitHub Actions / typecheck (23)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.

Check failure on line 414 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint-test-sdk (23)

Property 'setOverridesStore' does not exist on type 'EppoJSClient'.
overridesStore.init();

// instantiate and init assignment cache
const assignmentCache = assignmentCacheFactory({
chromeStorage: chromeStorageIfAvailable(),
Expand Down
1 change: 1 addition & 0 deletions src/storage-key-constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const CONFIGURATION_KEY = 'eppo-configuration';
export const META_KEY = 'eppo-configuration-meta';
export const OVERRIDES_SUFFIX_KEY = 'overrides';

0 comments on commit 78ed0d9

Please sign in to comment.