fetch item prices - throttle end points with promise queue

This commit is contained in:
2018-08-28 21:45:46 -05:00
parent 97acc0d4fa
commit 7ca8606284
17 changed files with 241 additions and 64 deletions

View File

@@ -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,
};