Skip to content

Commit 2f4bbbb

Browse files
authored
Merge pull request #251 from blackjid/add_load_stale_cache_option
add loadStale cache option to load already expired cache data
2 parents f2c2964 + abd9699 commit 2f4bbbb

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

flagsmith-core.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ const Flagsmith = class {
257257
traits:ITraits|null= null
258258
dtrum= null
259259
withTraits?: ITraits|null= null
260-
cacheOptions = {ttl:0, skipAPI: false}
260+
cacheOptions = {ttl:0, skipAPI: false, loadStale: false}
261261
async init(config: IInitConfig) {
262262
try {
263263
const {
@@ -306,7 +306,7 @@ const Flagsmith = class {
306306
this.identity = identity;
307307
this.withTraits = traits;
308308
this.enableLogs = enableLogs || false;
309-
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0 } : this.cacheOptions;
309+
this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, loadStale: !!cacheOptions.loadStale } : this.cacheOptions;
310310
if (!this.cacheOptions.ttl && this.cacheOptions.skipAPI) {
311311
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.")
312312
}
@@ -412,10 +412,14 @@ const Flagsmith = class {
412412
}
413413
if (this.cacheOptions.ttl) {
414414
if (!json.ts || (new Date().valueOf() - json.ts > this.cacheOptions.ttl)) {
415-
if (json.ts) {
415+
if (json.ts && !this.cacheOptions.loadStale) {
416416
this.log("Ignoring cache, timestamp is too old ts:" + json.ts + " ttl: " + this.cacheOptions.ttl + " time elapsed since cache: " + (new Date().valueOf()-json.ts)+"ms")
417417
setState = false;
418418
}
419+
else if (json.ts && this.cacheOptions.loadStale) {
420+
this.log("Loading stale cache, timestamp ts:" + json.ts + " ttl: " + this.cacheOptions.ttl + " time elapsed since cache: " + (new Date().valueOf()-json.ts)+"ms")
421+
setState = true;
422+
}
419423
}
420424
}
421425
if (setState) {

lib/flagsmith-es/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith-es",
3-
"version": "4.1.2",
3+
"version": "4.1.3",
44
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
55
"main": "./index.js",
66
"type": "module",

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "4.1.2",
3+
"version": "4.1.3",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

lib/react-native-flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "4.1.2",
3+
"version": "4.1.3",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

test/cache.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ describe('Cache', () => {
112112
...defaultState,
113113
});
114114
});
115+
test('should not ignore cache with expired ttl and loadStale is set', async () => {
116+
const onChange = jest.fn();
117+
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
118+
cacheFlags: true,
119+
onChange,
120+
cacheOptions: { ttl: 1, loadStale: true },
121+
});
122+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
123+
...defaultStateAlt,
124+
ts: new Date().valueOf() - 100,
125+
}));
126+
await flagsmith.init(initConfig);
127+
expect(onChange).toHaveBeenCalledTimes(1);
128+
expect(mockFetch).toHaveBeenCalledTimes(1);
129+
expect(getStateToCheck(flagsmith.getState())).toEqual({
130+
...defaultStateAlt,
131+
});
132+
});
115133
test('should not ignore cache with valid ttl', async () => {
116134
const onChange = jest.fn();
117135
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({

types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface IState<F extends string = string, T extends string = string> {
4646
declare type ICacheOptions = {
4747
ttl?: number;
4848
skipAPI?: boolean;
49+
loadStale?: boolean;
4950
};
5051

5152
export declare type IDatadogRum = {
@@ -233,6 +234,7 @@ export interface IFlagsmith<F extends string = string, T extends string = string
233234
cacheOptions: {
234235
ttl: number;
235236
skipAPI: boolean;
237+
loadStale: boolean;
236238
};
237239
/**
238240
* Used internally, this is the api provided in flagsmith.init, defaults to our production API

0 commit comments

Comments
 (0)