@@ -43,7 +43,6 @@ type RequestOptions = {
43
43
let AsyncStorage : AsyncStorageType = null ;
44
44
const DEFAULT_FLAGSMITH_KEY = "FLAGSMITH_DB" ;
45
45
const DEFAULT_FLAGSMITH_EVENT = "FLAGSMITH_EVENT" ;
46
- let FlagsmithKey = DEFAULT_FLAGSMITH_KEY ;
47
46
let FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT ;
48
47
const defaultAPI = 'https://edge.api.flagsmith.com/api/v1/' ;
49
48
let eventSource : typeof EventSource ;
@@ -82,7 +81,7 @@ const Flagsmith = class {
82
81
}
83
82
84
83
getFlags = ( ) => {
85
- let { api, evaluationContext } = this ;
84
+ const { api, evaluationContext } = this ;
86
85
this . log ( "Get Flags" )
87
86
this . isLoading = true ;
88
87
@@ -273,7 +272,7 @@ const Flagsmith = class {
273
272
timer : number | null = null
274
273
dtrum = null
275
274
withTraits ?: ITraits | null = null
276
- cacheOptions = { ttl :0 , skipAPI : false , loadStale : false }
275
+ cacheOptions = { ttl :0 , skipAPI : false , loadStale : false , storageKey : undefined as string | undefined }
277
276
async init ( config : IInitConfig ) {
278
277
const evaluationContext = toEvaluationContext ( config . evaluationContext || { } ) ;
279
278
try {
@@ -292,7 +291,7 @@ const Flagsmith = class {
292
291
enableDynatrace,
293
292
enableAnalytics,
294
293
realtime,
295
- eventSourceUrl= "https://realtime.flagsmith.com/" ,
294
+ eventSourceUrl= "https://realtime.flagsmith.com/" ,
296
295
AsyncStorage : _AsyncStorage ,
297
296
identity,
298
297
traits,
@@ -333,7 +332,7 @@ const Flagsmith = class {
333
332
onError ?.( message ) ;
334
333
} ;
335
334
this . enableLogs = enableLogs || false ;
336
- 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 ;
337
336
if ( ! this . cacheOptions . ttl && this . cacheOptions . skipAPI ) {
338
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." )
339
338
}
@@ -348,7 +347,6 @@ const Flagsmith = class {
348
347
this . timer = this . enableLogs ? new Date ( ) . valueOf ( ) : null ;
349
348
this . cacheFlags = typeof AsyncStorage !== 'undefined' && ! ! cacheFlags ;
350
349
351
- FlagsmithKey = cacheOptions ?. storageKey || DEFAULT_FLAGSMITH_KEY + "_" + evaluationContext . environment . apiKey ;
352
350
FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT + "_" + evaluationContext . environment . apiKey ;
353
351
354
352
if ( _AsyncStorage ) {
@@ -408,8 +406,8 @@ const Flagsmith = class {
408
406
if ( res && this . evaluationContext . environment ) {
409
407
const json = JSON . parse ( res ) ;
410
408
if ( json [ this . evaluationContext . environment . apiKey ] ) {
411
- const state = this . getState ( ) ;
412
- this . log ( "Retrieved events from cache" , res ) ;
409
+ const state = this . getState ( ) ;
410
+ this . log ( "Retrieved events from cache" , res ) ;
413
411
this . setState ( {
414
412
...state ,
415
413
evaluationEvent : json [ this . evaluationContext . environment . apiKey ] ,
@@ -459,7 +457,7 @@ const Flagsmith = class {
459
457
...json ,
460
458
evaluationContext : toEvaluationContext ( {
461
459
...json . evaluationContext ,
462
- identity : ! ! json . evaluationContext ?. identity ? {
460
+ identity : json . evaluationContext ?. identity ? {
463
461
...json . evaluationContext ?. identity ,
464
462
traits : {
465
463
...json . evaluationContext ?. identity ?. traits || { } ,
@@ -516,7 +514,7 @@ const Flagsmith = class {
516
514
}
517
515
} ;
518
516
try {
519
- const res = AsyncStorage . getItemSync ? AsyncStorage . getItemSync ( FlagsmithKey ) : await AsyncStorage . getItem ( FlagsmithKey ) ;
517
+ const res = AsyncStorage . getItemSync ? AsyncStorage . getItemSync ( this . getStorageKey ( ) ) : await AsyncStorage . getItem ( this . getStorageKey ( ) ) ;
520
518
await onRetrievedStorage ( null , res )
521
519
} catch ( e ) { }
522
520
}
@@ -544,15 +542,6 @@ const Flagsmith = class {
544
542
}
545
543
}
546
544
547
- private _loadedState ( error : any = null , source : FlagSource , isFetching = false ) {
548
- return {
549
- error,
550
- isFetching,
551
- isLoading : false ,
552
- source
553
- }
554
- }
555
-
556
545
getAllFlags ( ) {
557
546
return this . flags ;
558
547
}
@@ -596,7 +585,7 @@ const Flagsmith = class {
596
585
this . api = state . api || this . api || defaultAPI ;
597
586
this . flags = state . flags || this . flags ;
598
587
this . evaluationContext = state . evaluationContext || this . evaluationContext ,
599
- this . evaluationEvent = state . evaluationEvent || this . evaluationEvent ;
588
+ this . evaluationEvent = state . evaluationEvent || this . evaluationEvent ;
600
589
this . log ( "setState called" , this )
601
590
}
602
591
}
@@ -666,7 +655,7 @@ const Flagsmith = class {
666
655
}
667
656
668
657
setContext = ( clientEvaluationContext : ClientEvaluationContext ) => {
669
- let evaluationContext = toEvaluationContext ( clientEvaluationContext ) ;
658
+ const evaluationContext = toEvaluationContext ( clientEvaluationContext ) ;
670
659
this . evaluationContext = {
671
660
...evaluationContext ,
672
661
environment : evaluationContext . environment || this . evaluationContext . environment ,
@@ -751,6 +740,19 @@ const Flagsmith = class {
751
740
return res ;
752
741
} ;
753
742
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
+
754
756
private log ( ...args : ( unknown ) [ ] ) {
755
757
if ( this . enableLogs ) {
756
758
console . log . apply ( this , [ 'FLAGSMITH:' , new Date ( ) . valueOf ( ) - ( this . timer || 0 ) , 'ms' , ...args ] ) ;
@@ -762,7 +764,7 @@ const Flagsmith = class {
762
764
this . ts = new Date ( ) . valueOf ( ) ;
763
765
const state = JSON . stringify ( this . getState ( ) ) ;
764
766
this . log ( 'Setting storage' , state ) ;
765
- AsyncStorage ! . setItem ( FlagsmithKey , state ) ;
767
+ AsyncStorage ! . setItem ( this . getStorageKey ( ) , state ) ;
766
768
}
767
769
}
768
770
0 commit comments