diff --git a/src/common/Api/LeCoffreApi/sdk/AbstractService.ts b/src/common/Api/LeCoffreApi/sdk/AbstractService.ts index 0f13b415..928f9403 100644 --- a/src/common/Api/LeCoffreApi/sdk/AbstractService.ts +++ b/src/common/Api/LeCoffreApi/sdk/AbstractService.ts @@ -8,66 +8,56 @@ export default abstract class AbstractService { protected constructor() { } - protected static setItem(key: string, process: any): void { - const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]'); - - const index: number = list.findIndex((item: any) => item.process.processData.uid === process.processData.uid); - if (index !== -1) { - list[index] = { - process: process, - timestamp: Date.now() - }; - } else { - list.push({ - process: process, - timestamp: Date.now() - }); - } - - sessionStorage.setItem(key, JSON.stringify(list)); + protected static setItem(key: string, processId: string, process: any): void { + const cache: Record = + JSON.parse(sessionStorage.getItem(key) || '{}'); + + cache[processId] = { + process: process, // we overwrite existing process + timestamp: Date.now() + }; + + sessionStorage.setItem(key, JSON.stringify(cache)); } - protected static getItem(key: string, uid: string): any { - const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]'); - if (list.length === 0) { - return null; - } - - const item: any = list.find((item: any) => item.process.processData.uid === uid); + protected static getItem(key: string, processId: string): any | null { + const cache: Record = + JSON.parse(sessionStorage.getItem(key) || '{}'); + + const item = cache[processId]; if (!item) { return null; } const now: number = Date.now(); if ((now - item.timestamp) < this.CACHE_TTL) { - return item.process; + return { process: item.process, timestamp: item.timestamp }; } return null; } - protected static getItems(key: string): any[] { - const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]'); + protected static getItems(key: string): Record { + const cache: Record = + JSON.parse(sessionStorage.getItem(key) || '{}'); const now: number = Date.now(); - const items: any[] = []; - for (const item of list) { + const validItems: Record = {}; + for (const [processId, item] of Object.entries(cache)) { 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 { - const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]'); + protected static removeItem(key: string, processId: string): void { + const cache: Record = + JSON.parse(sessionStorage.getItem(key) || '{}'); - const index: number = list.findIndex((item: any) => item.process.processData.uid === uid); - if (index !== -1) { - list.splice(index, 1); - } + delete cache[processId]; - sessionStorage.setItem(key, JSON.stringify(list)); + sessionStorage.setItem(key, JSON.stringify(cache)); } }