From 6aed0b56fd5f49d542b11885035191075a04696c Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Fri, 9 Jan 2026 09:49:49 -0800 Subject: [PATCH] feat: added flushExecutions to clear cached results --- README.md | 9 +++++++++ src/lib/utils/executionLogger.ts | 11 +++++++++++ src/switcher.ts | 11 +++++++++-- tests/playground/index.ts | 8 ++++---- tests/switcher-functional.test.ts | 19 +++++++++++++++++++ 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b5a17a5..e48e7e8 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,15 @@ This is useful for: - Critical features that must be resolved remotely - Real-time configuration updates +### 6. Flush cached executions for a switcher + +When using throttling, you can clear cached results for a specific switcher: + +```ts +// Clear cached results for a specific switcher +Client.getSwitcher('FEATURE01').flushExecutions(); +``` + ## Testing & Development ### Built-in Stub Feature diff --git a/src/lib/utils/executionLogger.ts b/src/lib/utils/executionLogger.ts index 758dbf6..5c44e45 100644 --- a/src/lib/utils/executionLogger.ts +++ b/src/lib/utils/executionLogger.ts @@ -68,6 +68,17 @@ export default class ExecutionLogger { logger.splice(0, logger.length); } + /** + * Clear results by switcher key + */ + static clearByKey(key: string): void { + 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.ts b/src/switcher.ts index 78a52af..b712396 100644 --- a/src/switcher.ts +++ b/src/switcher.ts @@ -86,7 +86,7 @@ export class Switcher extends SwitcherBuilder implements SwitcherRequest { } if (arg2) { - return this.isItOn(arg1) as Promise; + return Promise.resolve(this.isItOn(arg1)) as Promise; } return this.isItOn(arg1) as boolean; @@ -127,7 +127,7 @@ export class Switcher extends SwitcherBuilder implements SwitcherRequest { /** * Execute criteria with detail information * - * @param arg1 - switcher key + * @param arg1 - switcher key or forceAsync boolean * @param arg2 - when true, forces async execution * @returns SwitcherResult or Promise based on execution mode */ @@ -260,6 +260,13 @@ export class Switcher extends SwitcherBuilder implements SwitcherRequest { } } + /** + * Flush cached executions for the current switcher key + */ + flushExecutions(): void { + ExecutionLogger.clearByKey(this._key); + } + /** * Submit criteria for execution (local or remote) */ diff --git a/tests/playground/index.ts b/tests/playground/index.ts index b5158ff..e63752b 100644 --- a/tests/playground/index.ts +++ b/tests/playground/index.ts @@ -100,14 +100,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); switcher = Client.getSwitcher(SWITCHER_KEY); - console.log("Sync:", await switcher.isItOn()); + console.log("Sync:", await switcher.isItOnBool(true)); - (switcher.isItOn() as Promise) + switcher.isItOnBool(true) .then(res => console.log('Promise result:', res)) .catch(error => console.log(error)); }; diff --git a/tests/switcher-functional.test.ts b/tests/switcher-functional.test.ts index ef4c93f..cc827a4 100644 --- a/tests/switcher-functional.test.ts +++ b/tests/switcher-functional.test.ts @@ -182,6 +182,25 @@ describe('Integrated test - Client:', function () { assertSpyCalls(spyPrepare, 2); }); + it('should flush executions from a specific switcher key', async function () { + // given API responses + given('POST@/criteria/auth', generateAuth('[auth_token]', 5)); + given('POST@/criteria', generateResult(true)); + + Client.buildContext(contextSettings); + const switcher = Client.getSwitcher('FLAG_1').throttle(1000); + + // when + assertTrue(await switcher.isItOn()); + let switcherExecutions = ExecutionLogger.getByKey('FLAG_1'); + assertEquals(switcherExecutions.length, 1); + + // test + switcher.flushExecutions(); + switcherExecutions = ExecutionLogger.getByKey('FLAG_1'); + assertEquals(switcherExecutions.length, 0); + }); + it('should not crash when async checkCriteria fails', async function () { // given API responses // first API call