Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make storage synchronous #90

Merged
merged 2 commits into from
Nov 3, 2023
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
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;
}
}