Skip to content

Commit b23bdfd

Browse files
committed
Merge branch 'main' into chore/typecheck-on-build
# Conflicts: # test/cache.test.ts
2 parents 21090f0 + fcf1625 commit b23bdfd

File tree

13 files changed

+11225
-151
lines changed

13 files changed

+11225
-151
lines changed

flagsmith-core.ts

Lines changed: 33 additions & 22 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) {
@@ -272,7 +273,7 @@ const Flagsmith = class {
272273
timer: number|null= null
273274
dtrum= null
274275
withTraits?: ITraits|null= null
275-
cacheOptions = {ttl:0, skipAPI: false, loadStale: false}
276+
cacheOptions = {ttl:0, skipAPI: false, loadStale: false, storageKey: undefined as string|undefined}
276277
async init(config: IInitConfig) {
277278
const evaluationContext = toEvaluationContext(config.evaluationContext || this.evaluationContext);
278279
try {
@@ -291,7 +292,7 @@ const Flagsmith = class {
291292
enableDynatrace,
292293
enableAnalytics,
293294
realtime,
294-
eventSourceUrl= "https://realtime.flagsmith.com/",
295+
eventSourceUrl= "https://realtime.flagsmith.com/",
295296
AsyncStorage: _AsyncStorage,
296297
identity,
297298
traits,
@@ -332,7 +333,7 @@ const Flagsmith = class {
332333
onError?.(message);
333334
};
334335
this.enableLogs = enableLogs || false;
335-
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
336+
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, storageKey:cacheOptions.storageKey, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
336337
if (!this.cacheOptions.ttl && this.cacheOptions.skipAPI) {
337338
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.")
338339
}
@@ -346,6 +347,9 @@ const Flagsmith = class {
346347
this.ticks = 10000;
347348
this.timer = this.enableLogs ? new Date().valueOf() : null;
348349
this.cacheFlags = typeof AsyncStorage !== 'undefined' && !!cacheFlags;
350+
351+
FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT + "_" + evaluationContext.environment.apiKey;
352+
349353
if (_AsyncStorage) {
350354
AsyncStorage = _AsyncStorage;
351355
}
@@ -382,7 +386,7 @@ const Flagsmith = class {
382386
}
383387

384388
if (AsyncStorage && this.canUseStorage) {
385-
AsyncStorage.getItem(FLAGSMITH_EVENT)
389+
AsyncStorage.getItem(FlagsmithEvent)
386390
.then((res)=>{
387391
try {
388392
this.evaluationEvent = JSON.parse(res!) || {}
@@ -399,12 +403,12 @@ const Flagsmith = class {
399403
}
400404

401405
if (AsyncStorage && this.canUseStorage) {
402-
AsyncStorage.getItem(FLAGSMITH_EVENT, (err, res) => {
406+
AsyncStorage.getItem(FlagsmithEvent, (err, res) => {
403407
if (res && this.evaluationContext.environment) {
404408
const json = JSON.parse(res);
405409
if (json[this.evaluationContext.environment.apiKey]) {
406-
const state = this.getState();
407-
this.log("Retrieved events from cache", res);
410+
const state = this.getState();
411+
this.log("Retrieved events from cache", res);
408412
this.setState({
409413
...state,
410414
evaluationEvent: json[this.evaluationContext.environment.apiKey],
@@ -480,7 +484,10 @@ const Flagsmith = class {
480484
}
481485
if (shouldFetchFlags) {
482486
// We want to resolve init since we have cached flags
483-
this.getFlags();
487+
488+
this.getFlags().catch((error) => {
489+
this.onError?.(error)
490+
})
484491
}
485492
} else {
486493
if (!preventFetch) {
@@ -511,7 +518,7 @@ const Flagsmith = class {
511518
}
512519
};
513520
try {
514-
const res = AsyncStorage.getItemSync? AsyncStorage.getItemSync(FLAGSMITH_KEY) : await AsyncStorage.getItem(FLAGSMITH_KEY);
521+
const res = AsyncStorage.getItemSync? AsyncStorage.getItemSync(this.getStorageKey()) : await AsyncStorage.getItem(this.getStorageKey());
515522
await onRetrievedStorage(null, res)
516523
} catch (e) {}
517524
}
@@ -539,15 +546,6 @@ const Flagsmith = class {
539546
}
540547
}
541548

542-
private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
543-
return {
544-
error,
545-
isFetching,
546-
isLoading: false,
547-
source
548-
}
549-
}
550-
551549
getAllFlags() {
552550
return this.flags;
553551
}
@@ -746,6 +744,19 @@ const Flagsmith = class {
746744
return res;
747745
};
748746

747+
private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
748+
return {
749+
error,
750+
isFetching,
751+
isLoading: false,
752+
source
753+
}
754+
}
755+
756+
private getStorageKey = ()=> {
757+
return this.cacheOptions?.storageKey || DEFAULT_FLAGSMITH_KEY + "_" + this.evaluationContext.environment?.apiKey
758+
}
759+
749760
private log(...args: (unknown)[]) {
750761
if (this.enableLogs) {
751762
console.log.apply(this, ['FLAGSMITH:', new Date().valueOf() - (this.timer || 0), 'ms', ...args]);
@@ -757,7 +768,7 @@ const Flagsmith = class {
757768
this.ts = new Date().valueOf();
758769
const state = JSON.stringify(this.getState());
759770
this.log('Setting storage', state);
760-
AsyncStorage!.setItem(FLAGSMITH_KEY, state);
771+
AsyncStorage!.setItem(this.getStorageKey(), state);
761772
}
762773
}
763774

@@ -821,7 +832,7 @@ const Flagsmith = class {
821832
private updateEventStorage() {
822833
if (this.enableAnalytics) {
823834
const events = JSON.stringify(this.getState().evaluationEvent);
824-
AsyncStorage!.setItem(FLAGSMITH_EVENT, events);
835+
AsyncStorage!.setItem(FlagsmithEvent, events);
825836
}
826837
}
827838

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)