Skip to content

Commit

Permalink
Release 8.0.0 (#274)
Browse files Browse the repository at this point in the history
* Revert "Merge pull request #258 from Flagsmith/revert-252-feat/support-multiple-environment-caching"

This reverts commit e2cfafd, reversing
changes made to b81e1de.

* Bump versions

* Fix old cache tests

---------

Co-authored-by: kyle-ssg <[email protected]>
  • Loading branch information
matthewelwell and kyle-ssg authored Dec 18, 2024
1 parent 5cd31ca commit fcf1625
Show file tree
Hide file tree
Showing 13 changed files with 11,050 additions and 77 deletions.
56 changes: 32 additions & 24 deletions flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ type RequestOptions = {
}

let AsyncStorage: AsyncStorageType = null;
const FLAGSMITH_KEY = "BULLET_TRAIN_DB";
const FLAGSMITH_EVENT = "BULLET_TRAIN_EVENT";
const DEFAULT_FLAGSMITH_KEY = "FLAGSMITH_DB";
const DEFAULT_FLAGSMITH_EVENT = "FLAGSMITH_EVENT";
let FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT;
const defaultAPI = 'https://edge.api.flagsmith.com/api/v1/';
let eventSource: typeof EventSource;
const initError = function(caller: string) {
Expand Down Expand Up @@ -80,7 +81,7 @@ const Flagsmith = class {
}

getFlags = () => {
let { api, evaluationContext } = this;
const { api, evaluationContext } = this;
this.log("Get Flags")
this.isLoading = true;

Expand Down Expand Up @@ -271,7 +272,7 @@ const Flagsmith = class {
timer: number|null= null
dtrum= null
withTraits?: ITraits|null= null
cacheOptions = {ttl:0, skipAPI: false, loadStale: false}
cacheOptions = {ttl:0, skipAPI: false, loadStale: false, storageKey: undefined as string|undefined}
async init(config: IInitConfig) {
const evaluationContext = toEvaluationContext(config.evaluationContext || this.evaluationContext);
try {
Expand All @@ -290,7 +291,7 @@ const Flagsmith = class {
enableDynatrace,
enableAnalytics,
realtime,
eventSourceUrl= "https://realtime.flagsmith.com/",
eventSourceUrl= "https://realtime.flagsmith.com/",
AsyncStorage: _AsyncStorage,
identity,
traits,
Expand Down Expand Up @@ -331,7 +332,7 @@ const Flagsmith = class {
onError?.(message);
};
this.enableLogs = enableLogs || false;
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, storageKey:cacheOptions.storageKey, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
if (!this.cacheOptions.ttl && this.cacheOptions.skipAPI) {
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.")
}
Expand All @@ -345,6 +346,9 @@ const Flagsmith = class {
this.ticks = 10000;
this.timer = this.enableLogs ? new Date().valueOf() : null;
this.cacheFlags = typeof AsyncStorage !== 'undefined' && !!cacheFlags;

FlagsmithEvent = DEFAULT_FLAGSMITH_EVENT + "_" + evaluationContext.environment.apiKey;

if (_AsyncStorage) {
AsyncStorage = _AsyncStorage;
}
Expand Down Expand Up @@ -381,7 +385,7 @@ const Flagsmith = class {
}

if (AsyncStorage && this.canUseStorage) {
AsyncStorage.getItem(FLAGSMITH_EVENT)
AsyncStorage.getItem(FlagsmithEvent)
.then((res)=>{
try {
this.evaluationEvent = JSON.parse(res!) || {}
Expand All @@ -398,12 +402,12 @@ const Flagsmith = class {
}

if (AsyncStorage && this.canUseStorage) {
AsyncStorage.getItem(FLAGSMITH_EVENT, (err, res) => {
AsyncStorage.getItem(FlagsmithEvent, (err, res) => {
if (res && this.evaluationContext.environment) {
const json = JSON.parse(res);
if (json[this.evaluationContext.environment.apiKey]) {
const state = this.getState();
this.log("Retrieved events from cache", res);
const state = this.getState();
this.log("Retrieved events from cache", res);
this.setState({
...state,
evaluationEvent: json[this.evaluationContext.environment.apiKey],
Expand Down Expand Up @@ -453,7 +457,7 @@ const Flagsmith = class {
...json,
evaluationContext: toEvaluationContext({
...json.evaluationContext,
identity: !!json.evaluationContext?.identity ? {
identity: json.evaluationContext?.identity ? {
...json.evaluationContext?.identity,
traits: {
...json.evaluationContext?.identity?.traits || {},
Expand Down Expand Up @@ -513,7 +517,7 @@ const Flagsmith = class {
}
};
try {
const res = AsyncStorage.getItemSync? AsyncStorage.getItemSync(FLAGSMITH_KEY) : await AsyncStorage.getItem(FLAGSMITH_KEY);
const res = AsyncStorage.getItemSync? AsyncStorage.getItemSync(this.getStorageKey()) : await AsyncStorage.getItem(this.getStorageKey());
await onRetrievedStorage(null, res)
} catch (e) {}
}
Expand Down Expand Up @@ -541,15 +545,6 @@ const Flagsmith = class {
}
}

private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
return {
error,
isFetching,
isLoading: false,
source
}
}

getAllFlags() {
return this.flags;
}
Expand Down Expand Up @@ -663,7 +658,7 @@ const Flagsmith = class {
}

setContext = (clientEvaluationContext: ClientEvaluationContext) => {
let evaluationContext = toEvaluationContext(clientEvaluationContext);
const evaluationContext = toEvaluationContext(clientEvaluationContext);
this.evaluationContext = {
...evaluationContext,
environment: evaluationContext.environment || this.evaluationContext.environment,
Expand Down Expand Up @@ -748,6 +743,19 @@ const Flagsmith = class {
return res;
};

private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
return {
error,
isFetching,
isLoading: false,
source
}
}

private getStorageKey = ()=> {
return this.cacheOptions?.storageKey || DEFAULT_FLAGSMITH_KEY + "_" + this.evaluationContext.environment?.apiKey
}

private log(...args: (unknown)[]) {
if (this.enableLogs) {
console.log.apply(this, ['FLAGSMITH:', new Date().valueOf() - (this.timer || 0), 'ms', ...args]);
Expand All @@ -759,7 +767,7 @@ const Flagsmith = class {
this.ts = new Date().valueOf();
const state = JSON.stringify(this.getState());
this.log('Setting storage', state);
AsyncStorage!.setItem(FLAGSMITH_KEY, state);
AsyncStorage!.setItem(this.getStorageKey(), state);
}
}

Expand Down Expand Up @@ -823,7 +831,7 @@ const Flagsmith = class {
private updateEventStorage() {
if (this.enableAnalytics) {
const events = JSON.stringify(this.getState().evaluationEvent);
AsyncStorage!.setItem(FLAGSMITH_EVENT, events);
AsyncStorage!.setItem(FlagsmithEvent, events);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/flagsmith-es/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/flagsmith-es/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flagsmith-es",
"version": "7.0.2",
"version": "8.0.0",
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
"main": "./index.js",
"type": "module",
Expand Down
4 changes: 2 additions & 2 deletions lib/flagsmith/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flagsmith",
"version": "7.0.2",
"version": "8.0.0",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"repository": {
Expand Down
Loading

0 comments on commit fcf1625

Please sign in to comment.