From 3b8a06cf8923f6e0f4481261ce19ae05fb3f8109 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Donoso Date: Mon, 16 Sep 2024 09:38:31 -0300 Subject: [PATCH 1/4] add loadStale cache option to load already expired cache data --- flagsmith-core.ts | 10 +++++++--- test/cache.test.ts | 18 ++++++++++++++++++ types.d.ts | 2 ++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/flagsmith-core.ts b/flagsmith-core.ts index 06e18a2..850a0a5 100644 --- a/flagsmith-core.ts +++ b/flagsmith-core.ts @@ -257,7 +257,7 @@ const Flagsmith = class { traits:ITraits|null= null dtrum= null withTraits?: ITraits|null= null - cacheOptions = {ttl:0, skipAPI: false} + cacheOptions = {ttl:0, skipAPI: false, loadStale: false} async init(config: IInitConfig) { try { const { @@ -306,7 +306,7 @@ const Flagsmith = class { this.identity = identity; this.withTraits = traits; this.enableLogs = enableLogs || false; - this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0 } : this.cacheOptions; + this.cacheOptions = cacheOptions ? { skipAPI: !!cacheOptions.skipAPI, ttl: cacheOptions.ttl || 0, 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.") } @@ -412,10 +412,14 @@ const Flagsmith = class { } if (this.cacheOptions.ttl) { if (!json.ts || (new Date().valueOf() - json.ts > this.cacheOptions.ttl)) { - if (json.ts) { + if (json.ts && !this.cacheOptions.loadStale) { 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") setState = false; } + else if (json.ts && this.cacheOptions.loadStale) { + this.log("Loading stale cache, timestamp ts:" + json.ts + " ttl: " + this.cacheOptions.ttl + " time elapsed since cache: " + (new Date().valueOf()-json.ts)+"ms") + setState = true; + } } } if (setState) { diff --git a/test/cache.test.ts b/test/cache.test.ts index 7a77e1f..b48c8ef 100644 --- a/test/cache.test.ts +++ b/test/cache.test.ts @@ -112,6 +112,24 @@ describe('Cache', () => { ...defaultState, }); }); + test('should not ignore cache with expired ttl and loadStale is set', async () => { + const onChange = jest.fn(); + const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({ + cacheFlags: true, + onChange, + cacheOptions: { ttl: 1, loadStale: true }, + }); + await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({ + ...defaultStateAlt, + ts: new Date().valueOf() - 100, + })); + await flagsmith.init(initConfig); + expect(onChange).toHaveBeenCalledTimes(1); + expect(mockFetch).toHaveBeenCalledTimes(1); + expect(getStateToCheck(flagsmith.getState())).toEqual({ + ...defaultStateAlt, + }); + }); test('should not ignore cache with valid ttl', async () => { const onChange = jest.fn(); const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({ diff --git a/types.d.ts b/types.d.ts index 4b21f32..04878aa 100644 --- a/types.d.ts +++ b/types.d.ts @@ -46,6 +46,7 @@ export interface IState { declare type ICacheOptions = { ttl?: number; skipAPI?: boolean; + loadStale?: boolean; }; export declare type IDatadogRum = { @@ -233,6 +234,7 @@ export interface IFlagsmith Date: Tue, 17 Sep 2024 17:21:00 +0100 Subject: [PATCH 2/4] Version bump --- lib/flagsmith-es/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/flagsmith-es/package.json b/lib/flagsmith-es/package.json index f266326..5c250e1 100644 --- a/lib/flagsmith-es/package.json +++ b/lib/flagsmith-es/package.json @@ -1,6 +1,6 @@ { "name": "flagsmith-es", - "version": "4.1.2", + "version": "4.1.3", "description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.", "main": "./index.js", "type": "module", From 073a91a849a3f6e06fbbb697dcb896ca8f74bc03 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Tue, 17 Sep 2024 17:21:28 +0100 Subject: [PATCH 3/4] Version bump --- lib/flagsmith/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/flagsmith/package.json b/lib/flagsmith/package.json index f198d95..737cb3c 100644 --- a/lib/flagsmith/package.json +++ b/lib/flagsmith/package.json @@ -1,6 +1,6 @@ { "name": "flagsmith", - "version": "4.1.2", + "version": "4.1.3", "description": "Feature flagging to support continuous development", "main": "./index.js", "repository": { From abd9699eec57c8a8c37f85a4e254f1e57396cd66 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Tue, 17 Sep 2024 17:21:44 +0100 Subject: [PATCH 4/4] Version bump --- lib/react-native-flagsmith/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/react-native-flagsmith/package.json b/lib/react-native-flagsmith/package.json index 2e31925..8c333ce 100644 --- a/lib/react-native-flagsmith/package.json +++ b/lib/react-native-flagsmith/package.json @@ -1,6 +1,6 @@ { "name": "react-native-flagsmith", - "version": "4.1.2", + "version": "4.1.3", "description": "Feature flagging to support continuous development", "main": "./index.js", "repository": {