1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-27 12:36:14 +01:00

add local and memory storage to web storage service

This commit is contained in:
Kyle Spearrin 2018-06-09 15:19:05 -04:00
parent b93b9feee4
commit 561120c51c

View File

@ -1,27 +1,60 @@
import { StorageService } from 'jslib/abstractions/storage.service';
import { ConstantsService } from 'jslib/services';
export class WebStorageService implements StorageService {
private memoryStore = new Map<string, any>();
private memoryKeys = new Set(['key']);
private localStorageKeys = new Set(['appId', 'anonymousAppId', 'rememberedEmail',
ConstantsService.disableFaviconKey, ConstantsService.lockOptionKey, ConstantsService.localeKey,
ConstantsService.lockOptionKey]);
get<T>(key: string): Promise<T> {
const json = window.sessionStorage.getItem(key);
if (json == null) {
return Promise.resolve(null);
if (this.memoryKeys.has(key)) {
if (this.memoryStore.has(key)) {
const obj = this.memoryStore.get(key);
return Promise.resolve(obj as T);
}
} else {
let json: string = null;
if (this.localStorageKeys.has(key)) {
json = window.localStorage.getItem(key);
} else {
json = window.sessionStorage.getItem(key);
}
if (json != null) {
const obj = JSON.parse(json);
return Promise.resolve(obj as T);
}
}
const obj = JSON.parse(json);
return Promise.resolve(obj as T);
return Promise.resolve(null);
}
save(key: string, obj: any): Promise<any> {
if (obj == null) {
this.remove(key);
return;
return this.remove(key);
}
if (this.memoryKeys.has(key)) {
this.memoryStore.set(key, obj);
} else {
const json = JSON.stringify(obj);
if (this.localStorageKeys.has(key)) {
window.localStorage.setItem(key, json);
} else {
window.sessionStorage.setItem(key, json);
}
}
const json = JSON.stringify(obj);
window.sessionStorage.setItem(key, json);
return Promise.resolve();
}
remove(key: string): Promise<any> {
window.sessionStorage.removeItem(key);
if (this.memoryKeys.has(key)) {
this.memoryStore.delete(key);
} else if (this.localStorageKeys.has(key)) {
window.localStorage.removeItem(key);
} else {
window.sessionStorage.removeItem(key);
}
return Promise.resolve();
}
}