Skip to content

Commit

Permalink
Merge pull request #222 from Flagsmith/chore/synchronous-web-cache
Browse files Browse the repository at this point in the history
chore: Synchronous web cache
  • Loading branch information
kyle-ssg authored Mar 28, 2024
2 parents f991d8a + c63b7f9 commit 68fe0e8
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 126 deletions.
41 changes: 41 additions & 0 deletions async-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export type AsyncStorageType = {
getItemSync?: (key:string)=>string|null
getItem: (key:string, cb?:(err:Error|null, res:string|null)=>void)=>Promise<string|null>
setItem: (key:string, value: string)=>Promise<string|null>
} | null
const AsyncStorage: AsyncStorageType = {
getItemSync: function(key) {
try {
const data = localStorage.getItem(key);
return data || null
} catch (e) {
return null
}
},
getItem: function (key, cb) {
return new Promise<any>((resolve, reject) => {
try {
const result = this.getItemSync!(key);
cb?.(null, result)
resolve(result)
} catch (err) {
cb && cb(err as Error, null);
reject(err);
}
});
},
setItem: function (key:string, value:string, cb?: (err:Error|null, res:string|null)=>void) {
return new Promise<any>((resolve, reject) => {
try {
localStorage.setItem(key, value);
cb && cb(null, value);
resolve(value);
} catch (err) {
cb && cb(err as Error, null);
reject(err);
}
});
}
};

export default AsyncStorage
25 changes: 17 additions & 8 deletions flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './types';
// @ts-ignore
import deepEqual from 'fast-deep-equal';
import { AsyncStorageType } from './async-storage';

