Skip to content

Commit 1b3f13c

Browse files
committed
neaten
1 parent 731053a commit 1b3f13c

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

flagsmith-core.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ type RequestOptions = {
4343
let AsyncStorage: AsyncStorageType = null;
4444
const DEFAULT_FLAGSMITH_KEY = "FLAGSMITH_DB";
4545
const DEFAULT_FLAGSMITH_EVENT = "FLAGSMITH_EVENT";
46-
let FlagsmithKey = DEFAULT_FLAGSMITH_KEY;
4746
let FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT;
4847
const defaultAPI = 'https://edge.api.flagsmith.com/api/v1/';
4948
let eventSource: typeof EventSource;
@@ -82,7 +81,7 @@ const Flagsmith = class {
8281
}
8382

8483
getFlags = () => {
85-
let { api, evaluationContext } = this;
84+
const { api, evaluationContext } = this;
8685
this.log("Get Flags")
8786
this.isLoading = true;
8887

@@ -273,7 +272,7 @@ const Flagsmith = class {
273272
timer: number|null= null
274273
dtrum= null
275274
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}
277276
async init(config: IInitConfig) {
278277
const evaluationContext = toEvaluationContext(config.evaluationContext || {});
279278
try {
@@ -292,7 +291,7 @@ const Flagsmith = class {
292291
enableDynatrace,
293292
enableAnalytics,
294293
realtime,
295-
eventSourceUrl= "https://realtime.flagsmith.com/",
294+
eventSourceUrl= "https://realtime.flagsmith.com/",
296295
AsyncStorage: _AsyncStorage,
297296
identity,
298297
traits,
@@ -333,7 +332,7 @@ const Flagsmith = class {
333332
onError?.(message);
334333
};
335334
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;
337336
if (!this.cacheOptions.ttl && this.cacheOptions.skipAPI) {
338337
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.")
339338
}
@@ -348,7 +347,6 @@ const Flagsmith = class {
348347
this.timer = this.enableLogs ? new Date().valueOf() : null;
349348
this.cacheFlags = typeof AsyncStorage !== 'undefined' && !!cacheFlags;
350349

351-
FlagsmithKey = cacheOptions?.storageKey || DEFAULT_FLAGSMITH_KEY + "_" + evaluationContext.environment.apiKey;
352350
FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT + "_" + evaluationContext.environment.apiKey;
353351

354352
if (_AsyncStorage) {
@@ -408,8 +406,8 @@ const Flagsmith = class {
408406
if (res && this.evaluationContext.environment) {
409407
const json = JSON.parse(res);
410408
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);
413411
this.setState({
414412
...state,
415413
evaluationEvent: json[this.evaluationContext.environment.apiKey],
@@ -459,7 +457,7 @@ const Flagsmith = class {
459457
...json,
460458
evaluationContext: toEvaluationContext({
461459
...json.evaluationContext,
462-
identity: !!json.evaluationContext?.identity ? {
460+
identity: json.evaluationContext?.identity ? {
463461
...json.evaluationContext?.identity,
464462
traits: {
465463
...json.evaluationContext?.identity?.traits || {},
@@ -516,7 +514,7 @@ const Flagsmith = class {
516514
}
517515
};
518516
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());
520518
await onRetrievedStorage(null, res)
521519
} catch (e) {}
522520
}
@@ -544,15 +542,6 @@ const Flagsmith = class {
544542
}
545543
}
546544

547-
private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
548-
return {
549-
error,
550-
isFetching,
551-
isLoading: false,
552-
source
553-
}
554-
}
555-
556545
getAllFlags() {
557546
return this.flags;
558547
}
@@ -596,7 +585,7 @@ const Flagsmith = class {
596585
this.api = state.api || this.api || defaultAPI;
597586
this.flags = state.flags || this.flags;
598587
this.evaluationContext = state.evaluationContext || this.evaluationContext,
599-
this.evaluationEvent = state.evaluationEvent || this.evaluationEvent;
588+
this.evaluationEvent = state.evaluationEvent || this.evaluationEvent;
600589
this.log("setState called", this)
601590
}
602591
}
@@ -666,7 +655,7 @@ const Flagsmith = class {
666655
}
667656

668657
setContext = (clientEvaluationContext: ClientEvaluationContext) => {
669-
let evaluationContext = toEvaluationContext(clientEvaluationContext);
658+
const evaluationContext = toEvaluationContext(clientEvaluationContext);
670659
this.evaluationContext = {
671660
...evaluationContext,
672661
environment: evaluationContext.environment || this.evaluationContext.environment,
@@ -751,6 +740,19 @@ const Flagsmith = class {
751740
return res;
752741
};
753742

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+
754756
private log(...args: (unknown)[]) {
755757
if (this.enableLogs) {
756758
console.log.apply(this, ['FLAGSMITH:', new Date().valueOf() - (this.timer || 0), 'ms', ...args]);
@@ -762,7 +764,7 @@ const Flagsmith = class {
762764
this.ts = new Date().valueOf();
763765
const state = JSON.stringify(this.getState());
764766
this.log('Setting storage', state);
765-
AsyncStorage!.setItem(FlagsmithKey, state);
767+
AsyncStorage!.setItem(this.getStorageKey(), state);
766768
}
767769
}
768770

0 commit comments

Comments
 (0)