@@ -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 ) {
@@ -272,7 +273,7 @@ const Flagsmith = class {
272
273
timer : number | null = null
273
274
dtrum = null
274
275
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 }
276
277
async init ( config : IInitConfig ) {
277
278
const evaluationContext = toEvaluationContext ( config . evaluationContext || this . evaluationContext ) ;
278
279
try {
@@ -291,7 +292,7 @@ const Flagsmith = class {
291
292
enableDynatrace,
292
293
enableAnalytics,
293
294
realtime,
294
- eventSourceUrl= "https://realtime.flagsmith.com/" ,
295
+ eventSourceUrl= "https://realtime.flagsmith.com/" ,
295
296
AsyncStorage : _AsyncStorage ,
296
297
identity,
297
298
traits,
@@ -332,7 +333,7 @@ const Flagsmith = class {
332
333
onError ?.( message ) ;
333
334
} ;
334
335
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 ;
336
337
if ( ! this . cacheOptions . ttl && this . cacheOptions . skipAPI ) {
337
338
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." )
338
339
}
@@ -346,6 +347,9 @@ const Flagsmith = class {
346
347
this . ticks = 10000 ;
347
348
this . timer = this . enableLogs ? new Date ( ) . valueOf ( ) : null ;
348
349
this . cacheFlags = typeof AsyncStorage !== 'undefined' && ! ! cacheFlags ;
350
+
351
+ FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT + "_" + evaluationContext . environment . apiKey ;
352
+
349
353
if ( _AsyncStorage ) {
350
354
AsyncStorage = _AsyncStorage ;
351
355
}
@@ -382,7 +386,7 @@ const Flagsmith = class {
382
386
}
383
387
384
388
if ( AsyncStorage && this . canUseStorage ) {
385
- AsyncStorage . getItem ( FLAGSMITH_EVENT )
389
+ AsyncStorage . getItem ( FlagsmithEvent )
386
390
. then ( ( res ) => {
387
391
try {
388
392
this . evaluationEvent = JSON . parse ( res ! ) || { }
@@ -399,12 +403,12 @@ const Flagsmith = class {
399
403
}
400
404
401
405
if ( AsyncStorage && this . canUseStorage ) {
402
- AsyncStorage . getItem ( FLAGSMITH_EVENT , ( err , res ) => {
406
+ AsyncStorage . getItem ( FlagsmithEvent , ( err , res ) => {
403
407
if ( res && this . evaluationContext . environment ) {
404
408
const json = JSON . parse ( res ) ;
405
409
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 ) ;
408
412
this . setState ( {
409
413
...state ,
410
414
evaluationEvent : json [ this . evaluationContext . environment . apiKey ] ,
@@ -480,7 +484,10 @@ const Flagsmith = class {
480
484
}
481
485
if ( shouldFetchFlags ) {
482
486
// We want to resolve init since we have cached flags
483
- this . getFlags ( ) ;
487
+
488
+ this . getFlags ( ) . catch ( ( error ) => {
489
+ this . onError ?.( error )
490
+ } )
484
491
}
485
492
} else {
486
493
if ( ! preventFetch ) {
@@ -511,7 +518,7 @@ const Flagsmith = class {
511
518
}
512
519
} ;
513
520
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 ( ) ) ;
515
522
await onRetrievedStorage ( null , res )
516
523
} catch ( e ) { }
517
524
}
@@ -539,15 +546,6 @@ const Flagsmith = class {
539
546
}
540
547
}
541
548
542
- private _loadedState ( error : any = null , source : FlagSource , isFetching = false ) {
543
- return {
544
- error,
545
- isFetching,
546
- isLoading : false ,
547
- source
548
- }
549
- }
550
-
551
549
getAllFlags ( ) {
552
550
return this . flags ;
553
551
}
@@ -746,6 +744,19 @@ const Flagsmith = class {
746
744
return res ;
747
745
} ;
748
746
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
+
749
760
private log ( ...args : ( unknown ) [ ] ) {
750
761
if ( this . enableLogs ) {
751
762
console . log . apply ( this , [ 'FLAGSMITH:' , new Date ( ) . valueOf ( ) - ( this . timer || 0 ) , 'ms' , ...args ] ) ;
@@ -757,7 +768,7 @@ const Flagsmith = class {
757
768
this . ts = new Date ( ) . valueOf ( ) ;
758
769
const state = JSON . stringify ( this . getState ( ) ) ;
759
770
this . log ( 'Setting storage' , state ) ;
760
- AsyncStorage ! . setItem ( FLAGSMITH_KEY , state ) ;
771
+ AsyncStorage ! . setItem ( this . getStorageKey ( ) , state ) ;
761
772
}
762
773
}
763
774
@@ -821,7 +832,7 @@ const Flagsmith = class {
821
832
private updateEventStorage ( ) {
822
833
if ( this . enableAnalytics ) {
823
834
const events = JSON . stringify ( this . getState ( ) . evaluationEvent ) ;
824
- AsyncStorage ! . setItem ( FLAGSMITH_EVENT , events ) ;
835
+ AsyncStorage ! . setItem ( FlagsmithEvent , events ) ;
825
836
}
826
837
}
827
838
0 commit comments