Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/lib/utils/executionLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
11 changes: 9 additions & 2 deletions src/switcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class Switcher extends SwitcherBuilder implements SwitcherRequest {
}

if (arg2) {
return this.isItOn(arg1) as Promise<boolean>;
return Promise.resolve(this.isItOn(arg1)) as Promise<boolean>;
}

return this.isItOn(arg1) as boolean;
Expand Down Expand Up @@ -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<SwitcherResult> based on execution mode
*/
Expand Down Expand Up @@ -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)
*/
Expand Down
8 changes: 4 additions & 4 deletions tests/playground/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>)
switcher.isItOnBool(true)
.then(res => console.log('Promise result:', res))
.catch(error => console.log(error));
};
Expand Down
19 changes: 19 additions & 0 deletions tests/switcher-functional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down