Skip to content

Commit fd3c3a7

Browse files
Revert client instantiate inside of init method. Create with empty … (#68)
Revert client instantiate inside of `init` method. Create with empty in-memory store. Populate the configuration store in `init`.
1 parent 168c8b8 commit fd3c3a7

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@eppo/js-client-sdk",
3-
"version": "3.0.2-rc.0",
3+
"version": "3.0.2-rc.1",
44
"description": "Eppo SDK for client-side JavaScript applications",
55
"main": "dist/index.js",
66
"files": [
@@ -57,7 +57,7 @@
5757
"webpack-cli": "^4.10.0"
5858
},
5959
"dependencies": {
60-
"@eppo/js-client-sdk-common": "3.0.6",
60+
"@eppo/js-client-sdk-common": "3.0.8",
6161
"md5": "^2.3.0"
6262
}
63-
}
63+
}

src/configuration-factory.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import { LocalStorageBackedAsyncStore } from './local-storage';
1111

1212
export function configurationStorageFactory(
1313
persistenceStore?: IAsyncStore<Flag>,
14+
forceMemoryOnly = false,
1415
): IConfigurationStore<Flag> {
15-
if (persistenceStore) {
16+
if (forceMemoryOnly) {
17+
return new MemoryOnlyConfigurationStore();
18+
} else if (persistenceStore) {
1619
return new HybridConfigurationStore(new MemoryStore<Flag>(), persistenceStore);
1720
} else if (hasWindowLocalStorage()) {
1821
// fallback to window.localStorage if available

src/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,18 @@ export interface IClientConfig {
8282

8383
export { IAssignmentLogger, IAssignmentEvent, IEppoClient } from '@eppo/js-client-sdk-common';
8484

85+
// Instantiate the configuration store with memory-only implementation.
86+
const configurationStore = configurationStorageFactory(undefined, true);
87+
8588
/**
8689
* Client for assigning experiment variations.
8790
* @public
8891
*/
8992
export class EppoJSClient extends EppoClient {
90-
public static instance: EppoJSClient;
93+
// Ensure that the client is instantiated during class loading.
94+
// Use an empty memory-only configuration store until the `init` method is called,
95+
// to avoid serving stale data to the user.
96+
public static instance: EppoJSClient = new EppoJSClient(configurationStore, undefined, true);
9197
public static initialized = false;
9298

9399
public getStringAssignment(
@@ -161,8 +167,10 @@ export async function init(config: IClientConfig): Promise<IEppoClient> {
161167
EppoJSClient.instance.stopPolling();
162168
}
163169

164-
const configurationStore = configurationStorageFactory(config.persistentStore);
165-
EppoJSClient.instance = new EppoJSClient(configurationStore, undefined, true);
170+
// Set the configuration store to the desired persistent store, if provided.
171+
// Otherwise the factory method will detect the current environment and instantiate the correct store.
172+
const configurationStore = configurationStorageFactory(config.persistentStore, false);
173+
EppoJSClient.instance.setConfigurationStore(configurationStore);
166174

167175
const requestConfiguration: FlagConfigurationRequestParameters = {
168176
apiKey: config.apiKey,

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@
380380
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz#1d572bfbbe14b7704e0ba0f39b74815b84870d70"
381381
integrity sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==
382382

383-
384-
version "3.0.6"
385-
resolved "https://registry.yarnpkg.com/@eppo/js-client-sdk-common/-/js-client-sdk-common-3.0.6.tgz#8d2019d45708b944e26e493f09bcce833925ffa1"
386-
integrity sha512-YV32rf2UjNuKDAwyEl2HKearxsHRr2UDOJl+xblq5RCG8KB19xjxlNoUeFHd6FwTmqDVFF998r2DDEzoL496YQ==
383+
384+
version "3.0.8"
385+
resolved "https://registry.yarnpkg.com/@eppo/js-client-sdk-common/-/js-client-sdk-common-3.0.8.tgz#738c67c2ae95d4c8b8b84cd8625ff010e11ac6cc"
386+
integrity sha512-Qe7O78Pt2PYyTXMLYQMoGyv0aOu5NdXsO1VWBCjA6aUqkWKyy4etyMy1BXpwsbxH6kvKq1uwTJO19N7zoCVFlQ==
387387
dependencies:
388388
md5 "^2.3.0"
389389
pino "^8.19.0"

0 commit comments

Comments
 (0)