enum FlagSource {
"NONE" = "NONE",
Expand All @@ -33,10 +34,7 @@ type DynatraceObject = {
"shortString": Record<string, string>,
"javaDouble": Record<string, number>,
}
type AsyncStorageType = {
getItem: (key:string, cb?:(err:string|null, res:string|null)=>void)=>Promise<string|null>
setItem: (key:string, value: string)=>Promise<string|null>
} | null

let AsyncStorage: AsyncStorageType = null;
const FLAGSMITH_KEY = "BULLET_TRAIN_DB";
const FLAGSMITH_EVENT = "BULLET_TRAIN_EVENT";
Expand Down Expand Up @@ -574,7 +572,7 @@ const Flagsmith = class {
//If the user specified default flags emit a changed event immediately
if (cacheFlags) {
if (AsyncStorage && this.canUseStorage) {
AsyncStorage.getItem(FLAGSMITH_KEY, (err, res) => {
const onRetrievedStorage = (err: Error|null, res: string|null) => {
if (res) {
try {
const json = JSON.parse(res);
Expand Down Expand Up @@ -604,7 +602,7 @@ const Flagsmith = class {
const shouldFetchFlags = !preventFetch && (!this.cacheOptions.skipAPI||!cachePopulated)
this._onChange!(null,
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits && !!Object.keys(this.traits).length },
this._loadedState(null, FlagSource.CACHE, shouldFetchFlags)
this._loadedState(null, FlagSource.CACHE, shouldFetchFlags)
);
this.oldFlags = this.flags;
resolve(true);
Expand Down Expand Up @@ -639,13 +637,20 @@ const Flagsmith = class {
this._loadedState(null, FlagSource.DEFAULT_FLAGS)
);
} else {
onError(WRONG_FLAGSMITH_CONFIG);
this.onError?.(new Error(WRONG_FLAGSMITH_CONFIG));
}
resolve(true);
}
}
return true
});
}
if(AsyncStorage.getItemSync) {
try {
onRetrievedStorage(null, AsyncStorage.getItemSync(FLAGSMITH_KEY))
} catch (e) {}
} else {
AsyncStorage.getItem(FLAGSMITH_KEY,onRetrievedStorage);
}
}
} else if (!preventFetch) {
this.getFlags(resolve, reject);
Expand Down Expand Up @@ -718,6 +723,10 @@ const Flagsmith = class {
this.flags = state.flags || this.flags;
this.identity = state.identity || this.identity;
this.traits = state.traits || this.traits;
this.withTraits = {
...(this.withTraits||{}),
...this.traits,
};
this.evaluationEvent = state.evaluationEvent || this.evaluationEvent;
this.log("setState called", this)
}
Expand Down
3 changes: 1 addition & 2 deletions index-es.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { IFlagsmith } from './types';

import fetch from "unfetch"
// @ts-expect-error
import AsyncStorage from "@callstack/async-storage";
import AsyncStorage from "./async-storage";
import core, { LikeFetch } from './flagsmith-core';
// @ts-ignore
globalThis.FlagsmithEventSource = typeof EventSource!== "undefined"? EventSource: null;
Expand Down
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { IFlagsmith } from './types';
globalThis.FlagsmithEventSource = typeof EventSource!== "undefined"? EventSource: null;

import fetch from "unfetch"
// @ts-expect-error
import AsyncStorage from "@callstack/async-storage";
import AsyncStorage from "./async-storage";
import core, { LikeFetch } from './flagsmith-core';
import _EventSource from 'reconnecting-eventsource'
// @ts-expect-error
Expand All @@ -15,7 +14,8 @@ if (typeof window !== "undefined") {
// @ts-expect-error, some people wish to use flagsmith globally
window.flagsmith = flagsmith;
}

export default flagsmith;
export const createFlagsmithInstance = ():IFlagsmith=>{
return core({AsyncStorage, fetch:_fetch, eventSource:_EventSource})
return core({ AsyncStorage, fetch:_fetch, eventSource:_EventSource})
}
3 changes: 1 addition & 2 deletions isomorphic-es.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-expect-error
import AsyncStorage from "@callstack/async-storage";
import AsyncStorage from "./async-storage";
import {IFlagsmith} from "./types";
// @ts-expect-error
globalThis.FlagsmithEventSource = typeof EventSource !== 'undefined' ? EventSource : null;
Expand Down
3 changes: 1 addition & 2 deletions isomorphic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-expect-error
import AsyncStorage from "@callstack/async-storage";
import AsyncStorage from "./async-storage";
import {IFlagsmith} from "./types";
import core from './flagsmith-core'

Expand Down
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": "3.23.2",
"version": "3.24.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
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": "3.23.2",
"version": "3.24.0",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion lib/react-native-flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-flagsmith",
"version": "3.23.2",
"version": "3.24.0",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"repository": {
Expand Down
12 changes: 11 additions & 1 deletion move-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ fs.copyFileSync(path.join(__dirname,"types.d.ts"),path.join(__dirname,"lib/flags
fs.copyFileSync(path.join(__dirname,"types.d.ts"),path.join(__dirname,"lib/react-native-flagsmith/types.d.ts"))



//Rollup can't ignore lib d.ts files
try {
fs.rmdirSync(path.join(__dirname,"lib/flagsmith-es/lib"), {recursive:true})
Expand All @@ -111,3 +110,14 @@ try {
try {
fs.rmdirSync(path.join(__dirname,"lib/react-native-flagsmith/lib"), {recursive:true})
} catch (e){}


try {
fs.rmdirSync(path.join(__dirname,"lib/flagsmith/test"), {recursive:true})
} catch (e){}
try {
fs.rmdirSync(path.join(__dirname,"lib/flagsmith-es/test"), {recursive:true})
} catch (e){}
try {
fs.rmdirSync(path.join(__dirname,"lib/react-native-flagsmith/test"), {recursive:true})
} catch (e){}
26 changes: 4 additions & 22 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
},
"dependencies": {
"@babel/preset-react": "^7.24.1",
"@callstack/async-storage": "1.1.0",
"encoding": "^0.1.12",
"fast-deep-equal": "^3.1.3",
"isomorphic-unfetch": "^3.0.0",
Expand Down
75 changes: 0 additions & 75 deletions patches/@callstack+async-storage+1.1.0.patch

This file was deleted.

Loading

0 comments on commit 68fe0e8

Please sign in to comment.