Skip to content

Commit e2cfafd

Browse files
authored
Merge pull request #258 from Flagsmith/revert-252-feat/support-multiple-environment-caching
Revert "feat: support multiple environment caching"
2 parents b81e1de + c772f71 commit e2cfafd

File tree

9 files changed

+68
-104
lines changed

9 files changed

+68
-104
lines changed

flagsmith-core.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ type RequestOptions = {
4141
}
4242

4343
let AsyncStorage: AsyncStorageType = null;
44-
const DEFAULT_FLAGSMITH_KEY = "FLAGSMITH_DB";
45-
const DEFAULT_FLAGSMITH_EVENT = "FLAGSMITH_EVENT";
46-
let FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT;
44+
const FLAGSMITH_KEY = "BULLET_TRAIN_DB";
45+
const FLAGSMITH_EVENT = "BULLET_TRAIN_EVENT";
4746
const defaultAPI = 'https://edge.api.flagsmith.com/api/v1/';
4847
let eventSource: typeof EventSource;
4948
const initError = function(caller: string) {
@@ -81,7 +80,7 @@ const Flagsmith = class {
8180
}
8281

8382
getFlags = () => {
84-
const { api, evaluationContext } = this;
83+
let { api, evaluationContext } = this;
8584
this.log("Get Flags")
8685
this.isLoading = true;
8786

@@ -272,7 +271,7 @@ const Flagsmith = class {
272271
timer: number|null= null
273272
dtrum= null
274273
withTraits?: ITraits|null= null
275-
cacheOptions = {ttl:0, skipAPI: false, loadStale: false, storageKey: undefined as string|undefined}
274+
cacheOptions = {ttl:0, skipAPI: false, loadStale: false}
276275
async init(config: IInitConfig) {
277276
const evaluationContext = toEvaluationContext(config.evaluationContext || {});
278277
try {
@@ -291,7 +290,7 @@ const Flagsmith = class {
291290
enableDynatrace,
292291
enableAnalytics,
293292
realtime,
294-
eventSourceUrl= "https://realtime.flagsmith.com/",
293+
eventSourceUrl= "https://realtime.flagsmith.com/",
295294
AsyncStorage: _AsyncStorage,
296295
identity,
297296
traits,
@@ -332,7 +331,7 @@ const Flagsmith = class {
332331
onError?.(message);
333332
};
334333
this.enableLogs = enableLogs || false;
335-
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, storageKey:cacheOptions.storageKey, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
334+
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
336335
if (!this.cacheOptions.ttl && this.cacheOptions.skipAPI) {
337336
console.warn("Flagsmith: you have set a cache ttl of 0 and are skipping API calls, this means the API will not be hit unless you clear local storage.")
338337
}
@@ -346,9 +345,6 @@ const Flagsmith = class {
346345
this.ticks = 10000;
347346
this.timer = this.enableLogs ? new Date().valueOf() : null;
348347
this.cacheFlags = typeof AsyncStorage !== 'undefined' && !!cacheFlags;
349-
350-
FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT + "_" + evaluationContext.environment.apiKey;
351-
352348
if (_AsyncStorage) {
353349
AsyncStorage = _AsyncStorage;
354350
}
@@ -385,7 +381,7 @@ const Flagsmith = class {
385381
}
386382

387383
if (AsyncStorage && this.canUseStorage) {
388-
AsyncStorage.getItem(FlagsmithEvent)
384+
AsyncStorage.getItem(FLAGSMITH_EVENT)
389385
.then((res)=>{
390386
try {
391387
this.evaluationEvent = JSON.parse(res!) || {}
@@ -402,12 +398,12 @@ const Flagsmith = class {
402398
}
403399

404400
if (AsyncStorage && this.canUseStorage) {
405-
AsyncStorage.getItem(FlagsmithEvent, (err, res) => {
401+
AsyncStorage.getItem(FLAGSMITH_EVENT, (err, res) => {
406402
if (res && this.evaluationContext.environment) {
407403
const json = JSON.parse(res);
408404
if (json[this.evaluationContext.environment.apiKey]) {
409-
const state = this.getState();
410-
this.log("Retrieved events from cache", res);
405+
const state = this.getState();
406+
this.log("Retrieved events from cache", res);
411407
this.setState({
412408
...state,
413409
evaluationEvent: json[this.evaluationContext.environment.apiKey],
@@ -457,7 +453,7 @@ const Flagsmith = class {
457453
...json,
458454
evaluationContext: toEvaluationContext({
459455
...json.evaluationContext,
460-
identity: json.evaluationContext?.identity ? {
456+
identity: !!json.evaluationContext?.identity ? {
461457
...json.evaluationContext?.identity,
462458
traits: {
463459
...json.evaluationContext?.identity?.traits || {},
@@ -514,7 +510,7 @@ const Flagsmith = class {
514510
}
515511
};
516512
try {
517-
const res = AsyncStorage.getItemSync? AsyncStorage.getItemSync(this.getStorageKey()) : await AsyncStorage.getItem(this.getStorageKey());
513+
const res = AsyncStorage.getItemSync? AsyncStorage.getItemSync(FLAGSMITH_KEY) : await AsyncStorage.getItem(FLAGSMITH_KEY);
518514
await onRetrievedStorage(null, res)
519515
} catch (e) {}
520516
}
@@ -542,6 +538,15 @@ const Flagsmith = class {
542538
}
543539
}
544540

541+
private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
542+
return {
543+
error,
544+
isFetching,
545+
isLoading: false,
546+
source
547+
}
548+
}
549+
545550
getAllFlags() {
546551
return this.flags;
547552
}
@@ -655,7 +660,7 @@ const Flagsmith = class {
655660
}
656661

657662
setContext = (clientEvaluationContext: ClientEvaluationContext) => {
658-
const evaluationContext = toEvaluationContext(clientEvaluationContext);
663+
let evaluationContext = toEvaluationContext(clientEvaluationContext);
659664
this.evaluationContext = {
660665
...evaluationContext,
661666
environment: evaluationContext.environment || this.evaluationContext.environment,
@@ -740,19 +745,6 @@ const Flagsmith = class {
740745
return res;
741746
};
742747

743-
private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
744-
return {
745-
error,
746-
isFetching,
747-
isLoading: false,
748-
source
749-
}
750-
}
751-
752-
private getStorageKey = ()=> {
753-
return this.cacheOptions?.storageKey || DEFAULT_FLAGSMITH_KEY + "_" + this.evaluationContext.environment?.apiKey
754-
}
755-
756748
private log(...args: (unknown)[]) {
757749
if (this.enableLogs) {
758750
console.log.apply(this, ['FLAGSMITH:', new Date().valueOf() - (this.timer || 0), 'ms', ...args]);
@@ -764,7 +756,7 @@ const Flagsmith = class {
764756
this.ts = new Date().valueOf();
765757
const state = JSON.stringify(this.getState());
766758
this.log('Setting storage', state);
767-
AsyncStorage!.setItem(this.getStorageKey(), state);
759+
AsyncStorage!.setItem(FLAGSMITH_KEY, state);
768760
}
769761
}
770762

@@ -828,7 +820,7 @@ const Flagsmith = class {
828820
private updateEventStorage() {
829821
if (this.enableAnalytics) {
830822
const events = JSON.stringify(this.getState().evaluationEvent);
831-
AsyncStorage!.setItem(FlagsmithEvent, events);
823+
AsyncStorage!.setItem(FLAGSMITH_EVENT, events);
832824
}
833825
}
834826

lib/flagsmith-es/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith-es",
3-
"version": "6.0.0",
3+
"version": "7.0.0",
44
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
55
"main": "./index.js",
66
"type": "module",

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "6.0.0",
3+
"version": "7.0.0",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

lib/react-native-flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "6.0.0",
3+
"version": "7.0.0",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

test/cache.test.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
// Sample test
12
import {
23
defaultState,
34
defaultStateAlt,
4-
FLAGSMITH_KEY,
55
getFlagsmith,
66
getStateToCheck,
77
identityState,
@@ -33,21 +33,7 @@ describe('Cache', () => {
3333
onChange,
3434
});
3535
await flagsmith.init(initConfig);
36-
const cache = await AsyncStorage.getItem(FLAGSMITH_KEY);
37-
expect(getStateToCheck(JSON.parse(`${cache}`))).toEqual(defaultState);
38-
});
39-
test('should set cache after init with custom key', async () => {
40-
const onChange = jest.fn();
41-
const customKey = 'custom_key';
42-
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
43-
cacheFlags: true,
44-
cacheOptions: {
45-
storageKey: customKey,
46-
},
47-
onChange,
48-
});
49-
await flagsmith.init(initConfig);
50-
const cache = await AsyncStorage.getItem(customKey);
36+
const cache = await AsyncStorage.getItem('BULLET_TRAIN_DB');
5137
expect(getStateToCheck(JSON.parse(`${cache}`))).toEqual(defaultState);
5238
});
5339
test('should call onChange with cache then eventually with an API response', async () => {
@@ -67,7 +53,7 @@ describe('Cache', () => {
6753
cacheFlags: true,
6854
onChange,
6955
});
70-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify(defaultStateAlt));
56+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify(defaultStateAlt));
7157
await flagsmith.init(initConfig);
7258

7359
// Flags retrieved from cache
@@ -100,7 +86,7 @@ describe('Cache', () => {
10086
identity: testIdentity,
10187
onChange,
10288
});
103-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
89+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
10490
...defaultStateAlt,
10591
identity: 'bad_identity',
10692
}));
@@ -116,7 +102,7 @@ describe('Cache', () => {
116102
onChange,
117103
cacheOptions: { ttl: 1 },
118104
});
119-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
105+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
120106
...defaultStateAlt,
121107
ts: new Date().valueOf() - 100,
122108
}));
@@ -134,7 +120,7 @@ describe('Cache', () => {
134120
onChange,
135121
cacheOptions: { ttl: 1, loadStale: true },
136122
});
137-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
123+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
138124
...defaultStateAlt,
139125
ts: new Date().valueOf() - 100,
140126
}));
@@ -152,7 +138,7 @@ describe('Cache', () => {
152138
onChange,
153139
cacheOptions: { ttl: 1000 },
154140
});
155-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
141+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
156142
...defaultStateAlt,
157143
ts: new Date().valueOf(),
158144
}));
@@ -169,7 +155,7 @@ describe('Cache', () => {
169155
cacheFlags: false,
170156
onChange,
171157
});
172-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
158+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
173159
...defaultStateAlt,
174160
ts: new Date().valueOf(),
175161
}));
@@ -187,7 +173,25 @@ describe('Cache', () => {
187173
onChange,
188174
cacheOptions: { ttl: 1000, skipAPI: true },
189175
});
190-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
176+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
177+
...defaultStateAlt,
178+
ts: new Date().valueOf(),
179+
}));
180+
await flagsmith.init(initConfig);
181+
expect(onChange).toHaveBeenCalledTimes(1);
182+
expect(mockFetch).toHaveBeenCalledTimes(0);
183+
expect(getStateToCheck(flagsmith.getState())).toEqual({
184+
...defaultStateAlt,
185+
});
186+
});
187+
test('should not get flags from API when skipAPI is set', async () => {
188+
const onChange = jest.fn();
189+
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
190+
cacheFlags: true,
191+
onChange,
192+
cacheOptions: { ttl: 1000, skipAPI: true },
193+
});
194+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
191195
...defaultStateAlt,
192196
ts: new Date().valueOf(),
193197
}));
@@ -205,7 +209,7 @@ describe('Cache', () => {
205209
onChange,
206210
cacheOptions: { ttl: 1, skipAPI: true, loadStale: true },
207211
});
208-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
212+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
209213
...defaultStateAlt,
210214
ts: new Date().valueOf() - 100,
211215
}));
@@ -216,15 +220,14 @@ describe('Cache', () => {
216220
...defaultStateAlt,
217221
});
218222
});
219-
220223
test('should validate flags are unchanged when fetched', async () => {
221224
const onChange = jest.fn();
222225
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
223226
onChange,
224227
cacheFlags: true,
225228
preventFetch: true,
226229
});
227-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
230+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
228231
...defaultState,
229232
}));
230233
await flagsmith.init(initConfig);
@@ -270,7 +273,7 @@ describe('Cache', () => {
270273
preventFetch: true,
271274
defaultFlags: defaultState.flags,
272275
});
273-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
276+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
274277
...defaultState,
275278
}));
276279
await flagsmith.init(initConfig);
@@ -316,7 +319,7 @@ describe('Cache', () => {
316319
preventFetch: true,
317320
});
318321
const storage = new SyncStorageMock();
319-
await storage.setItem(FLAGSMITH_KEY, JSON.stringify({
322+
await storage.setItem('BULLET_TRAIN_DB', JSON.stringify({
320323
...defaultState,
321324
}));
322325
flagsmith.init({
@@ -342,7 +345,7 @@ describe('Cache', () => {
342345
preventFetch: true,
343346
});
344347
const storage = new SyncStorageMock();
345-
await storage.setItem(FLAGSMITH_KEY, JSON.stringify({
348+
await storage.setItem('BULLET_TRAIN_DB', JSON.stringify({
346349
...identityState,
347350
}));
348351
const ts = Date.now();
@@ -353,8 +356,8 @@ describe('Cache', () => {
353356
});
354357
expect(flagsmith.getAllTraits()).toEqual({
355358
...identityState.traits,
356-
ts,
357-
});
359+
ts
360+
})
358361
});
359362
test('should cache transient traits correctly', async () => {
360363
const onChange = jest.fn();
@@ -392,4 +395,4 @@ describe('Cache', () => {
392395
},
393396
})
394397
});
395-
});
398+
});

test/default-flags.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Sample test
2-
import { defaultState, defaultStateAlt, FLAGSMITH_KEY, getFlagsmith, getStateToCheck } from './test-constants';
2+
import { defaultState, defaultStateAlt, getFlagsmith, getStateToCheck } from './test-constants';
33
import { IFlags } from '../types';
44

55
describe('Default Flags', () => {
@@ -51,7 +51,7 @@ describe('Default Flags', () => {
5151
cacheFlags: true,
5252
defaultFlags: {...defaultFlags, ...itemsToRemove},
5353
});
54-
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify({
54+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
5555
...defaultState,
5656
flags: {
5757
...defaultFlags,

0 commit comments

Comments
 (0)