All checks were successful
Build and Push to Registry / build-and-push (push) Successful in 3m59s
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import MessageBus from 'src/sdk/MessageBus';
|
|
|
|
export default abstract class AbstractService {
|
|
|
|
protected static readonly messageBus: MessageBus = MessageBus.getInstance();
|
|
|
|
private static readonly CACHE_TTL = 60 * 60 * 1000; // 60 minutes cache TTL
|
|
|
|
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 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);
|
|
if (!item) {
|
|
return null;
|
|
}
|
|
|
|
const now: number = Date.now();
|
|
if ((now - item.timestamp) < this.CACHE_TTL) {
|
|
return item.process;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected static getItems(key: string): any[] {
|
|
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
|
const now: number = Date.now();
|
|
|
|
const items: any[] = [];
|
|
for (const item of list) {
|
|
if (now - item.timestamp < this.CACHE_TTL) {
|
|
items.push(item.process);
|
|
}
|
|
}
|
|
|
|
return items;
|
|
}
|
|
|
|
protected static removeItem(key: string, uid: string): void {
|
|
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
|
|
|
|
const index: number = list.findIndex((item: any) => item.process.processData.uid === uid);
|
|
if (index !== -1) {
|
|
list.splice(index, 1);
|
|
}
|
|
|
|
sessionStorage.setItem(key, JSON.stringify(list));
|
|
}
|
|
}
|