@@ -41,8 +41,9 @@ type RequestOptions = {
41
41
}
42
42
43
43
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 ;
46
47
const defaultAPI = 'https://edge.api.flagsmith.com/api/v1/' ;
47
48
let eventSource : typeof EventSource ;
48
49
const initError = function ( caller : string ) {
@@ -80,7 +81,7 @@ const Flagsmith = class {
80
81
}
81
82
82
83
getFlags = ( ) => {
83
- let { api, evaluationContext } = this ;
84
+ const { api, evaluationContext } = this ;
84
85
this . log ( "Get Flags" )
85
86
this . isLoading = true ;
86
87
@@ -271,7 +272,7 @@ const Flagsmith = class {
271
272
timer : number | null = null
272
273
dtrum = null
273
274
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 }
275
276
async init ( config : IInitConfig ) {
276
277
const evaluationContext = toEvaluationContext ( config . evaluationContext || this . evaluationContext ) ;
277
278
try {
@@ -290,7 +291,7 @@ const Flagsmith = class {
290
291
enableDynatrace,
291
292
enableAnalytics,
292
293
realtime,
293
- eventSourceUrl= "https://realtime.flagsmith.com/" ,
294
+ eventSourceUrl= "https://realtime.flagsmith.com/" ,
294
295
AsyncStorage : _AsyncStorage ,
295
296
identity,
296
297
traits,
@@ -331,7 +332,7 @@ const Flagsmith = class {
331
332
onError ?.( message ) ;
332
333
} ;
333
334
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 ;
335
336
if ( ! this . cacheOptions . ttl && this . cacheOptions . skipAPI ) {
336
337
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." )
337
338
}
@@ -345,6 +346,9 @@ const Flagsmith = class {
345
346
this . ticks = 10000 ;
346
347
this . timer = this . enableLogs ? new Date ( ) . valueOf ( ) : null ;
347
348
this . cacheFlags = typeof AsyncStorage !== 'undefined' && ! ! cacheFlags ;
349
+
350
+ FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT + "_" + evaluationContext . environment . apiKey ;
351
+
348
352
if ( _AsyncStorage ) {
349
353
AsyncStorage = _AsyncStorage ;
350
354
}
@@ -381,7 +385,7 @@ const Flagsmith = class {
381
385
}
382
386
383
387
if ( AsyncStorage && this . canUseStorage ) {
384
- AsyncStorage . getItem ( FLAGSMITH_EVENT )
388
+ AsyncStorage . getItem ( FlagsmithEvent )
385
389
. then ( ( res ) => {
386
390
try {
387
391
this . evaluationEvent = JSON . parse ( res ! ) || { }
@@ -398,12 +402,12 @@ const Flagsmith = class {
398
402
}
399
403
400
404
if ( AsyncStorage && this . canUseStorage ) {
401
- AsyncStorage . getItem ( FLAGSMITH_EVENT , ( err , res ) => {
405
+ AsyncStorage . getItem ( FlagsmithEvent , ( err , res ) => {
402
406
if ( res && this . evaluationContext . environment ) {
403
407
const json = JSON . parse ( res ) ;
404
408
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 ) ;
407
411
this . setState ( {
408
412
...state ,
409
413
evaluationEvent : json [ this . evaluationContext . environment . apiKey ] ,
@@ -453,7 +457,7 @@ const Flagsmith = class {
453
457
...json ,
454
458
evaluationContext : toEvaluationContext ( {
455
459
...json . evaluationContext ,
456
- identity : ! ! json . evaluationContext ?. identity ? {
460
+ identity : json . evaluationContext ?. identity ? {
457
461
...json . evaluationContext ?. identity ,
458
462
traits : {
459
463
...json . evaluationContext ?. identity ?. traits || { } ,
@@ -513,7 +517,7 @@ const Flagsmith = class {
513
517
}
514
518
} ;
515
519
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 ( ) ) ;
517
521
await onRetrievedStorage ( null , res )
518
522
} catch ( e ) { }
519
523
}
@@ -541,15 +545,6 @@ const Flagsmith = class {
541
545
}
542
546
}
543
547
544
- private _loadedState ( error : any = null , source : FlagSource , isFetching = false ) {
545
- return {
546
- error,
547
- isFetching,
548
- isLoading : false ,
549
- source
550
- }
551
- }
552
-
553
548
getAllFlags ( ) {
554
549
return this . flags ;
555
550
}
@@ -663,7 +658,7 @@ const Flagsmith = class {
663
658
}
664
659
665
660
setContext = ( clientEvaluationContext : ClientEvaluationContext ) => {
666
- let evaluationContext = toEvaluationContext ( clientEvaluationContext ) ;
661
+ const evaluationContext = toEvaluationContext ( clientEvaluationContext ) ;
667
662
this . evaluationContext = {
668
663
...evaluationContext ,
669
664
environment : evaluationContext . environment || this . evaluationContext . environment ,
@@ -748,6 +743,19 @@ const Flagsmith = class {
748
743
return res ;
749
744
} ;
750
745
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
+
751
759
private log ( ...args : ( unknown ) [ ] ) {
752
760
if ( this . enableLogs ) {
753
761
console . log . apply ( this , [ 'FLAGSMITH:' , new Date ( ) . valueOf ( ) - ( this . timer || 0 ) , 'ms' , ...args ] ) ;
@@ -759,7 +767,7 @@ const Flagsmith = class {
759
767
this . ts = new Date ( ) . valueOf ( ) ;
760
768
const state = JSON . stringify ( this . getState ( ) ) ;
761
769
this . log ( 'Setting storage' , state ) ;
762
- AsyncStorage ! . setItem ( FLAGSMITH_KEY , state ) ;
770
+ AsyncStorage ! . setItem ( this . getStorageKey ( ) , state ) ;
763
771
}
764
772
}
765
773
@@ -823,7 +831,7 @@ const Flagsmith = class {
823
831
private updateEventStorage ( ) {
824
832
if ( this . enableAnalytics ) {
825
833
const events = JSON . stringify ( this . getState ( ) . evaluationEvent ) ;
826
- AsyncStorage ! . setItem ( FLAGSMITH_EVENT , events ) ;
834
+ AsyncStorage ! . setItem ( FlagsmithEvent , events ) ;
827
835
}
828
836
}
829
837
0 commit comments