From 7651f17e88667e95c1befa5ef2bd30e67d6eab04 Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Thu, 2 Nov 2023 16:21:10 -0700 Subject: [PATCH] make storage synchronous --- packages/experiment-browser/src/experimentClient.ts | 8 ++++---- packages/experiment-browser/src/storage/cache.ts | 8 ++++---- packages/experiment-browser/src/storage/local-storage.ts | 8 +++----- packages/experiment-browser/src/types/storage.ts | 6 +++--- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/experiment-browser/src/experimentClient.ts b/packages/experiment-browser/src/experimentClient.ts index 72c386bd..77be974b 100644 --- a/packages/experiment-browser/src/experimentClient.ts +++ b/packages/experiment-browser/src/experimentClient.ts @@ -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(); } /** @@ -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( @@ -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); } diff --git a/packages/experiment-browser/src/storage/cache.ts b/packages/experiment-browser/src/storage/cache.ts index 7e330f3b..892b3443 100644 --- a/packages/experiment-browser/src/storage/cache.ts +++ b/packages/experiment-browser/src/storage/cache.ts @@ -71,8 +71,8 @@ export class LoadStoreCache { this.cache = {}; } - public async load(): Promise { - const rawValues = await this.storage.get(this.namespace); + public async load() { + const rawValues = this.storage.get(this.namespace); let jsonValues: Record; try { jsonValues = JSON.parse(rawValues) || {}; @@ -100,8 +100,8 @@ export class LoadStoreCache { this.putAll(values); } - public async store(values: Record = this.cache): Promise { - await this.storage.put(this.namespace, JSON.stringify(values)); + public async store(values: Record = this.cache) { + this.storage.put(this.namespace, JSON.stringify(values)); } } diff --git a/packages/experiment-browser/src/storage/local-storage.ts b/packages/experiment-browser/src/storage/local-storage.ts index 57b05ee7..750d2e29 100644 --- a/packages/experiment-browser/src/storage/local-storage.ts +++ b/packages/experiment-browser/src/storage/local-storage.ts @@ -1,16 +1,14 @@ import { Storage } from '../types/storage'; export class LocalStorage implements Storage { - async get(key: string): Promise { + get(key: string): string { return localStorage.getItem(key); } - async put(key: string, value: string): Promise { + put(key: string, value: string): void { localStorage.setItem(key, value); - return; } - async delete(key: string): Promise { + delete(key: string): void { localStorage.removeItem(key); - return; } } diff --git a/packages/experiment-browser/src/types/storage.ts b/packages/experiment-browser/src/types/storage.ts index 36b1cb99..943e63bf 100644 --- a/packages/experiment-browser/src/types/storage.ts +++ b/packages/experiment-browser/src/types/storage.ts @@ -1,5 +1,5 @@ export interface Storage { - get(key: string): Promise; - put(key: string, value: string): Promise; - delete(key: string): Promise; + get(key: string): string; + put(key: string, value: string): void; + delete(key: string): void; }