@jmondi/browser-storage
Advanced tools
Comparing version 1.8.0 to 1.8.1
@@ -60,6 +60,11 @@ export type Serializer = { | ||
readonly adapter: AsyncAdapter<SetConfig>; | ||
readonly cachedAdapter: MemoryStorageAdapter; | ||
readonly prefix: string; | ||
readonly serializer: Serializer; | ||
constructor(config: AsyncStorageConfig); | ||
clear(): void; | ||
syncCache(): Promise<void>; | ||
getCache(key: string): string | null; | ||
setCache(key: string, value?: string): void; | ||
removeCache(key: string): void; | ||
clear(): Promise<void>; | ||
get<T>(key: string): Promise<T | null>; | ||
@@ -79,2 +84,3 @@ set(key: string, value?: unknown, config?: SetConfig): Promise<boolean>; | ||
private storage; | ||
entries(): IterableIterator<[string, string]>; | ||
clear(): void; | ||
@@ -81,0 +87,0 @@ getItem(key: string): string | null; |
@@ -93,2 +93,8 @@ export class AbstractBrowserStorage { | ||
}); | ||
Object.defineProperty(this, "cachedAdapter", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: new MemoryStorageAdapter() | ||
}); | ||
Object.defineProperty(this, "prefix", { | ||
@@ -110,5 +116,20 @@ enumerable: true, | ||
} | ||
clear() { | ||
this.adapter.clear?.(); | ||
async syncCache() { | ||
for (let [key, value] of this.cachedAdapter.entries()) { | ||
await this.adapter.setItem(key, value); | ||
} | ||
} | ||
getCache(key) { | ||
return this.cachedAdapter.getItem(key); | ||
} | ||
setCache(key, value) { | ||
if (value) | ||
this.cachedAdapter.setItem(key, value); | ||
} | ||
removeCache(key) { | ||
this.cachedAdapter.removeItem(key); | ||
} | ||
async clear() { | ||
await this.adapter.clear?.(); | ||
} | ||
async get(key) { | ||
@@ -178,2 +199,5 @@ return this.fromStore(await this.adapter.getItem(this.prefix + key)); | ||
} | ||
entries() { | ||
return this.storage.entries(); | ||
} | ||
clear() { | ||
@@ -180,0 +204,0 @@ this.storage.clear(); |
@@ -5,3 +5,3 @@ { | ||
"name": "@jmondi/browser-storage", | ||
"version": "1.8.0", | ||
"version": "1.8.1", | ||
"description": "Utilities for local and session browser storage.", | ||
@@ -8,0 +8,0 @@ "keywords": [ |
@@ -60,6 +60,11 @@ export type Serializer = { | ||
readonly adapter: AsyncAdapter<SetConfig>; | ||
readonly cachedAdapter: MemoryStorageAdapter; | ||
readonly prefix: string; | ||
readonly serializer: Serializer; | ||
constructor(config: AsyncStorageConfig); | ||
clear(): void; | ||
syncCache(): Promise<void>; | ||
getCache(key: string): string | null; | ||
setCache(key: string, value?: string): void; | ||
removeCache(key: string): void; | ||
clear(): Promise<void>; | ||
get<T>(key: string): Promise<T | null>; | ||
@@ -79,2 +84,3 @@ set(key: string, value?: unknown, config?: SetConfig): Promise<boolean>; | ||
private storage; | ||
entries(): IterableIterator<[string, string]>; | ||
clear(): void; | ||
@@ -81,0 +87,0 @@ getItem(key: string): string | null; |
@@ -98,2 +98,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(this, "cachedAdapter", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: new MemoryStorageAdapter() | ||
}); | ||
Object.defineProperty(this, "prefix", { | ||
@@ -115,5 +121,20 @@ enumerable: true, | ||
} | ||
clear() { | ||
this.adapter.clear?.(); | ||
async syncCache() { | ||
for (let [key, value] of this.cachedAdapter.entries()) { | ||
await this.adapter.setItem(key, value); | ||
} | ||
} | ||
getCache(key) { | ||
return this.cachedAdapter.getItem(key); | ||
} | ||
setCache(key, value) { | ||
if (value) | ||
this.cachedAdapter.setItem(key, value); | ||
} | ||
removeCache(key) { | ||
this.cachedAdapter.removeItem(key); | ||
} | ||
async clear() { | ||
await this.adapter.clear?.(); | ||
} | ||
async get(key) { | ||
@@ -186,2 +207,5 @@ return this.fromStore(await this.adapter.getItem(this.prefix + key)); | ||
} | ||
entries() { | ||
return this.storage.entries(); | ||
} | ||
clear() { | ||
@@ -188,0 +212,0 @@ this.storage.clear(); |
28463
668