Skip to content

Commit fcf1625

Browse files
Release 8.0.0 (#274)
* Revert "Merge pull request #258 from Flagsmith/revert-252-feat/support-multiple-environment-caching" This reverts commit e2cfafd, reversing changes made to b81e1de. * Bump versions * Fix old cache tests --------- Co-authored-by: kyle-ssg <kyle@solidstategroup.com>
1 parent 5cd31ca commit fcf1625

13 files changed

+11050
-77
lines changed

flagsmith-core.ts

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

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

8283
getFlags = () => {
83-
let { api, evaluationContext } = this;
84+
const { api, evaluationContext } = this;
8485
this.log("Get Flags")
8586
this.isLoading = true;
8687

@@ -271,7 +272,7 @@ const Flagsmith = class {
271272
timer: number|null= null
272273
dtrum= null
273274
withTraits?: ITraits|null= null
274-
cacheOptions = {ttl:0, skipAPI: false, loadStale: false}
275+
cacheOptions = {ttl:0, skipAPI: false, loadStale: false, storageKey: undefined as string|undefined}
275276
async init(config: IInitConfig) {
276277
const evaluationContext = toEvaluationContext(config.evaluationContext || this.evaluationContext);
277278
try {
@@ -290,7 +291,7 @@ const Flagsmith = class {
290291
enableDynatrace,
291292
enableAnalytics,
292293
realtime,
293-
eventSourceUrl= "https://realtime.flagsmith.com/",
294+
eventSourceUrl= "https://realtime.flagsmith.com/",
294295
AsyncStorage: _AsyncStorage,
295296
identity,
296297
traits,
@@ -331,7 +332,7 @@ const Flagsmith = class {
331332
onError?.(message);
332333
};
333334
this.enableLogs = enableLogs || false;
334-
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
335+
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, storageKey:cacheOptions.storageKey, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
335336
if (!this.cacheOptions.ttl && this.cacheOptions.skipAPI) {
336337
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.")
337338
}
@@ -345,6 +346,9 @@ const Flagsmith = class {
345346
this.ticks = 10000;
346347
this.timer = this.enableLogs ? new Date().valueOf() : null;
347348
this.cacheFlags = typeof AsyncStorage !== 'undefined' && !!cacheFlags;
349+
350+
FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT + "_" + evaluationContext.environment.apiKey;
351+
348352
if (_AsyncStorage) {
349353
AsyncStorage = _AsyncStorage;
350354
}
@@ -381,7 +385,7 @@ const Flagsmith = class {
381385
}
382386

383387
if (AsyncStorage && this.canUseStorage) {
384-
AsyncStorage.getItem(FLAGSMITH_EVENT)
388+
AsyncStorage.getItem(FlagsmithEvent)
385389
.then((res)=>{
386390
try {
387391
this.evaluationEvent = JSON.parse(res!) || {}
@@ -398,12 +402,12 @@ const Flagsmith = class {
398402
}
399403

400404
if (AsyncStorage && this.canUseStorage) {
401-
AsyncStorage.getItem(FLAGSMITH_EVENT, (err, res) => {
405+
AsyncStorage.getItem(FlagsmithEvent, (err, res) => {
402406
if (res && this.evaluationContext.environment) {
403407
const json = JSON.parse(res);
404408
if (json[this.evaluationContext.environment.apiKey]) {
405-
const state = this.getState();
406-
this.log("Retrieved events from cache", res);
409+
const state = this.getState();
410+
this.log("Retrieved events from cache", res);
407411
this.setState({
408412
...state,
409413
evaluationEvent: json[this.evaluationContext.environment.apiKey],
@@ -453,7 +457,7 @@ const Flagsmith = class {
453457
...json,
454458
evaluationContext: toEvaluationContext({
455459
...json.evaluationContext,
456-
identity: !!json.evaluationContext?.identity ? {
460+
identity: json.evaluationContext?.identity ? {
457461
...json.evaluationContext?.identity,
458462
traits: {
459463
...json.evaluationContext?.identity?.traits || {},
@@ -513,7 +517,7 @@ const Flagsmith = class {
513517
}
514518
};
515519
try {
516-
const res = AsyncStorage.getItemSync? AsyncStorage.getItemSync(FLAGSMITH_KEY) : await AsyncStorage.getItem(FLAGSMITH_KEY);
520+
const res = AsyncStorage.getItemSync? AsyncStorage.getItemSync(this.getStorageKey()) : await AsyncStorage.getItem(this.getStorageKey());
517521
await onRetrievedStorage(null, res)
518522
} catch (e) {}
519523
}
@@ -541,15 +545,6 @@ const Flagsmith = class {
541545
}
542546
}
543547

544-
private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
545-
return {
546-
error,
547-
isFetching,
548-
isLoading: false,
549-
source
550-
}
551-
}
552-
553548
getAllFlags() {
554549
return this.flags;
555550
}
@@ -663,7 +658,7 @@ const Flagsmith = class {
663658
}
664659

665660
setContext = (clientEvaluationContext: ClientEvaluationContext) => {
666-
let evaluationContext = toEvaluationContext(clientEvaluationContext);
661+
const evaluationContext = toEvaluationContext(clientEvaluationContext);
667662
this.evaluationContext = {
668663
...evaluationContext,
669664
environment: evaluationContext.environment || this.evaluationContext.environment,
@@ -748,6 +743,19 @@ const Flagsmith = class {
748743
return res;
749744
};
750745

746+
private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
747+
return {
748+
error,
749+
isFetching,
750+
isLoading: false,
751+
source
752+
}
753+
}
754+
755+
private getStorageKey = ()=> {
756+
return this.cacheOptions?.storageKey || DEFAULT_FLAGSMITH_KEY + "_" + this.evaluationContext.environment?.apiKey
757+
}
758+
751759
private log(...args: (unknown)[]) {
752760
if (this.enableLogs) {
753761
console.log.apply(this, ['FLAGSMITH:', new Date().valueOf() - (this.timer || 0), 'ms', ...args]);
@@ -759,7 +767,7 @@ const Flagsmith = class {
759767
this.ts = new Date().valueOf();
760768
const state = JSON.stringify(this.getState());
761769
this.log('Setting storage', state);
762-
AsyncStorage!.setItem(FLAGSMITH_KEY, state);
770+
AsyncStorage!.setItem(this.getStorageKey(), state);
763771
}
764772
}
765773

@@ -823,7 +831,7 @@ const Flagsmith = class {
823831
private updateEventStorage() {
824832
if (this.enableAnalytics) {
825833
const events = JSON.stringify(this.getState().evaluationEvent);
826-
AsyncStorage!.setItem(FLAGSMITH_EVENT, events);
834+
AsyncStorage!.setItem(FlagsmithEvent, events);
827835
}
828836
}
829837

lib/flagsmith-es/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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": "7.0.2",
3+
"version": "8.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-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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": "7.0.2",
3+
"version": "8.0.0",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

0 commit comments

Comments
 (0)