fetch item prices - throttle end points with promise queue
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
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) => {
|
||||
@@ -17,9 +20,30 @@ 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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user