Skip to content

Commit ceddb50

Browse files
authored
feat: add clear method to clear flag configs (#51)
1 parent 920a19c commit ceddb50

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

packages/experiment-browser/src/experimentClient.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ export class ExperimentClient implements Client {
182182
return { ...this.secondaryVariants(), ...this.sourceVariants() };
183183
}
184184

185+
/**
186+
* Clear all variants in the cache and storage.
187+
*
188+
*/
189+
public clear(): void {
190+
this.storage.clear();
191+
this.storage.save();
192+
}
193+
185194
/**
186195
* Get a copy of the internal {@link ExperimentUser} object if it is set.
187196
*

packages/experiment-browser/src/stubClient.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ export class StubExperimentClient implements Client {
3838
return {};
3939
}
4040

41+
public clear(): void {}
42+
4143
public exposure(key: string): void {}
4244
}

packages/experiment-browser/src/types/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface Client {
1010
fetch(user?: ExperimentUser): Promise<Client>;
1111
variant(key: string, fallback?: string | Variant): Variant;
1212
all(): Variants;
13+
clear(): void;
1314
exposure(key: string): void;
1415
getUser(): ExperimentUser;
1516
setUser(user: ExperimentUser): void;

packages/experiment-browser/test/client.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ test('ExperimentClient.all, initial variants returned', async () => {
162162
expect(variants).toEqual(initialVariants);
163163
});
164164

165+
/**
166+
* Call clear() to clear all variants in the cache and storage.
167+
*/
168+
test('ExperimentClient.clear, clear the variants in storage', async () => {
169+
const client = new ExperimentClient(API_KEY, {});
170+
await client.fetch(testUser);
171+
const variant = client.variant('sdk-ci-test');
172+
expect(variant).toEqual({ value: 'on', payload: 'payload' });
173+
client.clear();
174+
const clearedVariants = client.all();
175+
expect(clearedVariants).toEqual({});
176+
});
177+
165178
/**
166179
* Setting source to initial variants will prioritize variants in initial
167180
* variants over those stored in local storage.

0 commit comments

Comments
 (0)