Skip to content

Commit da21142

Browse files
author
Kevin Elko
committed
split storage into indexeddeb storage and in-memory storage implementations
1 parent fce5596 commit da21142

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

packages/remote-config/src/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export function getRemoteConfig(app: FirebaseApp = getApp(), options: RemoteConf
5757
const rc = rcProvider.getImmediate() as RemoteConfigImpl;
5858

5959
if (options.initialFetchResponse) {
60-
//
60+
// We use these initial writes as the initialization promise since they will hydrate the same
61+
// fields that storageCache.loadFromStorage would set.
6162
rc._initializePromise = Promise.all([
6263
rc._storage.setLastSuccessfulFetchResponse(options.initialFetchResponse),
6364
rc._storage.setActiveConfigEtag(options.initialFetchResponse?.eTag || ''),

packages/remote-config/src/register.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { RetryingClient } from './client/retrying_client';
3636
import { RC_COMPONENT_NAME } from './constants';
3737
import { ErrorCode, ERROR_FACTORY } from './errors';
3838
import { RemoteConfig as RemoteConfigImpl } from './remote_config';
39-
import { Storage } from './storage/storage';
39+
import { IndexedDbStorage, InMemoryStorage, Storage } from './storage/storage';
4040
import { StorageCache } from './storage/storage_cache';
4141
// This needs to be in the same file that calls `getProvider()` on the component
4242
// or it will get tree-shaken out.
@@ -80,7 +80,7 @@ export function registerRemoteConfig(): void {
8080
}
8181
const namespace = options?.templateId || 'firebase';
8282

83-
const storage = new Storage(appId, app.name, namespace);
83+
const storage = isIndexedDBAvailable() ? new IndexedDbStorage(appId, app.name, namespace) : new InMemoryStorage();
8484
const storageCache = new StorageCache(storage);
8585

8686
const logger = new Logger(packageName);

packages/remote-config/src/storage/storage.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,7 @@ export function openDatabase(): Promise<IDBDatabase> {
113113
/**
114114
* Abstracts data persistence.
115115
*/
116-
export class Storage {
117-
/**
118-
* @param appId enables storage segmentation by app (ID + name).
119-
* @param appName enables storage segmentation by app (ID + name).
120-
* @param namespace enables storage segmentation by namespace.
121-
*/
122-
constructor(
123-
private readonly appId: string,
124-
private readonly appName: string,
125-
private readonly namespace: string,
126-
private readonly openDbPromise = openDatabase()
127-
) {}
128-
116+
export abstract class Storage {
129117
getLastFetchStatus(): Promise<FetchStatus | undefined> {
130118
return this.get<FetchStatus>('last_fetch_status');
131119
}
@@ -187,6 +175,27 @@ export class Storage {
187175
return this.get<CustomSignals>('custom_signals');
188176
}
189177

178+
abstract setCustomSignals(customSignals: CustomSignals): Promise<CustomSignals>;
179+
abstract get<T>(key: ProjectNamespaceKeyFieldValue): Promise<T | undefined>;
180+
abstract set<T>(key: ProjectNamespaceKeyFieldValue, value: T): Promise<void>;
181+
abstract delete(key: ProjectNamespaceKeyFieldValue): Promise<void>;
182+
}
183+
184+
export class IndexedDbStorage extends Storage {
185+
/**
186+
* @param appId enables storage segmentation by app (ID + name).
187+
* @param appName enables storage segmentation by app (ID + name).
188+
* @param namespace enables storage segmentation by namespace.
189+
*/
190+
constructor(
191+
private readonly appId: string,
192+
private readonly appName: string,
193+
private readonly namespace: string,
194+
private readonly openDbPromise = openDatabase()
195+
) {
196+
super();
197+
}
198+
190199
async setCustomSignals(customSignals: CustomSignals): Promise<CustomSignals> {
191200
const db = await this.openDbPromise;
192201
const transaction = db.transaction([APP_NAMESPACE_STORE], 'readwrite');
@@ -344,3 +353,21 @@ export class Storage {
344353
return [this.appId, this.appName, this.namespace, key].join();
345354
}
346355
}
356+
357+
export class InMemoryStorage extends Storage {
358+
private db: { [key: string]: any } = {}
359+
360+
async get<T>(key: ProjectNamespaceKeyFieldValue): Promise<T> {
361+
return Promise.resolve(this.db[key] as T);
362+
}
363+
364+
async set<T>(key: ProjectNamespaceKeyFieldValue, value: T): Promise<void> {
365+
this.db[key] = value;
366+
return Promise.resolve(undefined);
367+
}
368+
369+
async delete(key: ProjectNamespaceKeyFieldValue): Promise<void> {
370+
this.db[key] = undefined;
371+
return Promise.resolve();
372+
}
373+
}

0 commit comments

Comments
 (0)