add local and memory storage to web storage service
This commit is contained in:
parent
b93b9feee4
commit
561120c51c
|
@ -1,27 +1,60 @@
|
||||||
import { StorageService } from 'jslib/abstractions/storage.service';
|
import { StorageService } from 'jslib/abstractions/storage.service';
|
||||||
|
import { ConstantsService } from 'jslib/services';
|
||||||
|
|
||||||
export class WebStorageService implements StorageService {
|
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> {
|
get<T>(key: string): Promise<T> {
|
||||||
const json = window.sessionStorage.getItem(key);
|
if (this.memoryKeys.has(key)) {
|
||||||
if (json == null) {
|
if (this.memoryStore.has(key)) {
|
||||||
return Promise.resolve(null);
|
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);
|
const obj = JSON.parse(json);
|
||||||
return Promise.resolve(obj as T);
|
return Promise.resolve(obj as T);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return Promise.resolve(null);
|
||||||
|
}
|
||||||
|
|
||||||
save(key: string, obj: any): Promise<any> {
|
save(key: string, obj: any): Promise<any> {
|
||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
this.remove(key);
|
return this.remove(key);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.memoryKeys.has(key)) {
|
||||||
|
this.memoryStore.set(key, obj);
|
||||||
|
} else {
|
||||||
const json = JSON.stringify(obj);
|
const json = JSON.stringify(obj);
|
||||||
|
if (this.localStorageKeys.has(key)) {
|
||||||
|
window.localStorage.setItem(key, json);
|
||||||
|
} else {
|
||||||
window.sessionStorage.setItem(key, json);
|
window.sessionStorage.setItem(key, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(key: string): Promise<any> {
|
remove(key: string): Promise<any> {
|
||||||
|
if (this.memoryKeys.has(key)) {
|
||||||
|
this.memoryStore.delete(key);
|
||||||
|
} else if (this.localStorageKeys.has(key)) {
|
||||||
|
window.localStorage.removeItem(key);
|
||||||
|
} else {
|
||||||
window.sessionStorage.removeItem(key);
|
window.sessionStorage.removeItem(key);
|
||||||
|
}
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue