50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
import Store from 'electron-store';
|
|
import { DateTime } from 'luxon';
|
|
import { IItemPrice } from '../model';
|
|
import { IItem } from '../model/item';
|
|
const store = new Store();
|
|
|
|
const storeUsername = (username: string) => {
|
|
store.set('user', username);
|
|
};
|
|
|
|
const storeSessionID = (sessionID: string) => {
|
|
store.set('sessionID', sessionID);
|
|
};
|
|
|
|
const getSessionID = (): string | undefined => {
|
|
return store.get('sessionID') || undefined;
|
|
};
|
|
|
|
const getUsername = (): string | undefined => {
|
|
return store.get('user') || undefined;
|
|
};
|
|
|
|
const getItemCache = (id: string): IItem | undefined => {
|
|
const storedVal = store.get(id);
|
|
if (!storedVal) {
|
|
return undefined;
|
|
}
|
|
const val = JSON.parse(storedVal);
|
|
if (DateTime.fromISO(val.timestamp) < DateTime.local().plus({ minutes: 10 })) {
|
|
return val.item;
|
|
}
|
|
store.delete(id);
|
|
return undefined;
|
|
};
|
|
|
|
const storeItemCache = (id: string, item: IItemPrice) => {
|
|
const timestamp = DateTime.local();
|
|
const val = JSON.stringify({ timestamp, item });
|
|
store.set(id, val);
|
|
};
|
|
|
|
export const StorageService = {
|
|
getItemCache,
|
|
getUsername,
|
|
getSessionID,
|
|
storeItemCache,
|
|
storeUsername,
|
|
storeSessionID,
|
|
};
|