Save maps in SessionStorage
This commit is contained in:
parent
c1f12b2cf7
commit
114c20dd26
@ -8,66 +8,56 @@ export default abstract class AbstractService {
|
|||||||
|
|
||||||
protected constructor() { }
|
protected constructor() { }
|
||||||
|
|
||||||
protected static setItem(key: string, process: any): void {
|
protected static setItem(key: string, processId: string, process: any): void {
|
||||||
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
const cache: Record<string, {process: any, timestamp: number}> =
|
||||||
|
JSON.parse(sessionStorage.getItem(key) || '{}');
|
||||||
const index: number = list.findIndex((item: any) => item.process.processData.uid === process.processData.uid);
|
|
||||||
if (index !== -1) {
|
cache[processId] = {
|
||||||
list[index] = {
|
process: process, // we overwrite existing process
|
||||||
process: process,
|
timestamp: Date.now()
|
||||||
timestamp: Date.now()
|
};
|
||||||
};
|
|
||||||
} else {
|
sessionStorage.setItem(key, JSON.stringify(cache));
|
||||||
list.push({
|
|
||||||
process: process,
|
|
||||||
timestamp: Date.now()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionStorage.setItem(key, JSON.stringify(list));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static getItem(key: string, uid: string): any {
|
protected static getItem(key: string, processId: string): any | null {
|
||||||
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
const cache: Record<string, {process: any, timestamp: number}> =
|
||||||
if (list.length === 0) {
|
JSON.parse(sessionStorage.getItem(key) || '{}');
|
||||||
return null;
|
|
||||||
}
|
const item = cache[processId];
|
||||||
|
|
||||||
const item: any = list.find((item: any) => item.process.processData.uid === uid);
|
|
||||||
if (!item) {
|
if (!item) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const now: number = Date.now();
|
const now: number = Date.now();
|
||||||
if ((now - item.timestamp) < this.CACHE_TTL) {
|
if ((now - item.timestamp) < this.CACHE_TTL) {
|
||||||
return item.process;
|
return { process: item.process, timestamp: item.timestamp };
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static getItems(key: string): any[] {
|
protected static getItems(key: string): Record<string, any> {
|
||||||
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
const cache: Record<string, {process: any, timestamp: number}> =
|
||||||
|
JSON.parse(sessionStorage.getItem(key) || '{}');
|
||||||
const now: number = Date.now();
|
const now: number = Date.now();
|
||||||
|
|
||||||
const items: any[] = [];
|
const validItems: Record<string, any> = {};
|
||||||
for (const item of list) {
|
for (const [processId, item] of Object.entries(cache)) {
|
||||||
if (now - item.timestamp < this.CACHE_TTL) {
|
if (now - item.timestamp < this.CACHE_TTL) {
|
||||||
items.push(item.process);
|
validItems[processId] = item.process;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return items;
|
return validItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static removeItem(key: string, uid: string): void {
|
protected static removeItem(key: string, processId: string): void {
|
||||||
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
const cache: Record<string, {process: any, timestamp: number}> =
|
||||||
|
JSON.parse(sessionStorage.getItem(key) || '{}');
|
||||||
|
|
||||||
const index: number = list.findIndex((item: any) => item.process.processData.uid === uid);
|
delete cache[processId];
|
||||||
if (index !== -1) {
|
|
||||||
list.splice(index, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionStorage.setItem(key, JSON.stringify(list));
|
sessionStorage.setItem(key, JSON.stringify(cache));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user