diff --git a/README.md b/README.md index c8bab53..bf8b659 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,15 @@ Client.subscribeNotifyError((error) => { }); ``` +#### Flush cached results from throttling + +When using throttling, you can clear cached results for a specific switcher: + +```js +// Clear cached results for a specific switcher +Client.getSwitcher('FEATURE01').flushExecutions(); +``` + ### Hybrid Mode Force specific switchers to resolve remotely while running in local mode. Ideal for features requiring remote validation (e.g., Relay Strategies): diff --git a/src/lib/utils/executionLogger.js b/src/lib/utils/executionLogger.js index e861635..1122899 100644 --- a/src/lib/utils/executionLogger.js +++ b/src/lib/utils/executionLogger.js @@ -64,6 +64,17 @@ export default class ExecutionLogger { logger.splice(0, logger.length); } + /** + * Clear results by switcher key + */ + static clearByKey(key) { + for (let index = logger.length - 1; index >= 0; index--) { + if (logger[index].key === key) { + logger.splice(index, 1); + } + } + } + /** * Subscribe to error notifications */ diff --git a/src/switcher.d.ts b/src/switcher.d.ts index 14a55b7..6714071 100644 --- a/src/switcher.d.ts +++ b/src/switcher.d.ts @@ -126,6 +126,11 @@ export class Switcher { */ throttle(delay: number): Switcher; + /** + * Flush cached executions for the current switcher key + */ + flushExecutions(): void; + /** * Force the use of the remote API when local is enabled */ diff --git a/src/switcher.js b/src/switcher.js index 27a1121..637267a 100644 --- a/src/switcher.js +++ b/src/switcher.js @@ -140,6 +140,10 @@ export class Switcher extends SwitcherRequest { } } + flushExecutions() { + ExecutionLogger.clearByKey(this._key); + } + #notifyError(err) { ExecutionLogger.notifyError(err); } diff --git a/tests/playground/index.js b/tests/playground/index.js index 841de60..11e2a2c 100644 --- a/tests/playground/index.js +++ b/tests/playground/index.js @@ -95,14 +95,14 @@ const _testSnapshotUpdate = async () => { console.log('checkSnapshot:', await Client.checkSnapshot()); }; -// Requires remote API +// Does not require remote API const _testAsyncCall = async () => { - setupSwitcher(false); + setupSwitcher(true); const switcher = Client.getSwitcher(SWITCHER_KEY); - console.log('Sync:', await switcher.isItOn()); + console.log('Sync:', await switcher.isItOnBool(true)); - switcher.isItOn() + switcher.isItOnBool(true) .then(res => console.log('Promise result:', res)) .catch(error => console.log(error)); }; diff --git a/tests/switcher-functional.test.js b/tests/switcher-functional.test.js index f2b758f..73ff0e9 100644 --- a/tests/switcher-functional.test.js +++ b/tests/switcher-functional.test.js @@ -193,6 +193,25 @@ describe('Integrated test - Switcher:', function () { assert.equal(spyPrepare.callCount, 2); }); + it('should flush executions from a specific switcher key', async function () { + // given API responses + given(fetchStub, 0, { json: () => generateAuth('[auth_token]', 1), status: 200 }); + given(fetchStub, 1, { json: () => generateResult(true), status: 200 }); // sync + + Client.buildContext(contextSettings); + const switcher = Client.getSwitcher('FLAG_1').throttle(1000); + + // when + assert.isTrue(await switcher.isItOn()); + let switcherExecutions = ExecutionLogger.getByKey('FLAG_1'); + assert.equal(switcherExecutions.length, 1); + + // test + switcher.flushExecutions(); + switcherExecutions = ExecutionLogger.getByKey('FLAG_1'); + assert.equal(switcherExecutions.length, 0); + }); + it('should not crash when async checkCriteria fails', async function () { this.timeout(5000);