@@ -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