Skip to content

Commit

Permalink
fix: make storage synchronous (#90)
Browse files Browse the repository at this point in the history
* make storage synchronous

* update tests
  • Loading branch information
bgiori authored Nov 3, 2023
1 parent 676d801 commit f279830
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
8 changes: 4 additions & 4 deletions packages/experiment-browser/src/experimentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ export class ExperimentClient implements Client {
storage,
);
this.flags = getFlagStorage(this.apiKey, this.config.instanceName, storage);
void this.flags.load();
void this.variants.load();
this.flags.load();
this.variants.load();
}

/**
Expand Down Expand Up @@ -688,7 +688,7 @@ export class ExperimentClient implements Client {
});
this.flags.clear();
this.flags.putAll(flags);
await this.flags.store();
this.flags.store();
}

private async storeVariants(
Expand All @@ -707,7 +707,7 @@ export class ExperimentClient implements Client {
for (const key in failedFlagKeys) {
this.variants.remove(key);
}
await this.variants.store();
this.variants.store();
this.debug('[Experiment] Stored variants: ', variants);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/experiment-browser/src/storage/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export class LoadStoreCache<V> {
this.cache = {};
}

public async load(): Promise<void> {
const rawValues = await this.storage.get(this.namespace);
public async load() {
const rawValues = this.storage.get(this.namespace);
let jsonValues: Record<string, unknown>;
try {
jsonValues = JSON.parse(rawValues) || {};
Expand Down Expand Up @@ -100,8 +100,8 @@ export class LoadStoreCache<V> {
this.putAll(values);
}

public async store(values: Record<string, V> = this.cache): Promise<void> {
await this.storage.put(this.namespace, JSON.stringify(values));
public async store(values: Record<string, V> = this.cache) {
this.storage.put(this.namespace, JSON.stringify(values));
}
}

Expand Down
8 changes: 3 additions & 5 deletions packages/experiment-browser/src/storage/local-storage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Storage } from '../types/storage';
export class LocalStorage implements Storage {
async get(key: string): Promise<string> {
get(key: string): string {
return localStorage.getItem(key);
}

async put(key: string, value: string): Promise<void> {
put(key: string, value: string): void {
localStorage.setItem(key, value);
return;
}

async delete(key: string): Promise<void> {
delete(key: string): void {
localStorage.removeItem(key);
return;
}
}
6 changes: 3 additions & 3 deletions packages/experiment-browser/src/types/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Storage {
get(key: string): Promise<string>;
put(key: string, value: string): Promise<void>;
delete(key: string): Promise<void>;
get(key: string): string;
put(key: string, value: string): void;
delete(key: string): void;
}
6 changes: 3 additions & 3 deletions packages/experiment-browser/test/util/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export const mockClientStorage = (client: Client) => {

class MockStorage implements Storage {
private store = {};
async delete(key: string): Promise<void> {
delete(key: string): void {
delete this.store[key];
}

async get(key: string): Promise<string> {
get(key: string): string {
return this.store[key];
}

async put(key: string, value: string): Promise<void> {
put(key: string, value: string): void {
this.store[key] = value;
}
}

0 comments on commit f279830

Please sign in to comment.